The Room Adventure: Additional Features

Asking the Player for a Name: Text Version

Difficulty: ★☆☆☆☆

Requirement: Basic game

You can have the player include their name in the game. Then add some references to it throughout. This can help bring a player into the experience.

What we need to do

  • We need to have a place for the player to get a name.
  • We need to update the game text to make use of the name.

Step 1

Go into your startGame() function and find the line where we get the player object. It's near the top of the function.

Step 2

We're going to use JavaScript's prompt function to ask the player for their name. We will also include a default value in case they want to use that instead. The default value is an optional argument, so you could leave it out, but then the dialogues would look a little funny.

Add the new code.

We're being a little over-cautious here. The getPlayer() function sets "Lica" as a default name already. We also put "Lica" in the prompt's default answer. If the player just hits enter, that's the name that will be used.

So why bother with the if statement? It's just in case the player deletes the name and leaves the field blank. You could leave out the if check and just leave the variable assignment (player.name = newName;) if you prefer.

If you don't want the if statement, you could shorten this even further and do this instead.

Step 3

Now go through the text strings in your game and add calls to player.name wherever you'd like to use the name. We're already inside the startGame function, so let's make a minor change while we're here.

Be careful. Remember that player.name is a variable and it has to be outside any sets of quotation marks for it to work. You have to make sure you concatenate correctly. That just means you need to use the plus sign to add the variable. Look at these generic examples so you see what I'm talking about.

Syntax Samples

Pay close attention to the starting and ending quotes in each example. You should see that player.name is never inside the quotes, even in the second example. There, the quotes open, you have some text, then they close, and then the name is added in, then a new set of quotes opens, with text, and then closes. It's a lot to look at, so take your time with it.

It's easy to cause a Syntax Error by forgetting the plus signs!

Updating startGame()

When we first made the game, we told the player what their name was. It's kind of odd to leave it that way. If someone types in "Kevin" then game says, "You are Kevin...". Um, yeah, I know. So let's make a change.

Currently, the next line of the text string says " and you are in a house". I added the "It's a new day" part so the sentence would make sense without me having to change anything else. But it's your game too, so go right ahead and change the text to whatever you want!

Update some other text strings

These are just suggestions. Change whatever you want!

Where else would you want to use the player's name?

Happy Coding!

—Dr. Wolf