Howdy, Stranger!

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

Newbie Can't Make Variables Work

Sorry for this. I feel like a fool. I've been using Twine for less than a few weeks and I've primarily used it for interactive stories, but now I'm trying to do different kinds of games and I feel really silly.
I don't understand the rules of variables?
I know the whole thing from the neocities guide:
(if: $legs is 8)[You're a spider!] will show the You're a spider! hook if $legs is 8. Otherwise, it is not run.
But I can't understand how to phrase else-if and things like that. Sorry if this isn't clear enough. Can someone just... Hold my hand and explain plainly how to use a dang variable. How to use else-if and either and the other things.

Comments

  • edited November 2016
    Don't know what use this will be, as it would appear from the above that you're not using Sugarcube.

    However, I will persist and tell you how I do it.

    I create a passage called StoryInit, add any variable I'm going to use in there, and set them to false. For instance:
    <<set $hasWood to false>>
    

    Then in the passage where the variable is used I would say:
    <<link "light fire">><<if $hasWood>>The fire is lit<</link>><<else>>You don't have any wood.
    
  • edited November 2016
    Hey, don't apologize. That's what we're here to help with! From the top, the whole shebang:
    1. Syntax

    In brief, variables are basically placeholders for a value. This value can be either a number, like 1, or a string, like "hat".

    All variables are called in your code by a string of characters beginning with $. $legs, $name, $have_sword? are all examples of a variable.
    Note that until you use the (set:) macro to define the value of a variable, it has the value 0 by default. If we enter "$name" in a blank passage, until we define it, it'll just print "0". After you define it, you can use your variable in passage text as a placeholder for the value it contains.
    My name is $name, and I am wearing $hats hats.
    

    2. The (set:) macro
    Used for setting the contents of variables!
    (set: $variable to value)
    

    Setting a variable to a number or a string:
    (set: $hats to 3), (set: $name to "Emily")
    

    Incrementing the value of a variable:
    (set: $hats to $hats + 1)    OR    (set: $hats to it + 1)
    

    Setting the value of one variable to the value of another value:
    (set: $ham to $eggs)
    

    3. The (if:) macro
    (if: $variable is value)[do the stuff in here]
    

    Slightly more complex. (if:) checks to see if the condition you put inside it is true or not. This is done with these logical operators. Then, if the condition you gave it is true, it displays/does whatever you put in brackets next to it. Here're some examples:
    (if: $arrows >= 1)[You pluck an arrow from your quiver and fire it into the enemy ranks.]
    
    (if: $arrows is not >= 1)[You reach for an arrow, but you're all out. How embarrassing!]
    
    (if: $arrows >= 1)[You shoot an arrow. (set: $arrows to it - 1)]
    

    As you can see in the last example, besides displaying text in the brackets, we can also nest additional macros inside of the first.

    4. The (elseif:) and (else:) macros
    (if: $variable is value)[do the stuff in here](elseif: $variable is this value instead)[do this stuff instead]
    
    (elseif:) can perhaps be more clearly read as "otherwise-if." It's very similar to (if:), but with one additional caveat: unlike (if:), which only cares whether or not its condition is true, (elseif:) goes up against the brackets of a previous (if:) or (elseif:), and also checks to see if it that one was true or not.

    Say we want to expand on our earlier arrow-shooting example-- an (elseif:) will let us tell the game what to do if we're out of arrows.
    (if: $arrows >= 1)[You shoot an arrow.(set: $arrows to it - 1)](elseif: $arrows is 0)[You're all out of arrows.]
    

    (else:) is very similar to (elseif:), but it doesn't check for its own condition. It can be read as "otherwise". It's usually used at the very end of a list of checks using (if:) and (elseif:) as sort of a "none of the above" condition.
    "What have I got in my pocket?" you ask. (if: $guess is 1)["Handses!"](elseif: $guess is 2)["Knife!"](else:)["String, or nothing!"] cries Gollum.
    

    5. The (either:) macro
    (either: "this", "this", "this", "or this")
    
    Compared to the previous macros, this one's really easy-- I'll just quote the Neocities documentation:
    Give this macro several values, separated by commas, and it will pick and return one of them randomly.

    Example usage:

    A (either: "slimy", "goopy", "slippery") puddle will randomly be "A slimy puddle", "A goopy puddle" or "A slippery puddle".
    Nore that you can also use it inside other macros, like (set:), to generate random values that you can then save long-term, like so:
    (set: $talent to (either: "Hunting", "Fishing", "Playing cards"))
    
    All of the values have an equal chance of being picked.
    Hope this helps-- and if I can help to further clarify my wall of text here, just ask!

    I've attached a simple working example of several of the (if:)/(elseif:)/(else:) macros in use-- just right-click the attachment and hit "save as" to download it. Then you can import it in Twine and play around with the code.


  • Thank you both so much!
Sign In or Register to comment.