Counting with a SET Macro, but how... (add a value and another value and make it a new result)

0 votes
asked Jul 3 by MrPeppermint (950 points)


It's been quite a while since I had a question, I have been pausing, too... Now I am trying to continue what i was working on and I ran into another problem and I hope someone may know the answer.

I am trying to collect coins... The StoryInit has this in it as a starting point:

<<set $coin to "0">>

... now in further passages, I am making my character find coins or earn them ... therefore it tried this:

<<set $coin += 1>> 

The problem is that it makes the print out of the variable be 01 and then in later passages 011 and so on...

But I want it to really count like 1 and in the later passage 2 and then 3 and so on, anyone knows what I am missing here?

Thank you in advance...


3 Answers

0 votes
answered Jul 3 by HiEv (7,020 points)
selected Jul 4 by MrPeppermint
Best answer

The problem is that:

<<set $coin to "0">>

sets $coin to a string with the character "0" in it.  You want to set $coin to a number instead do this:

<<set $coin to 0>>

With the quote marks around the zero it becomes a string, instead of a number.  That's why when you added other things to it, it assumed you wanted to concatenate strings, instead of add numbers.

Also, if you want, you can replace:

<<set $coin += 1>> 


<<set $coin++>> 


<<set ++$coin>> 

both of which will add 1 to $coin (see here for details on ++ and an explanation of the difference between the two).

commented Jul 4 by MrPeppermint (950 points)
Hello HiEv,

thank you so much, it was the ++ command that did exactly what I was hoping for. :)

Thanks a lot. Somehow strange but the ++ is nowhere mentioned where I looked at. Glad this place is there to ask questions and find people that know the program a lot better. :)

Thank you also to everyone else <3


0 votes
answered Jul 3 by JoshGrams (1,020 points)

Putting quotes around the 0 tells sugarcube that you want the value to be a string.  So when you add 1 to it, the 1 gets converted to a string: you're adding "0" and "1" to get "01", then "01" and "1" to get "011".  If you want it to be a number, use

<<set $coin to 0>>


0 votes
answered Jul 3 by TheMadExile (46,560 points)

Your problem is that you're initializing $coin as a string, rather than a number.  Try the following instead:

<<set $coin to 0>>


Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.