Resource dosen't duplicate correctly

Godot Version

4.5.1.stable

Question

i’m using resources to store the contents of a bottle, to avoid modifying the same resource, i create a duplicate of it, like this

emptyBottle = PlayerInventory.inventory[PlayerInventory.selectedSlotID].item
storedBottle = PlayerInventory.inventory[PlayerInventory.selectedSlotID].item.duplicate(true)

emptyBottle is what i use to give the player the old bottle back if they don’t modify anything while the storedBottle is the one i put the new stuff in

howerver, when the duplication occurs it does this

emptyBottle with 40 units stored and chem color yellow:


storedBottle that for some reason, reverts back to the default settings, 0 units stored and white color despite being a duplicate of the same resource:

is there a way to duplicate the resource properly or should i use something else other than resources?
this is the code of the resource if needed:

extends InvItem
class_name BottleStats

@export var maxUnitCapacity: float
@export var bottleType: bottleTypes
@export var storedChems: Array[chemContainerSlot] = [chemContainerSlot.new()]

var totUnitsStored: float
var isFirstElement: bool = true
var chemColor: Color = Color(1.0, 1.0, 1.0, 1.0)

enum bottleTypes {DRINK,BULLET,GRENADE}

#returns true if the chem level changed, if not returns false
func addChemToBottle(chemToAdd: ChemStats,unitsToTransfer: float) -> bool:
	if totUnitsStored + unitsToTransfer > maxUnitCapacity:
		return false
	if isFirstElement:
		storedChems[0].storedChem = chemToAdd
		storedChems[0].unitsStored = unitsToTransfer
		totUnitsStored += unitsToTransfer
		isFirstElement = false
		return true
	for i:int in storedChems.size():
		if storedChems[i].storedChem == chemToAdd:
			storedChems[i].unitsStored += unitsToTransfer
			totUnitsStored += unitsToTransfer
			if storedChems[i].unitsStored <= 0:
				storedChems.pop_at(i)
			if storedChems.is_empty():
				storedChems.append(chemContainerSlot.new())
				isFirstElement = true
			return true
	storedChems.append(chemContainerSlot.new())
	storedChems[storedChems.size() - 1].storedChem = chemToAdd
	storedChems[storedChems.size() - 1].unitsStored = unitsToTransfer
	totUnitsStored += unitsToTransfer
	if storedChems[storedChems.size() - 1].unitsStored <= 0:
		storedChems.pop_back()
	if storedChems.is_empty():
		storedChems.append(chemContainerSlot.new())
		isFirstElement = true
	return true

func getMatchingChem(chemToFind:ChemStats) -> chemContainerSlot:
	for i: int in storedChems.size():
		if storedChems[i].storedChem == chemToFind:
			return storedChems[i]
	return null

func isBottleEmpty() -> bool:
	if storedChems.is_empty():
		return true
	if storedChems.get(0).storedChem == null:
		return true
	return false

1 Like

Does it by any chance work correctly if you use Resource.duplicate_deep(Resource.DEEP_DUPLICATE_ALL)?

For example in your case:

emptyBottle = PlayerInventory.inventory[PlayerInventory.selectedSlotID].item
storedBottle = emptyBottle.duplicate_deep(Resource.DEEP_DUPLICATE_ALL)

well it duplicates the array, witch is the only thing that matters for the chem system, the color and tot units stored still remain the same though.
i just had the stored bottle copy them from the empty bottle since they are only graphical things

1 Like

That doesn’t sound right. Are they correctly set before duplicating the object? Might want to try printing the values right before then.