So i have a question. I have some places with differents maps with a different widgets. And now i wanna merge all widgets to one (if it possible).
For example, I set variable $loc_home and set map inside and set $loc_street. Its two differents maps. Next, i wanna making one widget, who will take arg and set it as a variable. For the arg i wanna use passage() for taking passage name and use it in condition for forming map on page. The name of map variables and passages names are equivalent
If I understand correctly you have two ‘location’ related Passages named loc_home and loc_street, and the meta data associated with those two Passages are contain within the $loc_home and $loc_street Story Variables respectively.
You will need to use the State.variables collection object to gain access to the data contain within the $loc_home and $loc_street Story Variables, using the passed in Passage Name argument to indicate which ‘Story Variable’ you want the data of.
<<set _loc = State.variables[$args]>>
The following code shows a possible implementation of a ‘map’ widget containing your second code example.
warning: The above code has NOT been tested, as I was unsure of the exact contents of the Story Variables you referenced in your examples. It also doesn’t check to make sure that the Passage Name being passed into the ‘map’ widget has an associated Story Variable.
You can do that, however you can’t use $args directly that way. If you add this to the start of your widget:
<<set _tempMap = State.getVar("$" + $args)>>
and then use the array in _tempMap (i.e. swap $loc_home for _tempMap in your first example), it should work fine. (See State.getVar() for details.)
Also, instead of using the story variables $i and $k for your loop variables, you should use the temporary variables _i and _k, because you should only use story variables when absolutely necessary to keep data across passage transitions. This is because story variables increase the size of the game history which has to be kept track of, which in turn slows down passage transitions, saves, and loads.
One other thing I’d suggest is that, instead of doing <<elseif _tempMap[_i][_k] eq #>> for each number, you could shorten that like this:
Thanks for answering. I just couldn’t find way to use a binding argument with a variable, but I tried something similar with state.variable. In any case, it partially helped. But!
I try two ways as you say, using first <<set _loc = State.variables[$args]>> and then <<set _tempMap = State.getVar("$" + $args)>> but in both ways i have an error Error: <<map>>: error within widget contents (Error: <<for>>: bad conditional expression: Cannot read property 'length' of undefined).
Just to clarify first, but in case you weren’t aware, this part of your code is redundant. You should use one or the other. (I assume you’re aware, I’m just covering my bases.)
This tells me that the name of the variable (not including the “$”) and the name of the passage aren’t the same.
If the name of the variable is “$loc_home”, then the name of the passage has to be “loc_home” for the above code to work with:
(Note that the characters around passage() are not apostrophes, they’re backquotes, found on the ~ key in the upper-left corner of most keyboards. You have this correct in your first post, but I thought it was worth mentioning for others.)
If the name of the passage is actually “$loc_home”, then you could just change my code to:
<<set _loc = State.getVar($args)>>
(You’d need to strip out the “$”, either from the passage names or from the $args value, to get Greyelf’s code to work with passage names like that.)