64x64 icon dark hosting
Choose a hosting plan here and get a free year's subscription to Tuts+ (worth $180).
Advertisement

Quick Tip: How to Code a Simple Character Name Generator

by
Gift

Start a hosting plan from $3.92/mo and get a free year on Tuts+ (normally $180)

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:

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:

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:

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:

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:

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:

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

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!


References

Advertisement