I’m working on an inventory for my game, and i have item classes like consumables, medicine, firearms etc. and for firearms i need to store the amount of ammo inside the gun, but if i just make a variable and change it from script all of the same firearms in the inventory will change, is there a way i can make a variable unique for each one?
I would say it depends on how you have it set up. One method would be eliminating the firearms need to know how much ammo it has. I don’t know how you have your shoot func() set up, however, if when the player picks up/equips the firearm a signal is passed to the player_ammo giving info such as clip size, ammo capacity, ammo type, ect… This node would then be updated rather than changing the actual weapon. This way the weapon doesn’t care if it has ammo or not, the player does.
Example:
Player picks up revolver
Revolver sends signal (6 bullets, 40 cal)
Player_ammo recieves signal; var ammo_cap = gun_cap, var ammo_type = gun_type
Player if player_ammo.ammo_cap > 0 calls gun.shoot()
For me I guess it doesn’t seem like the weapon needs to know if it can shoot or not. It is the player that needs to know that.
Edit:
Thinking through the question, if you need to have multiple weapons that you find laying around of the same type with different ammo amounts, if you aren’t instancing them uniquely in code, but placing them in world, you could use @export var for determining their starting value, but again if you want to change them I would keep that local to the player and consider what your items “need” to know.
My entire inventory is made of resources, the inventory resource has an array that contains slot resources that store the item inside, amount of items and what type of slot it is(firearm slot or equipment)
It is hard to know what the issue is without knowing how you are calling them. If they are unique instances that you are changing the variable of it shouldn’t be a problem. However, if you are changnng it by a shared class_name then that would be a problem.
You could store the information you need like was suggested in a dictionary, and change the value of the dictionary.
I would say that your inventory slot should be its own class, e.g. InventorySlot, which contains a reference to an Item that is being stored there and the quantity. It can even be internal to your Inventory class, as it will most likely be used only within that script. You can have an add_item() method that looks through the slots and accumulates quantity, or creates a new slot if the item is not found.
class_name Inventory
extends Node
class InventorySlot:
var item: Item
var quantity: int
var inventory_slots: Array[InventorySlot]
func add_item(item: Item, quantity: int)
# Check all inventory slots if there already is a slot with the same item that can be used.
for inventory_slot: InventorySlot in inventory_slots:
if inventory_slot.item == item:
inventory_slot.quantity += quantity
return
# In case an existing slot is not found, create a new slot.
var new_inventory_slot = InventorySlot.new()
new_inventory_slot.item = item
new_inventory_slot.quantity = quantity
inventory_slots.append(new_inventory_slot)