Howdy, Stranger!

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

Calling variables from other passages. (Snowman)

Hi! I'm using the JavaScript in my game and I used it to record information like the players name and age. I'm trying to call back to the variables in other passages to make my story more interactive. Below is my code with some string changes as to not reveal my writing.
<%
var start = prompt("Type 'start' to continue...")
alert("Let's gather some information about yourself.")
var PLAYER = prompt("What's your name?")
var AGE = prompt("How old are you? (This will determine whether profanity is used or not ONLY.)")
var SEX = prompt("Are you a boy? or a girl?")
var CITY = prompt("What city are you from?")

if (start === "start") {
print(" You are " + PLAYER + ". You live in "+CITY+". You are a "+SEX+". You are "+AGE+" years old.");
}
else {
close();
}

%>
Chapter 1
The calls to variables like PLAYER and CITY work fine in this passage, but when addressing them in other passages like the below:
You decide to go to the bank. While Frances is on her phone, your friends start walking off. <% print("Wait up " + PLAYER + " !") %> You and your friends wait for her to catch up. You head towards the bank. As you are walking, Josh suddenly stops. "Yo, whats up Josh? You alright?" asks Trevor. Josh pukes on the ground. "Holy <% if (AGE < 13) {
print("shit josh!");
}
else {
print("cow josh!");
}
%>
I get an error message that reads:
⚠ Uncaught ReferenceError: PLAYER is not defined (file:///C:/Users/Alec/AppData/Local/Temp/nw5500_29090/index.html#stories/fff4c9b3-e354-dd95-3290-56d6e1dd92d9/test: 4)

It seems to me that vars cannot be called upon in other passages. Is there a way that I can achieve my goal?

Comments

  • The first point in the Changes From The Norm section of the Snowman 2 documentation states that a special variable s is passed to each passage being displayed. This special variables can be used to store your story's data so it can be passage from one passage to the next.

    a. The passage from your example modified to use the s variable, I have not modified/corrected any other potential issues with the code.
    <%
    s.start = prompt("Type 'start' to continue...")
    alert("Let's gather some information about yourself.")
    s.PLAYER = prompt("What's your name?")
    s.AGE = prompt("How old are you? (This will determine whether profanity is used or not ONLY.)")
    s.SEX = prompt("Are you a boy? or a girl?")
    s.CITY = prompt("What city are you from?")
    
    if (s.start === "start") {
    print(" You are " + s.PLAYER + ". You live in " + s.CITY + ". You are a " + s.SEX + ". You are " + s.AGE + " years old.");
    }
    else {
    close();
    }
    
    %>
    [[Chapter 1]]
    
    b. An example of a possible Chapter 1 passage outputting the current values store within the s variable.
    start: <%= s.start %><br>
    PLAYER: <%= s.PLAYER %><br>
    AGE: <%= s.AGE %><br>
    SEX: <%= s.SEX %><br>
    CITY: <%= s.CITY %>
    

    note: The reasons you cant directly reference variables created in one passage from another is that those variables are created within a local scope and that local scope was destroyed once the next passage is rendered.
  • @greyelf Thank you so much! So if exchange the "var" tags with "s.variableName", I can call back to that variable in any future passage by just typing "s.variableName"?
  • Yes, you can access the current value of any variable that you have assigned previously to the special s variable but you are not calling back to the previous passage because that passage no longer exists.
Sign In or Register to comment.