Widget Variable Question

Hi there,

I’m new to this forum! Hi everyone. But have already a little Twine experience.
I’m using SugarCube and the latest Twine Version. Coding in Twee.
I have a question, but the question will be posed at the bottom of the post.
I created a widget, that writes a link. I wrote that widget-call several times in one passage.
I recognized, that the $args-variables are kept each indivudal for every widget-“call”. The local variables overwrote each other.

Example:

<<widget "WriteLink">>
<<set _a to $args[0]>> /*Thist is the link text*/
<<linkreplace _a>>Funny things happen, because you clicked _a<</linkreplace>>
<</widget>>

::Start
<<WriteLink "Click it">>
<<WriteLink "Click here">>

->Both Links will reveal “Funny things happen, because you clicked Click here” - which wasn’t my intent.
A better way to do it would be:

<<widget "WriteLink">>
<<linkreplace $args[0]>>Funny things happen, because you clicked $args[0]<</linkreplace>>
<</widget>>

->Both Links in ::Start will reveal different text.
It really amazed me, that the widget-“calls” keep there $args.

This is quite easy till now. But now I want to save the Link-Text into a object.

::Start
<<set $Item to [
{Text1: "Click it", Text2: "Not important"},
{Text1: "Click here", Text2:"Not important"}
]>>
<<WriteLink 0>>/*Aim: A link with "Click it"*/
<<WriteLink 1>>/*Aim: A link with "Click here"*/

<<widget "WriteLink">>
<<linkreplace $Item[$args[0]].Text1>>Funny things happen, because you clicked $Item[$args[0]].Text1<</linkreplace>>
<</widget>>

-> this widget will not work, for the text will be full of “object”

<<widget "WriteLink">>
<<set _a to $args[0]>> /*Thist is the link text*/
<<linkreplace $Iem[_a].Text1>>Funny things happen, because you clicked $Item[_a].Text1<</linkreplace>>
<</widget>>

-> This will neither work, the link will be fine, but the replace text not.

<<widget "WriteLink">>
<<set _a to $args[0]>> /*Thist is the link text*/
<<linkreplace $Item[_a].Text1>><<set _a to $args[0]>> Funny things happen, because you clicked $Item[_a].Text1<</linkreplace>>
<</widget>>

-> This will work.

Now my questions. They are two.

  1. Why is the strange object-thing happening? ($Item[$args[0]].Text1)
    The object-stuff will occure already in the linktext if it would be in “”:
<<widget "WriteLink">>
<<linkreplace "$Item[$args[0]].Text1">>Funny things happen, because you clicked $Item[$args[0]].Text1<</linkreplace>>
<</widget>>
  1. Is my solution solid? It feels not like good coding style.

I suggest reading the Passing an expression as an argument section of Macro Arguments. It explains how to you would use back-quotes to force your initial $Item[$args[0]].Text1 argument expression to be evaluated before the resulting value is passed as an argument to your <<linkreplace>> macro call.

You may also need to wrap the instance of $Item[$args[0]].Text1 you are trying to output the value of within a <<print>> related macro.
(untested example)

<<widget "WriteLink">>
<<linkreplace `$Item[$args[0]].Text1`>>Funny things happen, because you clicked <<= $Item[$args[0]].Text1>><</linkreplace>>
<</widget>>

Thanks a lot! (Especially for reading my long post)

You may also need to wrap the instance of $Item[$args[0]].Text1 you are trying to output the value of within a <<print>> related macro.

Until now it worked out fine. What it is depending on if it is neccessary? Is it browser depending?

It’s because SugarCube can only handle some “naked variables”, because in some cases it’s not easy to tell them apart from normal text. While SugarCube will recognize $Item[$args[0]] as a variable, it won’t recognize all of $Item[$args[0]].Text1, because it assumes that the period and everything after it is part of the text, not part of the variable. For anything like that you’ll need to use the <<print>> macro or one of its aliases, like <<=>>.

Thanks. (Took me a while to answer, sorry, but better late than never).

You may also need to wrap the instance of $Item[$args[0]].Text1 you are trying to output the value of within a <<print>> related macro.
(untested example)

This is right - in my previous post I thought I had an example in my story where it worked without the <> but it doesn’t. It’s needed in that case.