Howdy, Stranger!

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

Adding variables together in Harlowe

I haven't been able to find anything already in the forum or the internet to answer my question, so I'm sorry if this has already been answered. I also don't know if this is even possible--but that's why I'm asking.

What I'm aiming to do is add two numerical variables together to create a third variable. An example of how I'm trying to make it work: there will be both strength ($str) and constitution ($con) checks throughout the story, and have both of these influence the reader's health ($health = $str + $con). As the player progresses through my tale, they will have the opportunity to increase these base attributes and thus their maximum health.

I have tried several combinations that I thought would work, from ($health eq ($con+$str)) to attempting to create a dataset. The best result that I've achieved thus far is (set: $health to ($con+$str)), which Twine produces 0 (even though I have both $con and $str defined earlier in the "init" passage).

Is this even possible with Harlowe? If not, I'll come up with a different method of determining health and the like. If, on the other hand, it is possible and I just overlooked the method, would someone be willing to point me in the right direction?

Comments

  • Harlowe does not have an 'init' special passage, instead you can use a startup taggged passage to initialize (assign default value) variables before the first passage is shown:
    (set: $str to 18)
    (set: $con to 10)
    
    Harlowe does not support the eq operator, it uses the to operator for assignment.
    The following shows how to add two numerical values together:
    (set: $health to $con + $str)
    
    str: $str
    con: $con
    health: $health
    
    The following shows how to use the is operator to compare two values for equality:
    (if: $str is 18)[Strength is equal to 18]
    
  • Before posting my initial post, I did try

    (set: $health to ($con + $str))

    with different styles (plus/minus spaces around the plus sign, with and without the parentheses, ect) in the first passage of my Twine story. The initial values were at zero because the reader's first choice was what gender they were, and values would change based on that. (I know, I'm being sexist. Oh well...) The passage after choosing gender shows the different values.

    In other words, my story looks like this (while simplified greatly)...

    Starting passage ($str and $con set at 0; $health=$con+str)
    Passage where player chooses gender (for male characters, $str set at 12, $con set at 8; for female characters, $str set at 8, $con set at 12; no mention of $health)
    Passage to make and see changes

    When I test the story at this point, the third passage says that the player's health equals 0. Does that mean that I need to update the passage every time that a character's strength or constitution increases/decreases?

    When I next get the chance, I'll try setting all the initial code from my starting passage into a startup-tagged passage and see if that resolves the issue.
  • The attached Archive HTML file includes a demo story that does something similar to what you described. The story includes:

    a. A startup tagged passage (named Startup) that initializes the story's variables.

    b. A First passage that asks the gender question.

    c. A Second passage the displays the outcomes of the gender selection.

    d. The CSS (in the Story Stylesheet area) needed to hide the output of the startup tagged passage.

    Download the archive file and use Twine's Import From File option to add it's contents to your Story List.
  • Thanks! I... think I understand everything that's going on there. Maybe not fully, but enough that I can replicate it. Again, thanks!
  • The forum software eats attached files when you mark a comment as an Answer, so here is another copy of the Archive file.
  • for some reason i cant "ask a question" so im forced to ask here

    My test code is:
    {(set: $target="12")
    (set: $current="$num1"+"$num2")
    (set: $num1 = "1")
    (set: $num2 = "2")
    (set: $test = "12")
    
    $target = $current?
    (if: $target is $current)[
    YES]
    (else:)[NO]
    <br>
    
    $target = $test?
    (if: $target is $test)[
    YES]
    (else:)[NO]
    }
    

    Result:
    12 = 12 ? NO
    12 = 12 ? YES

    Debug says:
    12 $target = 1 $num1 2 $num2 $current
    12 $target = 12 $test

    soooo... Problem is obvious. But how can I solve it?
Sign In or Register to comment.