Item Stacking in Inventory

Godot Version

4.7.2

Question

tl:dr - Im using a String comprised of various variable values in my class instances to see if they are similar enough to stack in an inventory. Is there a better way or is this okay?

I’ve made an inventory system for my game which uses Item instances for items. This allows for multiple instances of the same item with different values for things like condition.

My inventory is a list style, so when the player collects multiples of the same item I want them to be stacked so the view filled with multiples of the same entry. However, because each item could have different values I need them to stack only those which match. Continuing my example, one iron sword could have a condition 50% while the rest are at 100% so I need two stacks in that case.

What I’m doing currently is to have a function in my Item class which gets a “stack_key”. Basically all the variables that I want included as a String separated with “|”. The inventory can use this when adding an item to the inventory to see if there is an entry with a matching stack_key, if not it creates a new stack. At this point, I don’t think items will be allowed to change state while in the inventory.

Is this an appropriate solution? I’m not sure if there is a faster way to compare items for “stackability”.

#Item
class_name Item
extends Resource

@export var data: ItemData

var stolen: bool = false


func get_stack_key() -> String:
	return str(data.item_id)

# Weapon
class_name Weapon
extends Item

@export var condition: int


func get_stack_key() -> String:
	return "%s|%d" % [
			super.get_stack_key(),
			condition
	]


#InventoryComponent
func add_item(item: Item) -> void:
	var stack_key: String = item.get_stack_key()
	
	for entry: InventoryEntry in entries:
		if stack_key == entry.stack_key:
			entry.stack_item(item)
			entry_modified.emit(entry)
			return
	
	var new_entry: InventoryEntry = InventoryEntry.new(item)
	entries.append(new_entry)
	entry_added.emit(new_entry)

Seems fine, is there anything about this that you find problematic?

Not specifically, I guess I just wanted to know if there was a better way of doing it rather than comparing strings? But it does seem to work how I wanted so I guess I really just wanted validation that I wasn’t overcomplicating it.

Your making your own specialized serializer/marshaller, this is very common and only as complicated as you make the format. You haven’t given us the stacking code, I assume entry.stack_item so we don’t know how you are actually storing/de-serializing this data, nor do we know your greater inventory system so detecting pain points is difficult from here. If it doesn’t cause you any troubles then it’s great!

Currently I just have add_item functionality, but here it is:

class_name InventoryComponent
extends Node

signal entry_added(entry: InventoryEntry)
signal entry_modified(entry: InventoryEntry)

@export var display_name: String = "Default Inventory"

var entries: Dictionary[String, InventoryEntry] # For easy lookup
var entry_list: Array[InventoryEntry] # For UI and sorting


func add_item(item: ItemInstance) -> void:
	var stack_key: String = item.get_stack_key()
	
	var entry: InventoryEntry = entries.get(stack_key)
	
	if entry == null:
		# This is a new item so create a stack for it.
		var new_entry: InventoryEntry = InventoryEntry.new(item)
		
		entries[stack_key] = new_entry
		entry_list.append(new_entry)

		entry_added.emit(new_entry)
		
		return
	
	# Add item to an existing stack.
	entry.stack_item(item)
	entry_modified.emit(entry)

class_name InventoryEntry
extends RefCounted

var display_name: String
var items: Array[ItemInstance]


func _init(p_item: ItemInstance) -> void:
	# First item determines the entry data.
	display_name = p_item.get_item_type().display_name
	items.append(p_item)


func stack_item(item: ItemInstance) -> void:
	items.append(item)


func get_quantity() -> int:
	return items.size()

@tool
class_name Weapon
extends ItemInstance

@export var weapon_type: WeaponType
@export var condition: int


func get_item_type() -> WeaponType:
	return weapon_type


func get_stack_key() -> String:
	return "%s|%d" % [
			weapon_type.type_id,
			condition
	]

I’d say you are on the right track. I do the same in our game Manabies. My stacking identifier us something like

"{id}__{modification}__{text_overlay}"

The arrow items in the toolbae at the bottom have a differrent modification (arrow direction), so that keeps them from stacking.

What about a dedicated ItemParameter class? ItemFloat, ItemInt/Bool etc.
Then you can embed the var stackable:bool into the parameter itself and tick it in inspector, and everything else would be automatic. Tho to make it look nice you would need inspector plugin code.

That actually sounds like an idea worth investigating. For now I’m going to stick with my basic get_stack_key() function for simplicity.

get_stack_key() already covers cases like struhy’s arrows that should not stack because the modification is part of the key. A later ItemParameter flag could still help for items that are stackable in one inventory and unique in another, without replacing that key.

I’ve settled for now on generating a string stack key for each item instance as it gets added to the inventory, and my entries cache a copy of it to make it easier to compare with new items getting added in. I also added a mergeable bool to the BaseItemType so certain item instances will be merged into a single instance with a quantity (like arrows) rather than storing several hundred arrow instances.