0 votes
by (1.1k points)
Here about $args, I have read all the documentation on widgets of sugarcube, but I want to ask exactly how does it work and so can I use $args exactly?
Some things the sugarcube documentation really does not explain well... And I would like to know all about it.

1 Answer

0 votes
by (159k points)
selected by
 
Best answer

The $args variable is an Array and it contains each of the arguments that you passed to your widget when you called it.

How you use the $args Array within a widget greatly depends on what exactly that widget's purpose is, but here are two (untested) examples.

eg. If you have a widget named doit and you call it in your passage like so.

<<doit "one" "two" "three">>

... then the $args Array avaiable to the doit widget will contain the three arguments ("one", "two", "three") you passed to it.

Because the $args variable is an Array you can use any of the associated SugaCube or JavaScript method & properties on it's value.

eg. You can used it's length property to check how many arguments were passed to the widget.

<<widget "doit">>
	/* Check if enough arguments were passed to the widget. */
	<<if $args.length < 3>>
		You need to passage 3 arguements to the ''doit'' widget.
	<</if>>
<</widget>>

eg. you can use integer indexes to access each argument. (Array's are zero based, 1st index is 0, next is 1, etc)

<<widget "doit">>
	<<set _first to $args[0]>>
	<<set _second to $args[1]>>
	<<set _third to $args[2]>>
<</widget>>

 

...