How To Make a Variable the Sum of Two Other Variables

Using Twine 2.7.0 and Harlowe


I am brand new to this, wanted to make IF for my son. I’m having a fun time but I cannot find an answer to this question.

In this game the player will have health and defense. I want the defense to be the sum of two other variables, Shield and Armor. I defined what Shield and Armor are; (set:$Armor to 10) (set: $Shield to 10) and I then wrote the code as (set: $Defense to $Shield + $Armor) expecting a value of 20, but it returns a value of 0. Can anyone help me understand why?

Thank you!

That code works for me. But it’s not like a spreadsheet: $Defense won’t update every time you change $Shield or $Armor. It just freezes the value at the time when you did the (set: $Defense ...).

Harlowe treats unset or unknown variables as 0, so my first guess would be that maybe you set $Defense first? Or that you have typos in the variable names?


You were correct, I had set my $Defense first. I’ll have to remember that Order of Operations is important! Also good to know it won’t automatically check to see if the $Shield and $Armor values have changed and adjust accordingly; I think I solved that by just setting the Shield and Armor again on the page I knew the player would get a new set of armor, and had it add the values together again on that page.

Thank you so much!