Inventory

Is an inventory saved, between games ?

Inventory implementations generally use one or more Story Variables to track whatā€™s in the container, and the current state of the Story Variables is persisted to the History system each time a Passage Transition occurs. And the current state of the History system is persisted in each Save the end-user creates.

So an Inventory is saved whenever the end-user creates a Save, and its reloaded whenever the end-user loads a Save.

It is unclear what you mean by ā€œbetween gamesā€, if you mean:

  1. Between play-throughs of a specific story/game, then the answer would be Yes if the end-user makes Saves.
  2. Is the current state of the Inventory of one specific story/game automatically available from another specific story/game, then No. Although there are techniques that can be used to transfer data for one story/game to another.

Thanks Greyelf ! So, the variables are saved in the Browser History (not the user hard drive).

Variables are saved with each game save (save slot), and game saves are saved to your browserā€™s localstorage. As you might guess from the name, localstorage is stored locally (as in on your hard drive). So, variables are saved on your hard drive, not in your history thatā€™s synced between browsers.

If youā€™re asking if you can save the game on one PC and turn load that save on another PC, the answer is no. If thatā€™s what youā€™re looking for, a work around would be to save the save file using the ā€œsave to diskā€ option in the save menu, pass the file to your other computer using google drive or something, and then load it using ā€œload from diskā€. Kind of clunky but it should work.

Thanks. I am an experienced programmer, but noob to Twine.
Iā€™m trying to create a ā€˜zork-likeā€™ inventory, where objects have a location value (in a room, or in inventory). So, if I drop an object in a room, it can still be there when I return.

I did something similar in an unfinished game. What I did was create two object lists: one for rooms and one for objects. That way I always knew what was where.

<<set $rooms = {
	office: ["coffee_cup"]
}>>

<<set $items = {
	coffee_cup: {
		name: "cup of coffee",
		location: "office"
	}
}>>

etc. And if you have the names of the room the same as the passage name, you can just use a macro to automatically list the stuff in the room.

Or at least thatā€™s how I did it. But maybe you already guessed thatā€™s the easiest way. :sweat_smile:

I once went a step further and use a single variable to store all ā€˜containersā€™ in the project, both those that represented each of the ā€˜locationsā€™ as well as the container that represented the ā€˜playerā€™ inventory.
eg. The equivalent of the followingā€¦

<<set $containers to {
	'player': [],
	'office': ['coffee_cup']
}>>

ā€¦and if a specific ā€˜locationā€™ name didnā€™t exist as a property within the $containers object then that mean there were no items in that ā€˜locationā€™, as did the array assigned to a ā€˜locationā€™ property being empty.
eg.

<<if not $containers.hasOwnProperty('library') or $containers['library'].length is 0>>
	The Library has no items in it.
<</if>>

note: I actually use ā€˜friendlyā€™ functions & macros to handle adding/removing/checking-for items for locations and the player.

1 Like

Thats a good method, Iā€™ll try it.

FYI, Writing a game is a lot of fun for me. Iā€™m 74 and recently retired from 56 years of business programming.
This game will be the first program Iā€™ve written for myself. Iā€™ve always enjoyed Zork series, and am trying to create a similar adventure game with video, images image maps, and audio.

1 Like

If youā€™re interested, you might want to take a look at my Universal Inventory System (UInv). Itā€™s written for use with Twine/SugarCube to try to simplify all of the complexities of creating a decent inventory system. I do note that itā€™s not quite at v1.0 yet, primarily due to needing to finish the documentation, sample code, and the UI tools, but itā€™s perfectly functional for most purposes as it is currently.

It uses a framework where there are ā€œbagsā€ (things which can contain items) and ā€œitemsā€ (things which can be added to, removed from, or moved between bags). So, you could set the player and the rooms up as ā€œbagsā€ with items in them, and then simply transfer those items between the player ā€œbagā€ and the current roomā€™s ā€œbagā€ as needed.

Hope that helps, good luck with your game, and donā€™t get lost in the maze of twisty little passages all alike! :wink:

2 Likes

Thanks HiEv,
Looks like youā€™re well on the way to have a very ā€˜completeā€™ inventory system.

New Question. I am using the Inventory ok. Is it possible to add an object to the inventory, without linking to another page.
ie.
<<if $inventory.indexOf(ā€œAn Unsigned Noteā€) == -1>>There is a note here. [[Pick up An Unsigned Note.]]<>
ā€“and/orā€“
<<set $inventory.push(ā€œAn Unsigned Noteā€, ā€œAn Unsigned Noteā€)>>

Yes, there are lots of ways. Did you have any method in particular in mind for how the user would do that?

You might want to check out the ā€œSimple Update Without Reloadā€ and ā€œSwapping Itemsā€ examples from my Twine/SugarCube sample code collection to see some examples of things you could do and how to code them.

Iā€™m not sure why you put the same text there twice. Youā€™d only need to do:

<<set $inventory.push("An Unsigned Note")>>

and that would add the string ā€œAn Unsigned Noteā€ to the end of the $inventory array. (See the .push() method for details.)

Hope that helps! :slight_smile:

P.S. When posting code in these forums you should make sure you mark it as ā€œPreformatted textā€ using the </> button, otherwise the forum tends to ā€œeatā€ the inside of some macros. (For example, see how your <</if>> ended up getting up being displayed as <>.)

1 Like

Great. Thanks.
Fundamentally I want to do this:
----- Passage -----
As you stroll along the path. . .
You are carrying: Nothing
You see a note. Pickup the Note

----- Same Passage ā€“
As you stroll along the path. . .
You are carrying: The Note