So I have here this:

func on_click():
	print("I was clicked")
	clone = get_parent().get_parent().duplicate()
	print(clone)
	print(clone.position)
	clone.position = Vector2(0, 0)
	print(clone.position)

But I believe clones itself to the player.tscn scene, as it doesn't show in the map scene (so I am guessing it clones to player.tscn), so how do I make it clone to the map.tscn scene?

I think if you duplicate a node, you still have to add it to the scene, which is probably why its not showing in the map scene. I think something like this will give the effect you are looking for:

func on_click():
	print ("I was clicked")
	var new_clone = get_parent().get_parent().duplicate()
	get_tree().current_scene.add_child(new_clone)
	clone = new_clone
	print (clone)
	print (clone.position)
	clone.position = Vector2(0, 0)
	print (clone.position)

@TwistedTwigleg said: I think if you duplicate a node, you still have to add it to the scene, which is probably why its not showing in the map scene. I think something like this will give the effect you are looking for:

func on_click():
	print ("I was clicked")
	var new_clone = get_parent().get_parent().duplicate()
	get_tree().current_scene.add_child(new_clone)
	clone = new_clone
	print (clone)
	print (clone.position)
	clone.position = Vector2(0, 0)
	print (clone.position)

I thought I had to use add_child, just didn't know how to really use it. Will try it now.

Looks like the ragdoll bones just break off, does duplicate() duplicate PinJoint2D?

Note:

I'm actually calling this from a button press:

extends TileMap

var cloneCount = 0

func _on_Button_pressed() -> void:
	var new_clone = $Ragdoll.duplicate()
	get_tree().current_scene.add_child(new_clone)
	
	var clone = new_clone
	
	print (clone)
	print (clone.position)
	clone.position = Vector2(0, 0)
	print (clone.position)

@EpicDankDude said: What is clone = new_clone?

I just assigned it to clone to fit the example OP code, as I wasn't sure if the code was using a class variable or a function one.

Looks like the ragdoll bones just break off, does duplicate() duplicate PinJoint2D? I mean, the bones act all ragdolly but they snap off lol

If the PinJoint2D settings are setup in the Ragdoll scene, then it should copy them. You may need to do duplicate(true), with the second parameter telling Godot to do a deep copy. I'm not sure if that would fix it, but it may. It could be that Joint2D-based nodes use absolute NodePaths or when duplicating the paths link to the original joints in the original Ragdoll, so that's why they are not connected.

If you can, you may also potentially want to save the ragdoll as a scene and then instance it instead of duplicating the ragdoll node if you cannot get duplicate joints working. Instanced scenes should have working joints when a new clone/instance is created via code.

Yeah, I think instanced scenes are the way to go for something like this.

2 years later