Setting a different variable with one link depending on an independent variable

Twine Version: 2.8.1.0 Sugarcube Version: 2.36.1

I am trying to find a way to set a different variable, well actually a different object property in this specific case, e.g. $foo.a, $foo.b, to a constant value depending on the state of a separate variable. For example, if $dynamicvar is set to the value “a” then I would want to set $foo.a to 1, and if $dynamicvar had the value “b” I’d want to set $foo.b to 1. Is there any elegant way to do this short of writing out if statements for each case?

For the full context of why I want to do this keep reading but that’s the gist of it.

I am trying to make a puzzle game where the main puzzle mechanic is a tile based puzzle with tiles you select from a dialog window, and then click on a “slot” on the page to change the slots value and image to the ones associated with that tile. I’ve been able to accomplish certain parts of this system already, particularly the setting of a slots value to the tile you clicked last, done through setting a var $hold to the tiles value, and then setting the slots var to $hold, and $hold back to its original state, and changing the image dynamically using a variable like, [img[$img1]],
however I am stuck on how to tell which piece to “give back” to the player. Any help on this topic would be greatly appreciated.

The <<if>> family of macros can be used to conditionally show content or execute macros.

<<if $dynamicvar is "a">>
	<<set $foo.a to 1>>
<<elseif $dynamicvar is "b">>
	<<set $foo.b to 1>>
<</if>>

And the above code can be placed within the body of a <<link>> macro so that the condition check is done when a specific link is selected.

<<link "Select Me">>
	<<if $dynamicvar is "a">>
		<<set $foo.a to 1>>
	<<elseif $dynamicvar is "b">>
		<<set $foo.b to 1>>
	<</if>>
<</link>>

note: if the String value of the $dynamicvar variable is always the same as one of the property names on the Object being stored in the $foo variable, then it may be possible to use a combination of the $dynamicvar variable’s value and Bracket Notation to update the relevant property of $foo
eg,

<<set $foo[$dynamicvar] = 1>>

…which can also be executed within the body of a <<link>> macro call…

<<link "Select Me">>
	<<set $foo[$dynamicvar] = 1>>
<</link>>

Hello, thank you very much for the quick response!
When I try to implement that into my code using this image link:

<<link [img[$testtwo.img1][testmuralone]]>><<set $testtwo.piece[$testtwo.slotone]; $testtwo.slottwo to "z"; $testtwo.hold to "z">><</link>>

I get an error reading:

"<<set>>: Bad evaluation, cannot read properties of undefined (reading 'a').

Any idea why this might be happening?

Ah wait, I understand.
The bracket syntax is adding a dot character so its reading as $testtwo.piece.a or $testtwo.piece.b.
I ended up nesting a second object into the first, this is actually a cleaner way to do it in the long run anyways, I really appreciate the time taken to help and have a good day!

If you just want to access piecea you can do

<<set $testtwo['piece' + $testone.slotone] to 1>>

Actually no, it’s the other way around.

Bracket Notation existed as a syntax for access elements of a collection related object before Dot Notation became a thing, even though Dot Notation is more commonly used these days when accessing the properties of an Object.

So basically person.name is shorthand for person['name']

1 Like

Oh wow that’s very interesting! It’s always neat to see how more commonly used syntax has developed compared to the same line of code before that syntax was added. :smiley:

Thanks for the additional solution, I’ll likely use it in future if I’m in the same situation and making a property into a sub-object is less desirable!