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 (63.1k 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.6k points)

You can do this without quoting the variable too

<<run State.setVar($args.raw, $args[0] + 10))>>
by (270 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.
...