0 votes
by (150 points)

Is it possible to set up something like, say, 

<<widget "UpgradeItem">>
//do things
<</widget>>

And then call it like <<UpgradeItem $iron_sword>>, and have it actually change the value of $iron_sword?

Widgets normally avoid modifying variables passed into them by corying them into $args[i], but is there a way to make them affect the parameter intentionally?

1 Answer

+1 vote
by (61.7k points)

If the variable is an object, it will do that automatically (ie pass the objects by reference). If the value is a primitive, like a number or string, pass the variable in quotes and edit it's value using State.setVar(). For example, if you call the widget like this: 

<<UpgradeItem "$iron_sword">>

You can do this inside the widget: 

<<run State.setVar($args[0], State.getVar($args[0]) + 10))>>

Which would increase the $iron_sword variable's value by 10. 

by (8.5k points)

You can do this without quoting the variable too

<<run State.setVar($args.raw, $args[0] + 10))>>
by (220 points)
First of all, I'd like to point out that there is an unexpected token in both examples, from copy and pasting them. There's an extra ")"

Second of all, how do you set strings? Because attempting to use "=, is, ect" will throw an error. It only works with math operations such as "+, -, /, * ect" which is not what I'm looking for.
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.
...