Howdy, Stranger!

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

IF THEN statements in Harlowe

I'm trying to make the leap from Twine 1.0 Sugarcube to Twine 2.0 Harlowe and I'm struggling with variables.

I'm trying to build a version of the old Kingdom or Hamurabi games which require variable manipulation. I know how this is done in Sugarcube but I'm struggling in Harlowe.

I've set the variables likes this:

(set: $money to 40)
(set: $resp to 20)

Money: $money

Respectablility: $resp

test variables

And then go on to test this on the next page:


Repectability: $resp

Money: $money

(if: $money > $resp) [You are getting more respectable (set: $resp to $resp + $money)]
(if: $money < $resp) [You are getting less respectable (set: $resp to $resp - $money)]
(else: ) [Your respectability is the same]

Your new Respectability is $resp

I have also tried the following for the third line:

(if: $money = $resp)
(if $money is $resp)

It works where $money is less than $resp but not the other way around. The lines of code should do an identical job as far as I can see but they don't.

So two novice questions:

* Am I right in thinking that the square brackets are used to contain the expression? In other words, this is the code that should be run if the condition is met?

*Is the problem something to do with placement of lines?

Any help massively appreciated!

Carl.

Comments

  • The square brackets (known in Harlowe as a hook) contain the "code" to execute if the condition is met, yes.

    Your problem is that your second (if:) should be an (elseif:) instead. For example:
    (if: $money > $resp) [You are getting more respectable (set: $resp to $resp + $money)]
    (elseif: $money < $resp) [You are getting less respectable (set: $resp to $resp - $money)]
    (else:) [Your respectability is the same]
    

    Also, when modifying a variable's current value, you may replace instances of the variable after the first with the keyword it. For example:
    <!-- These (set:) macros. -->
    (set: $resp to $resp + $money)
    (set: $resp to $resp - $money)
    
    <!-- May also be written like so. -->
    (set: $resp to it + $money)
    (set: $resp to it - $money)
    
    You don't have to do that, but it does save some typing.
  • edited January 2016
    opps: it appears TheMadExile types faster than I do. lol

    Please use the C button in the comment area's toolbar to wrap your code examples in code tags.
    (if: $money = $resp)
    (if $money is $resp)
    There are a number of errors/issues with the above:

    a. You are missing the requires colon after the macro name in the second line, so that line will output the values of the $money and $resp variables.

    b. A single equals sign (=) means assignment in Javascript (and Harlowe script), you use either double (==) or triple (===) equal signs when doing a comparison.

    c. Harlowe does not support assigning values to variables within the conditional part of the (if:) macro.

    d. Harlowe does not support using double (==) or triple (===) equal signs when doing a comparison in an (if:) macro, it uses the is keyword operator.

    e. Based on all the above the two lines are not equivalent.

    There are a number of errors/issue within your test variables passage:
    Repectability: $resp

    Money: $money

    (if: $money > $resp) [You are getting more respectable (set: $resp to $resp + $money)]
    (if: $money < $resp) [You are getting less respectable (set: $resp to $resp - $money)]
    (else: ) [Your respectability is the same]

    Your new Respectability is $resp
    1. You have an Invalid space character between your (if:) macro's and their assocated hooks [...]

    2. Because the second (if:) macro is not an (else-if:) the two (if:)'s are considered mutually exclusive, and because you are changing the value of $resp in the first (if:) the condition in both (if:) macros end up being true.

    a. $money (40) is greater than $resp (20) so $rest is changed to equal 60 (20+40)
    b. $money (40) is less than $resp (60) so $rest is changed to equal 20 (60-40)

    3. The (else:) macro is only associated with the second (if:) macro.

    4. (not an error as such) You have an unnecessary space character in the (else: ) macro.

    Your example should look something like the following:
    Repectability: $resp
    
    Money: $money
    
    (if: $money > $resp)[You are getting more respectable (set: $resp to $resp + $money)]
    (else-if: $money < $resp)[You are getting less respectable (set: $resp to $resp - $money)]
    (else:)[Your respectability is the same]
    
    Your new Respectability is $resp
    
  • Thanks both of you.

    I clearly have a lot to learn!

    Back to experimenting and feeling my way through the docs I guess.

    Thanks again.
  • Ah, I missed the spurious spaces between the macros and their hooks. Good catch, greyelf. I should have been paying more attention.
Sign In or Register to comment.