0 votes
by (380 points)
edited by

I would like to be able to reference a specific property of an object variable based on the value of another variable.

Here is a simple example of this. I was hoping that the latter <<print>> would match the former, but it doesn't, presumably because the variable is read as "$c" rather than the value of $c:

<<set $a to { b : 1 },  $c = "b">>

<<print $a.b>>
<<print $a.$c>>

 I was intending to use this to implement a wardrobe system where a widget is used to change clothes.

<<widget "wardrobe">>
    <<if $wardrobe.$args[0].state == 0>>
    <<elseif $wardrobe.$args[0].state == 1>>
        <<if $character.clothes == $args[0]>>
You are wearing the $wardrobe.$args[0].name.
        <<else>>
[[$wardrobe.$args[0].name|Wardrobe][$character.clothes to $args[0]]]>>
        <</if>>
    <</if>>
<</widget>>

With <<widget "dress1">>, for example, finding properties of 'wardrobe.dress1'.

I can do a lengthy workaround with multiple arrays containing the names, ID's and states of the clothes separately, but that seems less elegant. Is there a way of doing it in this way? 

1 Answer

0 votes
by (380 points)
selected by
 
Best answer

<<widget "wardrobe">>\
    <<if $wardrobe[$args[0]].state == 0>>\
    <<elseif $wardrobe[$args[0]].state == 1>>\
        <<if $character.clothes == $args>>\
You are wearing the <<print $wardrobe[$args[0]].name>> .
        <<else>>\
<<link $wardrobe[$args[0]].name "Wardrobe">><<set $character.clothes to $args[0]>><</link>>
        <</if>>
    <</if>>
<</widget>>

I seem to have managed to get it to work using bracket notation. But if this seems wrong or if there are any glaring mistakes, please feel free to correct them.

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