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