Howdy, Stranger!

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

Name Replacement (JavaScript) (Snowman 1.1)

Hello! I'm ecstatic to have found Twine! I am very inexperienced when it comes to JavaScript and HTML, but I've fond that they provide me with the widest range of customization options. I was able to quickly format the story layout (font, text size, margins, alignment, etc) by modifying the stylesheet found while using Snowman 1.1.

I've been using this code to set the name variable in the 'starting point' passage.
<%var character_name = prompt("Enter Your Character's Name");%>

I'd like to use variables that allow the player to set their own character name for the duration of the game. For example, if the variable was 'character_name' and the player typed Steve' as their character's name. All the 'character_name' text would be replaced.

JavaScript text would read. "Hello, 'character_name'."

The player then inputs 'Steve' into the text box.

CYOA text then reads. "Hello, Steve."

Comments

  • Snowman has the story.state object (in-template shorthand: s) which can be used for this purpose (must be used, if you want values saved into checkpoints).

    Set the name:
    <% s.characterName = prompt("Enter Your Character's Name") %>
    
    Reference the name:
    Hello, <%= s.characterName %>.
    


    As small aside. Snowman uses _.template(), so you have three different template hooks you can use within normal passages (not Story JavaScript). For example:
    <% … %>  : Run some JavaScript.
    <%= … %> : Run some JavaScript and interpolate the result.
    <%- … %> : Run some JavaScript and interpolate the HTML-escaped result.
    
  • Thank so so very much, TheMadExile! I should have come here first.
Sign In or Register to comment.