Quantcast
Channel: Help! with 2.0 - Twine Forum
Viewing all articles
Browse latest Browse all 1844

[Sugarcube 2.13.0] Passing arguments and widgets calling widgets

$
0
0
Hey there,

I come to you with another problem that I ran into.
I'm running SugarCube 2.13.0 and Twine 2.1.0

The issue is the following, I'm trying to implement a way in which the player can interact with the inventory of other objects in the game, by placing and taking items from their containers. I'm basically listing the items inside the player's and the inspected container's contents (items) and then offer actions that can take place on the given object. Such as moving items back and forth, inspecting item properties, etc.

I'm trying to do this using a <<widget>> which calls another <<widget>>. I've tested and been using recursive <<widget>> which had no problems whatsoever (probably because of the same arguments that get passed to them).
In my case I fail to pass the _item object to <<putItemInContainer>>. Since the latter throws an error that .hasOwnProperty of an undefined variable made the whole thing crash and burn. This is very interesting to me since, the other widgets have never been complaining. I imagine the for loop and the temporary _i (iterator value) adds a complication. I'm trying to remedy this by setting up a temporary variable: _item and reference the indexed item ($player.inventory[_i]). This also doesn't seem to help in this case.

I tried to substitute the <<putItemIntoContainer>> widget by taking out the part that was important to me with the hasOwnProperty check and then the calling of other widgets such as unequip and JS function call to transfer the item into the new container. Now, this substitution kinda fails too since I only get the last item that is then put into the container after the second try, which I can't take out, since the wrong item gets put into the container.

There must be a way to call transfertItem() using the correct arguents (_item) I just have no idea how.

Can anyone explain to me what is going on here?
Am I missing something when passing arguments to another widget?
/% when clicked, shows the contents of the container and the actions that can be carried out on them %/
/%TODO: Add option for the player to put their items into the container <<putItemInHere _container _container[_i]>>%/
<<widget "openContainer">><<nobr>>
	  <<set _container to $args[0]>>
	  <<linkreplace "Open container">>
		  <<if _container.length > 0>>
			  The $args[1] holds the following items:
			  <<for _i = 0; _i < _container.length; _i++>>
			  	_container[_i].name X _container[_i].count
			  	<<showItem _container[_i]>>
				<<inspectItem _container[_i]>>
				<<debugItem _container[_i]>>
			  <</for>>
		  <<else>>
			  @@color:orange;This seems to be an empty $args[1]!@@
		  <</if>>
		  <<if $player.inventory.length > 0>>
		  <<for _i = 0; _i < $player.inventory.length; _i++>>
		  	<<set _item to $player.inventory[_i]>>
			_item.name X _item.count
			<<showItem _item>>
			<<inspectItem _item>>
			<<debugItem _item>>
			<<linkreplace "Put _item.name in here">>
				<<putItemIntoContainer $player.inventory _container _item>>
			<</linkreplace>>
		  <</for>>
		  <<else>>
		  	@@color:orange;The player's inventory is empty!@@
		  <</if>>
	<</linkreplace>>
<</nobr>><</widget>>
/%
Places an item from on inventory into the other
$args[0] - source inventory
$args[1] - destination inventory
$args[2] - item, this needs to be already inside the source inventory
so that it can be moved into the destination inventory
%/
<<widget "putItemIntoContainer">><<nobr>>
	<<if $args.length < 3>>
		@@color:red:There aren't enough parameters passed to transfer an item.@@
	<<else>>
		<<set _item to $args[2]>>
		<<set _src_container $args[0]>>
		<<set _dest_container $args[1]>>
		<<if typeof _item !== undefined>>
			<<if typeof _item !== "string">>
			  <<set _item to getItemById(_src_container, _item)>>
			<</if>>
			<<if _item.hasOwnProperty("isEquipped")>>
				<<if not _item.isEquipped>>
				  @@color:purple;You place _item.name into a container!@@
				  <<transferItem _src_container _dest_container _item>>
				<<else>>
					<<if _item.hasOwnProperty('clothingType')>>
					  @@color:orange;We un-equipped your item and moved it!@@
					  <<unequip $player.clothing _item.clothingType>>
					<<else>>
					  @@color:red;no clothingType property on passed item (3rd argument).@@
					<</if>>
				<</if>>
			<<else>>
			  @@color:purple;you put _item.name into a container!@@
			  <<transferItem _src_container _dest_container _item>>
			<</if>>
		<<else>>
			@@color:red;[Error]putItemIntoContainer arg[0] or item is undefined@@
		<</if>>
	<</if>>
<</nobr>><</widget>>

Viewing all articles
Browse latest Browse all 1844

Trending Articles