banner



How To Create A Random Name Generator

Read Time: 4 mins Languages:

In this tutorial, I'll show you how to create a simple random name generator, which you could use to give random NPCs a huge variety of unique names or give a player name suggestions when they click a New Name button.

The basic system is fairly simple, and can be built in pretty much any programming language and environment available. We'll be building it in Unity, which should be relatively easy to set up. If you want to check out the code directly, you can get the project files from GitHub.

Ready? Let's get going.


Set Up the Interface

Create a new JavaScript file in Unity and call it nameGenerator. Put the following code into it:

var currentName: String = "Alex"; function OnGUI() {     if(GUI.Button(Rect(10,10,100,50), "New Name"))     {         CreateNewName();     }     GUI.Box(Rect(120,10,200,50), currentName); }  function CreateNewName() { }

Then add this script to a gameObject in the scene (putting in on the main camera will do).

This creates a simple interface that will display the name that will be created. If you try it out, the button will do nothing right now, though.


Create the First Name

Add Some Syllables

To generate a new first name, we'll first create a list of syllables from which the name will be assembled. Add this code:

var firstNameSyllables: Array; function Start() {     firstNameSyllables = new Array();     firstNameSyllables.Push("mon");     firstNameSyllables.Push("fay");     firstNameSyllables.Push("shi");     firstNameSyllables.Push("zag");     firstNameSyllables.Push("blarg");     firstNameSyllables.Push("rash");     firstNameSyllables.Push("izen"); }

You'll want to add some more yourself; this is by far not enough!

Put the First Name Together

To actually choose a name, adapt the CreateNewName() function like so:

function CreateNewName() {     //Creates a first name with 2-3 syllables     var firstName: String = "";     var numberOfSyllablesInFirstName: int = Random.Range(2, 4);     for (var i: int = 0; i < numberOfSyllablesInFirstName; i++)     {         firstName += firstNameSyllables[Random.Range(0, firstNameSyllables.length)];     }     var firstNameLetter: String = "";     firstNameLetter = firstName.Substring(0,1);     firstName = firstName.Remove(0,1);     firstNameLetter = firstNameLetter.ToUpper();     firstName = firstNameLetter + firstName; }

This code will randomly pick syllables from the list, two or three times, and string them together. Then it will remove the first letter, capitalize it, and stick it back in front of the name.

When you press the button now, you get a randomly assembled name from the list! You can try it out in this build:

Here are some of the generated names:

  • Zagmonshi
  • Izenzag
  • Shifay
  • Rashblarg

But this name generator is quite simple. Let's multiply our results by adding last names.


Create the Last Name

Add Some Syllables

Let's add a new list of syllables which will be only used in the last name. The new code looks like this:

var lastNameSyllables: Array; function Start() {     lastNameSyllables = new Array();     lastNameSyllables.Push("malo");     lastNameSyllables.Push("zak");     lastNameSyllables.Push("abo");     lastNameSyllables.Push("wonk"); }

As before, these are just to get you started. Add more syllables to make it more diverse!

Putting It All Together

Next, adapt the CreateNewName() function this way:

function CreateNewName() {     //Creates a first name with 2-3 syllables     var firstName: String = "";     var numberOfSyllablesInFirstName: int = Random.Range(2, 4);     for (var i: int = 0; i < numberOfSyllablesInFirstName; i++)     {         firstName += firstNameSyllables[Random.Range(0, firstNameSyllables.length)];     }     var firstNameLetter: String = "";     firstNameLetter = firstName.Substring(0,1);     firstName = firstName.Remove(0,1);     firstNameLetter = firstNameLetter.ToUpper();     firstName = firstNameLetter + firstName;      //Creates a last name with 1-2 syllables     var lastName: String = "";     var numberOfSyllablesInLastName: int = Random.Range(1, 3);     for (var j: int = 0; j < numberOfSyllablesInLastName; j++)     {         lastName += lastNameSyllables[Random.Range(0, lastNameSyllables.length)];     }     var lastNameLetter: String = "";     lastNameLetter = lastName.Substring(0,1);     lastName = lastName.Remove(0,1);     lastNameLetter = lastNameLetter.ToUpper();     lastName = lastNameLetter + lastName;      //assembles the newly-created name     currentName = firstName + " " + lastName; }

Now, instead of just getting one name from the list, the generator will create a first name, put a space after it, and then create a last name.

You can try it out in this build here:

Here are some of the generated names:

  • Blargshiizen Maloabokor
  • Zagblarg Maloson
  • Rashzag Wonkli
  • Shifay Abomalo

Add a Suffix to the End of the Name

As a final detail, let's add a suffix, which will appear at the end of the last name. Add this code to the beginning of the nameGenerator script:

var nameSuffixes: Array;  function Start() {     nameSuffixes = new Array();     nameSuffixes.Push("son");     nameSuffixes.Push("li");     nameSuffixes.Push("ssen");     nameSuffixes.Push("kor"); }

Next, add the following to CreateNewName(), before the name gets assembled at the end:

//adds a suffix to the last name with a chance of 50% if(Random.Range(0,100) < 50) {     lastName += nameSuffixes[Random.Range(0, nameSuffixes.length)]; }

Now there is a 50% chance that a generated name will get a typical name ending.


Conclusion

In this tutorial, I showed you the basic code to form a name generator. You can extend this with extra variables and features. Try adding:

  • Middle names
  • More name suffixes (III, OBE, and so on)
  • Prefixes (Sir, Lady, Count, and the like)
  • Other additions ("Glagnarr the Destructor")

You can use this idea for other things, too: give spaceships random names, or create random item descriptions. Go wild!

Matthias Zarzecki

Matthias Zarzecki is an Unity-Award nominated Indie Designer and Software Engineer. His games can be found on iOS, Android, OUYA and the GameStick. He is on Twitter as @IcarusTyler, and his website is matthewongamedesign.com.

How To Create A Random Name Generator

Source: https://gamedevelopment.tutsplus.com/tutorials/quick-tip-how-to-code-a-simple-character-name-generator--gamedev-14308

Posted by: pullenmrseach.blogspot.com

0 Response to "How To Create A Random Name Generator"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel