Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How do I allow the player to choose their name?

I've been tying to work out how to give players the option to type their own name rather than just choose one from a selection. How can I do this?

I'm not settled in a format yet but I think I'll stick with T1 with Sugarcane / Sugarcube 2 at least for now.

Thanks in advance!


  • edited April 2016
    Ahoy, the way I did it in mine (using twine 2 - so not sure if it will work in twine 1 though =/ - and sugarcube 2.5.0) was to have a passage that leads to where you want to ask the player their name. In that passage, I just had
    Enter name and press enter <<textbox "$name" "" "nextpassage">>
    You will then have to manually create the 'nextpassage' passage, as it won't automatically make it like it would with a normal passage. When the player enters their name in the box and press enters, whatever they will put in will be used as the $name setting, and typing $name into a passage will display the name they entered.

    I've also added a file which is a basic example of the above.
  • Thank you, I'll give that a go and see if it works!
  • Hm... 'no macro or passage called 'textbox'.
  • So I just found the <<textinput>> macro. So I did this -
    'Your name is <textinput $name>>

    with a link to the next passage containing '<<display $name>>' but it gives me the following error -
    '<<display>>: "state.history[0].variables.mname == null && (state.history[0].variables.mname = 0);state.history[0].variables.mname" did not evaluate to a passage name'
  • If I use a button macro I just get a '<<display>>: The "Bob" passage does not exist'
  • @Tilaidie
    You need to be careful which tags you assign to your questions because sugarcube 2 and sugarcane are two different story formats.
    This is why you received the "no macro or passage called 'textbox'" error message because <<textbox>> is a SugarCube macro and it seems that you are actually using the Sugarcane story format.

    The <<display>> macro is used to display the contents of passages and depending on the value of $name in your example you will get one of three results:

    1. If the value of $name is the name of a passage then the contents of that passage will be displayed within the passage containing the <<display $name>> macro call.

    2. If $name has a value but that value is not the name of a passage then you will receive the following error: <<display>>: The "a" passage does not exist

    3. If $name has no value or its value is an empty String then you will receive an error similar to the one included in your post.

    If you want to output the current value of a variable then you should be using the <<print>> macro.
    <<print $name>>
  • Yusssss!!! <<print $name>> it is! Thanks oodles!
  • edited August 2016
    Thanks for the tips, but I am trying to figure out something related to If/End IF conditions. - How to 'ensure' a $name variable is NEVER empty when you first set it up. (Like the reader may just 'hit' return and go onto the game.) Btw, I'm working in Twine 2 and Sugarcube 2, I believe.
  • edited August 2016
    In StoryInit passage write <<set $name to "Uncle Bob">>
  • Thank you. Not 'quite' what I was looking for, but thanks :)
  • Your post does make it sound like you wanted pretty much what Claretta provided. So, since that's not quite what you were looking for, would you care to elaborate on what you do want?

    I mean, we could guess, but that's hardly fair to us. There are various ways to do what I think you probably want. Rather than enumerate them all, however, it would be nice to be able to cut to the chase.
  • Sorry - um, well, I'm working in Twine 2/Sugarcube, and essentially I got the textbox code working - but I wanted to have some code that 'verified' that the 'name' you set is NOT blank before moving to the main game.

    I decided to start a discussion topic with this in more depth, but it hasn't been approved yet, I think?

    <<textbox "$name" "">>
    <<if $name isnot "">>
    <<goto "MainPassageInGame">>

    Basically what I wanted the code to do is you input the name in the textbox, and it'll go to the main passage to start the TWINE novel. BUT if the reader should happen to 'hit return' without typing in anything... I want them to put a name in, OR I can have it set to a 'default name' and move onto the Passage.
  • I'd probably recommend something like the following.

    1. Create a passage named StoryInit if you don't already have one—that's a passage which has special meaning to SugarCube. Put something like the following in it, much as Claretta described:
    <<set $name to "John">> 
    That will setup the default name.

    2. Use something like the following where you want the name entry to be:
    <<textbox "$name" $name>> \
    <<button "Confirm">>
    <<set $name to $name.trim()>>
    <<if $name is "">>
    	<<replace "#name-error">>Please enter a name!<</replace>>
    	<<goto "MainPassageInGame">>
    <</button>> &nbsp;&nbsp; \
    <span id="name-error"></span>
    That will inform the player that they need to enter a name if they empty the textbox or leave nothing but spaces.
  • edited August 2016
    Wow. Thank you. I'll do that! AND YES. The code works. Thank you very much. THIS should be a staple for those who want to let the users put in their own 'character' name while playing the TWINE Fiction.
Sign In or Register to comment.