Need guidance in creating an inventory system

0 votes
asked Dec 21, 2017 by SleeteWayne (140 points)

I am looking for a little help in implementing an inventory system. Nothing terribly complex. I am currently storing the data on my inventory as:

<<set $Items to {
	makeup_kit	: {
		name	: "Makeup kit",
		value	: 20.52,
		desc	: "",
		type	: "hygiene",
		slot	: 0
	camera		: {
		name	: "Disposable Camera",
		value	: 20.52,
		desc	: "",
		type	: "Utility",
		slot	: 0
} >>

At the moment I am trying to insert items into my player data using push() no problem. 

And my player data container looks something like::

<<set $player = {
  	"fname" 			: "First Name...",
	"lname" 			: "Last Name...",
  	"age" 				: random(16,22),
	inventory			: []

I know I can push and pop the additions into $player.inventory, but I am having trouble trying to use $player.inventory.find(), $player.inventory.includes(), or etc to see if something already exists so that I can try to update the quantities. Whenever I would try to use find() it always returns false. I'd like to avoid having to write a for loop each time to see if something is in the inventory as I'm not versed in writing custom macros. Could someone suggest a solution, a better implementation, or a pre-existing solution? 

1 Answer

0 votes
answered Dec 21, 2017 by greyelf (76,170 points)

There are a number of threads on the Old Forums Archive (link in the top right sidebar) that cover different methods of implement an inventory system, and the potential issues with referencing objects stored with such inventories. There are also a couple of question about this subject matter on this Q/A site.

One such thread is Inventory System (Sugarcube 2.0 / Twine 2), which includes an explanation of why you need a way to unequally IDentify each object stored within each inventory instance.

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.