Howdy, Stranger!

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

What is the proper format for setting and calling variables Twine 2.0.11, Sugarcube 1.0.34

Hi,

I'm very new to Twine and have started creating a project using the Web-version of Twine. I'm comfortable with the use and function of variables and use them quite extensively, but I'm running into issues and inconsistencies in my game and want to try and get it sorted out once and for all.

I've done a lot of searching through the web and looked at multiple resources on how to properly format my coding so it works the right way with my configuration, but I'm still not comfortable with the results. My question is, with my version of Twine and story format, what is the correct way/syntax to set and check a variable? I think I may have it incorrect, because there are so many ways listed depending on which version of Twine is used and which format you are using, and they seem to vary slightly between them.

I've been setting my variable using this syntax:
<<set $sky_color to "blue">>

And checking it using this syntax:
<<if $sky_color is "blue">>"Oh look, the sky is blue today."<<endif>>

Which works 90% of the time just fine, but there are some instances where using this same syntax with another variable throws out errors of not being able to set the variable, or bad <<if>> statement.

In those cases, if I change it to:
<<set $sky_color to blue>>

And checking it using this syntax:
<<if $sky_color is blue>>"Oh look, the sky is blue today."<<endif>>

Then it works....however, there are at least three-dozen other variables using the other syntax that work with no issues...and don't get me started on the use of 'eq'.

What syntax should I use in this configuration that is the 'correct' way based on the game engine and story format? Is there a time/place that the ""s are used and some where they shouldn't be? I've tried searching to get the answer, but always seem to find only a partial answer (example given for Twine 2, but Harlow format, or Sugarcube format, and Twine 1).

Thanks in advance!!

Comments

  • edited October 2016
    It depends on the type of the value.

    Here's an example of the most common of the primitive types (a.k.a. value types)—I'm going to skip examples of less used primitive types and reference types for the moment:
    → Strings
    <<set $name to "Bob">>
    <<set $sky_color to "blue">>
    
    → Numbers
    <<set $answer to 42>>
    <<set $ampere to 6.241e18>>
    
    → Booleans
    <<set $hasRedKey to true>>
    <<set $is_ghost to false>>
    


    Grap wrote: »
    I've been setting my variable using this syntax:
    <<set $sky_color to "blue">>
    

    And checking it using this syntax:
    <<if $sky_color is "blue">>"Oh look, the sky is blue today."<<endif>>
    
    You're setting the $sky_color story variable to the string value "blue", which seems perfectly normal. Later, you're checking to see if the $sky_color story variable is strictly equal to the string value "blue", which is also perfectly normal.

    Grap wrote: »
    Which works 90% of the time just fine, but there are some instances where using this same syntax with another variable throws out errors of not being able to set the variable, or bad <<if>> statement.
    So, why not show examples of the instances you had issues with. Seeing the code that's having/causing issues is always better than simply hearing about it.

    Grap wrote: »
    In those cases, if I change it to:
    <<set $sky_color to blue>>
    

    And checking it using this syntax:
    <<if $sky_color is blue>>"Oh look, the sky is blue today."<<endif>>
    

    Then it works....however, there are at least three-dozen other variables using the other syntax that work with no issues... […]
    There is no identifier by the name of blue by default in any browser that I know of, so either that specific example could not work or something you're doing is creating globals.

    If that wasn't an actual example of working code, then as I said before, why not show examples of the instances you're actually having issues with.

    Grap wrote: »
    […] and don't get me started on the use of 'eq'.
    Yes, actually, please do start—I'm honestly interested in what your issue happens to be.


    EDIT/PS: Are you using the <<back>> macro at all? If so, do you know that it rewinds/undoes state?
  • TheMadExile,

    Thank you for looking at this.

    I've corrected the affected code, but here is what the original issue was:

    I have this variable set in the StoryInit passage
    <<set $is_excited to "false">>
    

    And then in the passage where the variable is checked, I put:
    <<if $is_excited is "true">>Nikki gets up off the couch and moves towards him...<<endif>>
    

    I removed and re-typed the variable several times, thinking maybe it was simple as a typo, but always got the same result. The only thing I did to correct this problem was to remove the ""s around false and true.

    Looking at the part of your reply dealing with Booleans, I see where I may be making an error in my coding. The odd thing is, that nearly all of my variables are coded like this:
    <<set $has_dvd to "false">>
    <<set $has_pics to "false">>
    <<set $has_key to "false">>
    

    and I've not had any issues with them. Is this a case of just getting 'lucky' that the system hasn't balked at all the instances of having the quotes around my true/false variables?

    To respond to the <<back>> edit; Yes, I think I have 1, maybe 2 instances of that macro used, but I don't believe it's used anywhere in the recently affected passages, but I'll double check. If the macro is not used/called during the debugging process, could the macro affect any passages outside of it?

    Another thing I'm thinking is that I'm checking multiple variables in the same passage to tailor the output. I'm wondering if the way I'm using the code may be causing an issue.

    In this case, the previous passage sets the variable. In my example, there may be more than one 'source' passage which leads to a single 'destination' passage, like this:

    Passage: Watch DVD:
    Nikki stops watching the DVD and puts down the remote.<<set $is_excited to "true">><<set $caught_with_dvd to "true">>
    What should she do now?
    
    [[Look at him?]]
    [[Leave]]
    

    Passage: Read magazine:
    Nikki stops reading the magazine and quickly puts it away.<<set $is_excited to "true">><<set $caught_with_mag to "true">>
    
    What should she do now?
    
    [[Look at him?]]
    [[Leave]]
    

    If the player selects the look at him passage, the variables are checked and the dialog is output accordingly:

    Passage: Look at him:
    <<if $caught_with_mag is "true">>"I was just...well, it was just laying here on the table, so I..."<br><br><<endif>><<if $caught_with_dvd is "true">>"I was just...well, I accidentally hit the Play button on the remote and the movie started, so I..."<br><br><<endif>>"Jason looks at her with disappointment. He thought he had raised her better than this..."
    

    The instance where this error occured is in the above example. In other passages where only a single variable is checked, I've not run into a problem.

    I'm not a coder, so it could be as simple as the way I'm structuring the code is causing the issue, which is fine, I can correct it, it's just that my brain isn't used to looking at information a different way, but I know it can be an issue.

    Thanks again for your help!
  • edited October 2016
    Edit: The variables <<set $caught_with_mag to "false">> and <<set $caught_with_dvd to "false">> are set in the StoryInit passage at the start.

    oh, wow...I think I just solved it!! I just read what you wrote in another thread.
    PSA: Word processors can easily break code and are not recommended—breakage often comes via typographic character replacements, e.g. replacement of normal double/single quotes with typographic/curly quotes. They may be used, however, care should always be taken when using any non-plain text editor.

    I wrote the passages that were broken in Word, then copy/pasted them in. the <<set>> variable in the StoryInit passage was done in the program, so I'll bet the ""s were different. *slams face into desk*

    whoops!!

    I just did a test and yes, the exact same thing happened...okay, I can go to bed now that I've learned something today.

    Thank you for listening!

  • Grap wrote: »
    <<set $is_excited to "false">>
    
    <<if $is_excited is "true">>Nikki gets up off the couch and moves towards him...<<endif>>
    
    In the above example you are (incorrectly) using a String value instead of a Boolean value to represent something that is either true or false and TheMadExile included an example of how to correctly assign a Boolean value, note the lack of quotes around the value.
    → Booleans
    <<set $hasRedKey to true>>
    <<set $is_ghost to false>>
    

    The following example show how to use an <<if>> to test the current state of a Boolean variable.
    <<if $hasRedKey>>You have the Red Key<</if>>
    <<if not $is_ghost>>You are not a Ghost<</if>>
    
    <<set $is_excited to false>>
    <<if $is_excited>>Nikki gets up off the couch and moves towards him...<<endif>>
    
  • Greyelf,

    Thank you for the further clarification. Yeah, it clears up the overall coding error I was making. I've gone back through all of my code and cleaned it up. Cleans up the coding quite a bit, and avoids the possibility of running into the issue I was having.

    Thanks again, everyone, for the help!
Sign In or Register to comment.