Godot Version
4.7.1 stable
Question
I’m trying to make a script that duplicated a Package, but the amount that duplicates doubles, and I don’t know why.
func spawnPackage():
var packagedupe = get_node("/root/Main/Packages/PackageD").duplicate()
get_node("/root/Main/Packages/PackageD").add_child(packagedupe)
packagedupe.name = "Package" + str(Packages.size())
print(packagedupe.name)
packagedupe.visible = true
print(packagedupe.visible)
packagedupe.get_node("CollisionShape3D").disabled = false
print(packagedupe.get_node("CollisionShape3D").disabled)
packagedupe.global_position = global_position + Vector3(0, 5, 0)
print(packagedupe.global_position)
packagedupe = null
You’re parenting each copy under PackageD:
get_node("/root/Main/Packages/PackageD").add_child(packagedupe)
Next duplicate() copies PackageD and every package already stuck under it, so count doubles each spawn.
Duplicate from a template, parent to Packages (or another folder node), not to the source:
func spawnPackage():
var template = get_node("/root/Main/Packages/PackageD")
var packagedupe = template.duplicate()
get_node("/root/Main/Packages").add_child(packagedupe)
packagedupe.name = "Package" + str(Packages.size())
packagedupe.visible = true
packagedupe.get_node("CollisionShape3D").disabled = false
packagedupe.global_position = global_position + Vector3(0, 5, 0)
Cleaner long-term suggestion: make a package.tscn and preload(…).instantiate() instead of duplicating a live node.
–Also
packagedupe = null doesn’t free anything ; the node is already in the tree.