Howdy, Stranger!

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

[NOOB] Twine Variable Math (Basic)

edited July 2014 in Help! with 1.x
Okay, I'm new to twine and I've been playing around with some of the functions so that I can start creating a game with everything I want in it, but I can't figure out how to do math with the variables? It's better to show than explain:

<<set $str to 0>>
<<set $mag to 0>>
<<set $gold to 0>>

[[attack monsters for gold]]
<<set $str to += 5>>
<<set $gold to += 10>>

<<print $str>>
<<print $mag>>
<<print $gold>>

All of these show up as zeros!
I've tried many different things such as:

<<set $str = $str + 5>>
<<set $str += 5>>
<<set $str to ($str + 5)>>

But when I print it onto the screen, they appear as zeros. This is a very basic concept and I will be unable to create my game without it! I searched the forum for around two hours and found nothing. (Probably because it's so easy) Please help!


FIX: Right after I posted this, I found that the problem was the <<"set">>, the correct line of code for this is <<$str +=5>> I apologize for any time wasted. Although, I may have just helped a bunch of people who didn't understand this like I did.

Comments

  • Zayuz wrote:

    <<set $str += 5>>
    This is the recommended formulation.
    Zayuz wrote:

    <<set $str = $str + 5>>
    <<set $str to ($str + 5)>>
    These should also work (but using = instead of 'to' isn't strictly recommended), and I'm not sure why they don't.
    Zayuz wrote:
    <<set $str to += 5>>
    This is wrong, and IMHO the next Twine version ought to warn about this incorrect formulation.

    Zayuz wrote:
    FIX: Right after I posted this, I found that the problem was the <<"set">>, the correct line of code for this is <<$str +=5>> I apologize for any time wasted. Although, I may have just helped a bunch of people who didn't understand this like I did.

    This is actually the <<print>> macro's shorthand form. What you should note is that <<set>> changes values without printing them, and <<print>> prints values while either changing them or leaving them as they are.
Sign In or Register to comment.