Variables concatenation?

Twine Version: 2.6.2
SugarCube 2.36.1

Hi all!

I have an idea on how to make my life easier, but I can’t make it work.

I have defined $peterhappiness and $chrishappiness and so on,
And I use this flag: $char

I want to increase $($char)happiness, based on my flag $char
And don’t want to make if/elseif statements, because I have lots of characters ($char), and I feel like this, I can make my life easier.

I tried:
($char = peter) and ($peterhappiness = 5)

<<set $xhapp to 0>>
<<set $xhapp = “$” + $char +“happiness”>>

<<print $xhapp>> //some how it prints 5 (me happy)
<<set $xhapp += 5>>
<<print $xhapp>> //some how it prints $peterhappiness5 (string, me not happy because its not number 10)

and after this some how I need to assign that 10 I was expecting to the $($char)happiness

I hope some one can help me, thanks!

You want the State.variables object, like this:

<<set State.variables[$char + "happiness"] = 5>>
1 Like

Thank you sir! It’s working. You just saved me from a lot of work.
Thanks again!

You could also use objects instead:

$char = {happiness : 0, otherstat : 6}

To do afterwards

<<set $char.happiness +=5>>
<<set $var to $char.happiness>>