Howdy, Stranger!

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

Is it possible to set a variable to true within a hook?

Hi. I'm a new user of Twine, and haven't written any code before in my whole life. I'm using Harlow. I'm trying to make it so that a variable changes to true from false, once you click the choice, to avoid having an extra passage in between. I don't know the vocabulary well enough to describe it better than that. The code i'm trying to use is this:

(if:$stumbledlikeafool is true)[Grab a candle and head on back to the church and go down in the cellar->Go down in the cellar(set:$haslight3 to true)]

Is it possible to do something like this? Make haslight3 true,without having to use an extra passage? What happens now, is that it becomes true once you read this passage, either you click the option or not. I'll post a screen cap, in the hope that somebody answers my question. Thank you.

Comments

  • edited December 2016
    The 'correct' way to check the value of a Boolean (true/fale) variable using an (if:) macro is:
    to check for true
    
    (if: $stumbledlikeafool)[....]
    
    to check for false
    
    (if: not $checkedsaw)[....]
    


    You can use a (link:) macro to do what you asked:
    (if: $stumbledlikeafool)[\
    (link: "Grab a candle and head on back to the church and go down in the cellar")[\
    (set: $haslight3 to true)\
    ]\
    ]
    
    note: I added backslash character based line-break escaping to the above to make it more readable, they are not needed for the technique to work.


    warning: Changing the value of a variable after the current passage has been displayed/rendered will not result in the re-execution/re-processing of any conditional based macro that reference the change variable.
    eg. If you have code like the following:
    (set: $var to false)
    
    (link: "Click Me")[
    	(set: $var to true)
    ]
    
    (if: $var)[The value of var is true]
    
    ... the "The value of var is true" text will not automatically appear once the "Click Me" link is clicked.
  • Thanks for answering. I think I understand some of what you're trying to explain, but not all of it. I'm sorry, but all this is very new to me.

    So, if I understand correctly, you don't NEED the "is true" part, in the (if:) macro? And instead of checking for "is false", you just type in "not" in front of the boolean? I suppose that means that you don't really ever need to set any variable to false, since it's false if not true? Not knowing any better, I've set every boolean to "is false" in the first passage of the story:)

    And then you put a left bracket between each of the parentheses, in order to make "haslight3" true, only if you click the link "grab a candle..."?

    The last part of your answer, I don't really understand.
  • edited December 2016
    It didn't work. I'm sure it's my fault, I think you know what you're talking about. I solved it by adding an extra passage for now, but your way seems far better.
  • In many languages "if smth then" is used as a short for "check if this smth exists and is true, then do the following:", and Harlowe is no exception (cause it's using JS afaik). so the whole (if: $stumbledlikeafool)[....] is like saying "if this variable exists and is true" (and actually some other if's). If you say (if: not $stumbledlikeafool) that would stand for "if not (this variable exists and is true)" or simply "if this variable either not exists or not true (false). You may read some basic boolean logic on wiki I guess.

    Btw if you won't preset the variables to false this greyelf's code would also work fine because these variable would not exist :)

    What comes to the brackets part... Let me try to explain it all together.
    would use # for commentary indication
    (if: $stumbledlikeafool)[
    # this is the if part, all the other things inside the [] would happen only if this check passes
    (link: "Grab a candle and head on back to the church and go down in the cellar")
    # this creates a clickable link, which has the text and on click initiates this hook:
    [ # opening of the link: hook
    (set: $haslight3 to true) 
    # setting a variable after you click the link
    ] # closing of the link: 
    ] # closing for if
    

    The last part was about the processing of the page.
    F.E.:
    (set: $var to 0)
    $var
    (link: "click this")[ (set:$var to 1) ]
    $var
    

    No matter you click the link or not, both $var lines would print 0, because the page is first processed and shown, and your changes after clicking would not apply until you revisit the page. To bypass this you'd need to use named hooks, but that's a different story.

    Hope this helps. Just visited the forum and thought I might be of help.

  • A variable can store many different data types, some of them are: String (text), Number, Date, and Boolean. The Boolean data type consists of two values: true and false

    When you write code like (if: $variable is "value") the $variable is "value" part is known as a (conditional) expression and all expressions are either true or false.

    eg. either it is true that $variable is (equal to) "value" or it is false

    This is why you don't need to include the is true or the is false part of the expression when checking a Boolean value/variable, because they already equal either true or false.

    Generally you should assign an initial/default value to all your variables at the start of your story, preferably within a startup tagged passage, because in Harlowe any variable you don't assign a default value to will equal 0 (zero).
Sign In or Register to comment.