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

Hello,

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?

Peppermint

by (44.7k points)
selected

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>> ``

with:

``<<set \$coin++>> ``

or:

``<<set ++\$coin>> ``

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

by (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

*waves*

Peppermint
by (1.1k 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>>``

``<<set \$coin to 0>>``
{"ft_lang_label":"__label__en","ft_lang_prob":0.97873884,"math_prob":0.5895386,"size":699,"snap":"2022-27-2022-33","text_gpt3_token_len":173,"char_repetition_ratio":0.09352518,"word_repetition_ratio":0.0,"special_character_ratio":0.2746781,"punctuation_ratio":0.16363636,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98334783,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-29T18:25:27Z\",\"WARC-Record-ID\":\"<urn:uuid:95874095-895e-4138-a3fa-c6de8de17f0f>\",\"Content-Length\":\"38635\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fb9fad68-2ed1-47d6-a1d5-30bdd3fe38dd>\",\"WARC-Concurrent-To\":\"<urn:uuid:0e9176d1-a700-48f9-a767-c4bf06b2e204>\",\"WARC-IP-Address\":\"23.92.16.172\",\"WARC-Target-URI\":\"https://twinery.org/questions/19500/counting-with-set-macro-but-value-another-value-make-result\",\"WARC-Payload-Digest\":\"sha1:GSQA67STOYKOHIPFP6AUKCFUFXHPP6IR\",\"WARC-Block-Digest\":\"sha1:IRMA6PJ6XYOCHZSOTSBCOB6ASG5J3EKW\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103642979.38_warc_CC-MAIN-20220629180939-20220629210939-00268.warc.gz\"}"}