Is it possible to put a link and a variable in an object key/entry

Twine Version: 2.92 Sugarcube

Hi all,

Can you put a link that sets a variable into an object key?

I know you can just do a link with the double square brackets [[“Link” |Destination Passage]] but I want it to set a variable when the link is clicked as well

<<set $object1 to {
                  name: "Chair",
                  link: "[["Chair Description"| Item Description Passage]] ",
                  variable: "<<set $variable to 1 >>" 
                  }


Something like the below (but it throws up an error when I try it):

<<set $chair1 to {
                 name: "Chair",
                 link: "<<link"Chair Description" "Item Description Passage">><<set $variable to 1>><</link>>   "
                 }>>

Thank you!

My guess is that the quotation marks are the problem, which IIRC you can get around by nesting single quotes inside double quotes or vice versa, but in this case I would just use the link markup with setter:

<<set $chair1 to {
                 name: "Chair",
                 link: "[[Chair description][$variable to 1]]"
                 }>>

(You don’t need quotes with the square bracket link markup, btw.)

I tested the above and it worked fine.

1 Like

Emery Joyce has already given you a potential solution, but I wanted to explain about using single or double quotes inside String literal values.

If you need to use the String delimiting quote-type inside itself the String value, like…

'some string with a single-quote ' character inside it'
"some string with a double-quote " character inside it"

…then you need to either:

1: use the other quote-type as the String delimiter

"some string with a single-quote ' character inside it"
'some string with a double-quote " character inside it'

2: use a \ back-slash character to escape the internal copy of the delimiting quote-type

'some string with a single-quote \' character inside it'
"some string with a double-quote \" character inside it"

So your link String examples need to look more like…

'[["Chair Description"|Item Description Passage]]'

and

'<<link "Chair Description" "Item Description Passage">><<set $variable to 1>><</link>>'

note: the 1st of the above would still not achieve the outcome you’re looking for.

1 Like

Thank you both! That’s very helpful.