Howdy, Stranger!

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

How to give a variable value by adding multiple other variables?

Hello!

I'm trying to create a way for one variable to be the sum of several other variables, and the old internet search didn't come up with an answer that worked for me. Any advice is greatly appreciated!

Example that shows my intention, but is flawed coding/not resulting in the total addition of all variables equalling the $totalsum variable:

<<set $totalsum = $variable1 + $variable2 + $variable3 + $variable4>>

Thank you for any help you can offer! :)

Comments

  • Try this:
    <<set $totalsum = ($variable1 + $variable2 + $variable3 + $variable4)>>
  • Crowbeak wrote:

    Try this:
    <<set $totalsum = ($variable1 + $variable2 + $variable3 + $variable4)>>


    Sometimes you're just missing a couple of brackets. Works as intended! Thank you :)
  • I'm kind of perplexed why it wouldn't work without the brackets, though. It's all very odd. Humour a coder and maybe describe what the values being added were?


  • The plot thickens, actually.

    I thought my problem was the missing brackets. TURNS OUT I had confused  "eq" with "=".

    I did a test just now by removing the brackets, and the equation still works as intended.

    In my code, I had set up the values I wanted to add together using "eq" instead of "=", so none of them were working.
  • Whenever I leave Twine for a month or two and come back I forget the difference between = and == and my if conditionals end up setting variables.
    The fact that <<if $thing = true>> actually equates to <<set $thing as true>> I find really hard to remember for some reason.
    I considered tattooing 'set = if ==' somewhere to my person but instead I plan to pummel myself over the head with this book in the hope that something gets through in osmosis: http://www.amazon.co.uk/Python-Kids-Playful-Introduction-Programming/dp/1593274076

  • In all honesty, there's little reason to ever use "=", "==" or "eq" in a Twine version that contains "is" and "to". I encourage avoiding the former three and exclusively using the latter two.
Sign In or Register to comment.