0 votes
by (500 points)

Using this code (as a widget):

<<widget Add_ItemToInv>><<nobr>>
<<set $ItemAdded = "args[0]">>
<<if $Inventory.includes($args[0])>>
<<set $TextMessage = "You already have the item $ItemAdded in your inventory.">>
<<else>>
<<set $TextMessage = "You have added the $ItemAdded to your inventory.">>
<<endif>>
<<set $Inventory.pushUnique($args[0])>>
$TextMessage
<</nobr>><</widget>>

How can i then display/print out $TextMessage (i.e args[0]) in a different passage at a later point, without getting something like this:

You have added the $args[0] to your inventory.

I get you're supposed to use <<print>>, but i'm not entirely sure how to do it properly in practice. The widget itself on it's own works fine and displays/prints properly, but when i use $TextMessage somewhere else or outside the widget, i get the following above.

Thanks

1 Answer

+1 vote
by (38.6k points)
selected by
 
Best answer

The problem is that putting "args[0]" inside quotes means that it's a string, not a variable.  What you want to do is this:

<<widget "Add_ItemToInv">><<nobr>>
	<<if $Inventory.includes($args[0])>>
		You already have the item $args[0] in your inventory.<br>
	<<else>>
		You've added the $args[0] to your inventory.<br>
		<<set $Inventory.push($args[0])>>
	<</if>>
<</nobr>><</widget>>

Note that you're supposed to use "<</if>>", and NOT "<<endif>>".  The "<<endif>>" marker is depreciated and may not work in future releases.

Also, instead of using "<<nobr>>" macros in your widgets, you might want to just have a "nobr" tag on your widget passage(s) instead.

Hope that helps!  :-)

by (500 points)
It isn't possible to print it as a string? Doing it without $TextMessage as a string means i would have to use the widget more than once to show the same text.

Good to know about the <<endif>> though, thanks :)
by (154k points)

@Sylen

As demostrated by the <<widget>> macro's examples the name argument of that macro is a String so the value should be quoted. Your widget declaration should look as follows.

<<widget "the-widget-name">>
	/* your code */
<</widget>>

Your current declaration is relying on SugarCube guessing the data type of the argument you are suppling the <<widget>> macro.

by (500 points)

@greyelf

I don't think i understand entirely, if i quote the widget macro i still just get back something like this (when not setting args[0] as a string)

You have added the $ItemAdded to your inventory.

 

by (154k points)

@Sylen

If you look at the example I gave you will notice that the widge name String argument..
"the-widget-name"
...I passed to the <<widget>> macro (used to define the custom widget) is wrapped in quotes.

Or if you perfer an example based on your own original one...

<<widget "Add_ItemToInv">><<nobr>>
	/* the code of your widget.. */
<</nobr>><</widget>>

(note that the "Add_ItemToInv" value being passed to the <<widget>> macro is quoted)

by (500 points)
@greyelf

Yeah i understand and that's exactly what i did :) -  it just doesn't seem to change anything to put the widget name in quotes.
by (38.6k points)

"It isn't possible to print it as a string?"

Of course it's possible, it just didn't seem necessary.  If you need that then do this:

<<widget "Add_ItemToInv">><<nobr>>
	<<set $ItemAdded = $args[0]>>
	<<if $Inventory.includes($args[0])>>
		<<set $TextMessage = "You already have the item " + $args[0] + " in your inventory.">>
	<<else>>
		<<set $TextMessage = "You've added the " + $args[0] + " to your inventory.">>
		<<set $Inventory.push($args[0])>>
	<</if>>
	$TextMessage<br>
<</nobr>><</widget>>

Now $ItemAdded will get set to the first value passed to the macro and $TextMessage will get set to the output of that macro.  If you don't use $ItemAdded then you can get rid of that line.

by (500 points)
@HiEv

This worked  :)

Thanks!
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.
...