Here are a few suggestions to improve your widget.
1) The story variable style ($args
) of widget arguments is deprecated, meaning it will go away at some point. You should only use the temporary variable style (_args
). For example:
<<run State.setVar($args[1], State.getVar($args[1]) + $args[2]) >>
Should ideally be:
<<run State.setVar(_args[1], State.getVar(_args[1]) + _args[2])>>
Further, since you’ve already made copies of the arguments, you may as well reuse those for increased clarity:
<<run State.setVar(_key, State.getVar(_key) + _change)>>
2) The <Array>.contains()
method was deprecated in favor of the <Array>.includes()
method. For example:
<<if _key.split(".").contains("strength")>>
_name is _diff strong
<<elseif _key.split(".").contains("stamina")>>
_name is now _diff fit
<</if>>
Should ideally be:
<<if _key.split(".").includes("strength")>>
_name is _diff strong
<<elseif _key.split(".").includes("stamina")>>
_name is now _diff fit
<</if>>
Furthermore, since you’re really just looking for strings, you could simply use <String>.includes()
to avoid unnecessarily splitting the string into an array. For example, ideally you’d do:
<<if _key.includes("strength")>>
_name is _diff strong
<<elseif _key.includes("stamina")>>
_name is now _diff fit
<</if>>
Putting it all together:
<<widget "statChange">><<nobr>>
<<set _name to _args[0]>>
<<set _key to _args[1]>>
<<set _change to _args[2]>>
<<run State.setVar(_key, State.getVar(_key) + _change)>>
<<if _change lt -5>>
<<set _diff to "much less">>
<<elseif _change lt 0>>
<<set _diff to "less">>
<<elseif _change gt 5>>
<<set _diff to "much more">>
<<elseif _change gt 0>>
<<set _diff to "more">>
<</if>>
<<if _key.includes("strength")>>
_name is _diff strong
<<elseif _key.includes("stamina")>>
_name is now _diff fit
<</if>>
<</nobr>><</widget>>
Usage is exactly the same.