Issue with resource.duplicate(true)

Godot Version

4.3

Question

I’m trying to use resource.duplicate to make a copy of weapons/armor in the player’s inventory to show on an equipment menu. Here is the code snippet that uses duplicate:

	#print(player_inv_slots[0].num_equipped)
	
	for i in range(player_inv_slots.size()):
		var cat = player_inv_slots[i].item.category
		if cat == "Weapons" or cat == "Armor":
			var dup = player_inv_slots[i].duplicate(true)
			shown_equip.append(dup)
			#print(dup.num_equipped)

Here I try to build an array shown_equip that is used to control what displays in the equipment menu. The first print returns the correct num_equipped from the original item in the inventory. However, the second print does not get the same value from the duplicate (dup) once it’s created. Not sure what’s going wrong; num_equipped is just an int, and to my understanding those should be carried over when using duplicate(true). How would I fix this problem and/or find a different solution?

Have you checked your usage against the description of duplicate() in the docs? Resource — Godot Engine (stable) documentation in English

It looks like it only copies @export properties or ones with a specific usage annotation on them, so maybe that’s your problem?

Ahh I missed the fact that it duplicates exported variables - thank you for pointing that out to me, everything works as intended now that I’ve made the variables exported! Cheers!

1 Like

No worries, glad to have helped.

A tip if you (or a reader) didn’t know, you can ctrl click various keywords (class names, method names, etc.) in the Godot code editor and it’ll take you to their definition. If their definition is built into Godot then it opens the docs for you in the editor instead. It’s really handy!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.