Duplicate() not making a unique copy of my custom resource

Godot Version



I am making a dungeon crawler where you have multiple party members. I have a party member scene and a custom resource for storing their inventory data. The party members have this code in their script

@export var inventory: Inventory

func _ready():
	inventory = inventory.duplicate()

The problem is, for some reason the resource stored in the inventory variable is still not unique. When I put stuff in one party member’s inventory, they all get it for some reason. When I make the inventory unique from the editor it works fine but I don’t want to do that manually every time so it would be really nice if there is a solution to this

Have you tried setting the Local To Scene setting on the resource to true?
That helped me a lot with related issues for my own custom resource.

Thanks for the reply, I just checked and it seems that the issue still persists if I do that

Try duplicating with the subresources argument as true.

Sorry, forgot to mention it in the original post, tried that already and it didn’t have any effect

You may be encountering this known issue with duplicating Resources then: Resource.duplicate(true) doesn't duplicate subresources stored in Array or Dictionary properties · Issue #74918 · godotengine/godot · GitHub

Your Inventory resource probably contains an array or dictionary that is not getting a unique instance.

The workaround is to create a custom duplicate function for your resource that will also make deep copies of all the arrays and dictionaries that it contains.

Thank you, that seems to explain this