How could I go about making a functional Equipment Slot system(Sugarcube2)

So I have begun to notice that I have been making sooooo many things in my storyinit passage, especially long strings of sets, like<<set $copperAmethystRing to "Copper, Amethyst Ring">> or <<set $dragonscaledaggersEquip to false>> I have a feeling I will eventually loose track of it all if I don’t find a way to condense it all into sections like maybe <<set $copper.Amethyst.Ring>> or perhaps it would be <<set $Ring.copper.Amethyst>>? and <<set $Equip.dragonscale.daggers>>? but in any case I’m in need of a condense all, with a way to display what equipment the player has in there inventory and what is equipped at that time. I have an inventory passage to store the items that will be displayed and an equipment passage to equip/unequip/switch weapons, armor, and accessories too. I’m still a beginner at this stuff, but I am learning, albeit relatively slow, still learning in any case.

also thanks for all your help: HiEv, Greyelf, Dan Cox, and anyone else who answered my questions before. your all a real help to the twine community. :smiley:

My advice would be the following two questions “Which options are the most important?”, and, from that, “Is there a better way to organize the data?”

Which options are the most important?

Do you really need seven sword options when three options might work better? Do you need Copper and Amethyst rings, or could you use only one of them? Which weapons and combinations are the most important?

Can you reduce the total combinations somehow?

Is there a better way to organize the data?

One of the common ways I have explained inventory systems through either implicit or explicit arrangement. That is, you can “add to” an inventory, or you can use Boolean flags to “toggle” if an item is in an inventory or not.

For example, say you only have six items total in the game. A more explicit approach where an item either is or is not in the inventory might work well. They can each be either true or false.

Another approach is more implicit. Items are “added” to an inventory. In this way of viewing inventories, they are arrays or objects in JavaScript. You “add” an item through working an array (usually push()) or add an extra key to an object. You could then have a large set of possible combinations, but only the smaller inventory set matters.

1 Like

warning: the following code examples have not been tested, they may contain syntax errors.

Do the attributes of your equipment related items change during the playing of the game?

If not then you may want to consider moving the definition of the items to the special setup object, that way they don’t consume unnecessary space within the History system (and the associated Saves).

<<set setup.items to {}>>
<<set setup.items["Copper Amethyst Ring"] to {
	name: "Copper Amethyst Ring",
	image: "images/copper-amethyst-ring.jpg",
	bonus: "magic + 2"
<<set setup.items["Dragon Scale Daggers"] to {
	name: "Dragon Scale Daggers",
	image: "images/dragon-scale-daggers.jpg",
	attack: 12,
	damage: "2d4"

note: the string value between the square brackets of each of the above assignment to the setup.items variable can be use an an Identifier for the related item object.

You could then track which items the player is wearing by assigned that items identifier string to a slot associated with the player object.

<<set $player to {
	name: "Jane Doe",
	age: 18,
	wearing: {
		head: "identifier of a head related item",
		body: "identifier of a body related item",
		weapon: "identifier of a weapon related item"
		accessory: "Copper Amethyst Ring"

You would use code the following to replace the item current associated with a specific slot with another item.

<<set $player.wearing.weapon to "Dragon Scale Daggers">>

and you can use code like the follow to access item related information for any currently worn item, which uses the identifier stored within a worn slot of the player to lookup the related item object within setup.items.

Accessory Bonus: <<= setup.items[$player.wearing.accessory].bonus >>

I’m using copper, silver, gold, corundum, and mythril as a sort of rank up system to upgrade buffs as and when the player equips them and I was also thinking of giving the player a slight bonus if they have all assessaries(rings, neckless, and anklets) of the specific type(ruby, sapphire, amethyst, etc.) and rank(copper, silver, gold, etc.).

also on a side note, I haven’t given my player stats much significance to the game mechanics yet, but I’d like to make the players/allies/enemies dex stat a sorting function from greatest number and working down to the least number to organize who attacks first, then next, then next, etc. however, the first to attack is always the player, but from then on until a party loses or flees the battle it goes in that order of greatest to least using the dex stat.

You might want to take a look at my Universal Inventory System (UInv), which is a JavaScript “plugin” for Twine/SugarCube 2 to add a game inventory system.

With UInv you put the UInv code at the bottom of your JavaScript section, build all of your items ahead of time in an “Item Builder” which generates code for those items, and then add your item code to the UInv code. Once you’ve done that you can easily create multiple inventories, move items between inventories, add/delete/modify items, and search for particular items or items with certain properties. It also stores the data in a way which helps minimize history bloat, which can slow down page transitions, saving, and loading.

The code is mostly complete except for some of the display code I still plan on adding. I also need to finish writing the help file, testing the functions, and write better sample code, but the code is completely functional as it is currently. You can always find the current release of UInv here.

Give it a shot and let me know if you have any questions on it, find any problems, or if you have any suggestions for improvements.

Hope that helps! :slight_smile:

1 Like