So how do I set the player's name and then recall it

So as the title says I want figure out a way to ask the player to type in a name, and then it's saved as an attribute. Then in a in dialogue the variable is recalled and then inserted into the sentence.
"Hey there,(Player Name), nice to see you again."
like that?


K.V.

EDIT

VERSION 2

You'd want to use player.alias.


Create a new function named: SetCustomPlayerName

Paste this in for the script:

  msg ("Please enter your name.")
  get input {
    // Use Trim() to remove any leading or trailing whitespace.
    if (Trim(result) = ""){
      // If the player didn't enter any valid characters, print an error message and call this function again.
      msg ("Invalid input.  Please try again.")
      SetCustomPlayerName
    }
    else {
      // If the player DID enter some characters, set that to player.alias.
      player.alias = Trim(result)
      // From here on out, we can refer to the player as player.alias.
      msg ("Hey there, {player.alias}!  Nice to meet you!")
    }
  }

To call it, add the line SetCustomPlayerName to your script to call the new function.

(This function would probably be called before or after entering the first room of the game).


Use "player.alias" for the attribute - that is what it is for.


@ K.V.
I tired the code in the after entering script for the first room of the game and got this error
Failed to load game.
The following errors occurred:
Error: Error adding script attribute 'enter' to element 'world entrance': Invalid variable name ''
Anyway I used your code to construct one that uses an attribute of the player object to set a name, but I can't figure out how to call the attribute in a message
So
"Hi, my name is (player.custom_name), nice to meet you."
That how I want to call it


K.V.

My mistake. That was an entire function in full code view.

You would need to paste that function into full code view, just above the last line of code (which is always "</asl>").

Then, in that after enter script, add the line SetCustomPlayerName.


To skip the entire function:

Try pasting this alternate version in your script instead:

// **
// * Script to allow the player to input a name.
// *
// **/
// Create a temporary script instead of creating a function.
game.nameSetupScript => {
  msg ("Please enter your name.")
  get input {
    // Use Trim() to remove any leading or trailing whitespace.
    if (Trim(result) = "") {
      // If the player didn't enter any valid characters, print an error message and invoke this script again.
      msg ("Invalid input.  Please try again.")
      invoke (game.nameSetupScript)
    }
    else {
      // If the player DID enter some characters, set that to player.alias
      player.alias = Trim(result)
      // From here on out, we can refer to the player as player.alias
      msg ("Hey there, {player.alias}!  Nice to meet you!")
      // Delete this script.  We are done with it.
      game.nameSetupScript = null
    }
  }
}
// Invoke the script
invoke (game.nameSetupScript)
// **
// * END OF SCRIPT
// **/

NOTE:

I changed it to player.alias, as The Pixie was correct.


<game name="EXAMPLE">

  <attr name="start" type="script">

    msg ("Name?")

    get input {

      // quest (for 'get input', 'show menu / ShowMenu', 'ask / Ask', and etc) automatically (hidden from you) does this:
      //
      // result = YOUR_TYPED_IN_INPUT_OR_YOUR_SELECTED_INPUT_FROM_A_MENU_CHOICE

      player.alias = result

      // using the "normal" scripting method:
      
      msg ("Hey there, " + player.alias + ", nice to see you again.")

    }

  </attr>

</game>

or:

<game name="EXAMPLE">

  <attr name="start" type="script">

    msg ("Name?")

    get input {

      // quest (for 'get input', 'show menu / ShowMenu', 'ask / Ask', and etc) automatically (hidden from you) does this:
      //
      // result = YOUR_TYPED_IN_INPUT_OR_YOUR_SELECTED_INPUT_FROM_A_MENU_CHOICE

      player.alias = result

      // using the text processor commands:

      msg ("Hey there, {player.alias}, nice to see you again.")

    }

  </attr>

</game>

Okay I got it to work, thanks guys you made this problem very easy and fast to solve, which is not always the result I get when asking questions you guys are the best!


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums