Howdy, Stranger!

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

Trouble with variables in Sugar cube/passages partially diapering

edited May 2015 in Help! with 1.x
Hi-despite playing around with Twine 1.0, I'm still very much a beginner-I've tried making a game with a simple yes/no variable, with a latter link not appearing if the variable remains"no" (in this case, picking up an object)

but for some reason, the link to that passage either always shows up, or half the passage flat out disappears. I'm currently using sugarcube-any advice?

Comments

  • edited May 2015
    Posting the code you're using will allow us to see what's wrong. If you're trying to use the actual words "yes" or "no" you might be running into issues with writing strings properly, but impossible to tell without seeing the code.
  • okay this is in the first passage

    <<silently>>
    <<set $cross="no">>
    <<endsilently>>

    and here's the second:

    <<set $cross = "yes">>

    and here's the last bit:


    <<if $cross= "yes">> The sight of the cross repeals them

    <<else>>you're powerless to resist there charms <<endif>>

    I'm thinking about switching over to Harlowe -it seems a bit easier
  • edited May 2015
    You are not writing the variables consistently. In the first you lack spaces and in the second you have spaces, so you are actually creating two different variables, "$cross" and "$cross=". In your if statement you are trying to call "$cross=", which you do not define as yes anywhere - you only define "$cross" as yes.

    So you would want:
    <<silently>>
    <<set $cross = "no">>
    <<endsilently>>
    
    <<set $cross = "yes">>
    
    <<if $cross = "yes">> [[The sight of the cross repeals them]]<<else>>[[you're powerless to resist there charms]] <<endif>>
    

    Also remember you can use booleans like true and false. Brush up on:

    http://www.motoslave.net/sugarcube/1/docs/macros.html#macros-set

    I just use 1 and 0 for these types of variables. I find it simpler than using strings.

    Also, bear in mind that you are using an else statement, so there's technically no need to set a false condition, since <<else>> assumes falsehood if the true condition is not met. You could simply say in the second passage:
    <<set $cross to 1>>
    

    And then in the other say:
    <<if $cross is 1>>[[The sight of the cross repeals them]]<<else>>[[you're powerless to resist their charms]]<</if>>
    

    All that said, it's still recommended to initialise $cross to false at story start in the StoryInit special passage. You just do not need to set it to false in the actual story once you've done that, since we assume that your protagonist does not start the game with a cross.
  • samz93 wrote: »
    <<if $cross= "yes">> [[The sight of the cross repeals them]]
    
    You are making a common newbie mistake.

    In Twine (and Javascript) a single equals sign '=' means assignment where as a double equals sign '==' is used to compare. So what is happening in your <<if>> macro is that you are assigning the value of 1 to your $cross variable and then testing if that assignment was true.

    For this reason I suggest you use the words to and is instead of a single or double equals sign. Your example re-written:
    note: you don't need to use the <<silently>> macro in your first passage, <<set>> macros don't output anything expect any new-lines you add yourself, which you can remove using a backslash.
    :: first passage
    <<set $cross to "no">>\
    
    
    :: second passage
    <<set $cross to "yes">>\
    
    
    :: other passage
    <<if $cross is "yes">> [[The sight of the cross repeals them]]
    <<else>>[[you're powerless to resist there charms]]<</if>>
    

    If you want to keep using equal signs then your example would look like the following:
    :: first passage
    <<set $cross = "no">>\
    
    
    :: second passage
    <<set $cross = "yes">>\
    
    
    :: other passage
    <<if $cross == "yes">> [[The sight of the cross repeals them]]
    <<else>>[[you're powerless to resist there charms]]<</if>>
    
  • edited May 2015
    Yeah I always use "to" and "is" so I missed that in my above example, sorry.

    But my second suggestion is still right. :p There's no need in your story to set to "no" except at story initialisation.

    In the example given, the <<set $cross to "no">> is not doing anything at all, as <<else>> will assume anything is false that isn't explicitly defined.
  • I just want to interject that inconsistent use of white space in code does NOT create new variables... White space is ignored in these cases.
  • edited May 2015
    Sometimes.

    You cannot write <<set $crossto 1>>, because you end up with the $crossto variable. It's still code, and the white space makes a considerable difference. ;)

    I put it out there because it's possible (without me knowing more), that Twine does some funky conversion of "=" to "to", resulting in mangled variables. (EDIT: Though I tested it just then and this is not the case for $cross=, though $crossto produces an error indicating it wants the white space.)

    It's always best to directly follow the story's example syntax, which includes white space, so I think that's still worth commenting on.
  • As stated by others, leaving spaces out in macros like <<set>> and <<if>> can cause issues.

    The <<set>> macro:
    It is a good idea to place a single space between the variable name and the assignment symbol (either = or to), and between the assignment symbol and the value:
    <<set $variable to "value">>
    <<set $variable = "value">>
    

    The <<if>> macro:
    It is a good idea to place a single space between the left part of a condition and the comparison symbol, and between the comparison symbol and the right part:
    <<if $variable is "value">>
    
    <<if $variable >= 99>>
    
    <<if $variable1 neq $variable2>>
    
  • Beyond what's already been said, when what you're doing is boolean logic (true/false, yes/no, on/off) then you should probably simply use booleans. If for no other reason than it simplifies all of your conditionals. For example:
    /* Initialize the $cross state to false at the start (best done in the StoryInit passage). */
    <<set $cross to false>>
    
    /* After playing a while, the player finds and picks up the cross, so set $cross to true. */
    <<set $cross to true>>
    
    /* Even later, the player has run afoul of some vampires, so now you test for the $cross. */
    <<if $cross>>[[The sight of the cross repels them]]\
    <<else>>[[You're powerless to resist their charms]]<</if>>
    
    Of course, in cases like that example, I like to prefix the variable names with "has" or "is", depending on the situation. For example:
    <<set $hasCross to false>>
    …
    <<set $hasCross to true>>
    …
    <<if $hasCross>>
    
    You get the idea.
  • But a space or no space before the = operator should not cause any issues, right?
  • edited May 2015
    Probably not. I was wrong in that assertion as I have noted. I just still consider it something worth cautioning against because with Twine macro syntax being shorthand for what's going on invisibly under the hood, who knows when it will break if you don't follow a macro creator's examples exactly.
  • timsamoff wrote: »
    But a space or no space before the = operator should not cause any issues, right?
    Correct. The JavaScript assignment operators do not consist of characters which are valid as part of an identifier, so they'll never be confused as part of one. For example, the following are all perfectly valid:
    <<set $cross=true>>    → OK
    <<set $cross =true>>   → OK
    <<set $cross= true>>   → OK
    <<set $cross = true>>  → OK
    
    On the other hand, since the TwineScript assignment operators do consist of characters which are valid as part of an identifier, more care has to be taken with them. For example:
    <<set $crosstotrue>>    → WRONG: Ambiguous, looks like a single story variable identifier
    <<set $cross totrue>>   → WRONG: Ambiguous and a syntax error
    <<set $crossto true>>   → WRONG: Ambiguous and a syntax error
    <<set $cross to true>>  → OK
    
Sign In or Register to comment.