Making a variable the sum of two other variables

Twine Version: 2.3.16
Story Format: Sugarcube 2.36.1

I jus have a quick question, and hopefully an equally quick solution. Like it says in the title, I am trying to create a variable which is supposed to be the sum of two other variables. The problem is, it comes back all wrong. Instead of adding the values of the other variables, it just displays text. I’ve included an example below:

<<set $AppBase to 5>>
<<set $AppBonus to 0>>
<<set $Appearance to "AppBase" + "AppBonus">>

Instead of telling me the value is 5 when I print it, it tells me the value is AppBaseAppBonus. I’ve looked through the documentation for sugarcube but didn’t find anything that I could interpret as useful for my dilemma. Thanks in advance.


When you’re using quotes like that, you’re setting the $Appearance variable to a string of text made up of two shorter strings, each contained between a set of quotes (you are not referencing your other variables). To set $Appearance to the sum of values, do this instead:

<<set $Appearance to $AppBase + $AppBonus>>

1 Like

I really thought I tried both with and without quotation, but it works. Thank you very much.

Sugarcube will not read something like a variable without $ (story variable) or _ (temporary variable). So even without quotations,

<<set $AppBase to 5>>
<<set $AppBonus to 1>>
<<set $Appearance to AppBase + AppBonus>>

would result in an error, as AppBase and AppBonus are “not defined”. Even if the variables had been defined in JavaScript, they would still have to be translated into story or temp variables that can be read by the <<set>> macro.

That was my mistake in the example code, because I do have $-signs on all my variables in Twine. I didn’t copy/paste from my project, but merely posted an example to showcase my dilemma. Anyway, I got the answer to my question, and the problem has been solved. It works really nicely. Thank you guys :wink:

1 Like