How do I create duplicates of kinematic bodies?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By da_dev

So, I have a KinematicBody2d of a soldier and when I press, let’s say, ui_select a new soldier is created. I wrote this script for the root node, but it doesn’t seem to work.

func _input(event):
if event.is_action_pressed("ui_select"):
	var soldier = get_node("Soldier").duplicate()
	soldier.position = Vector2(get_viewport().size.x/2, get_viewport().size.y/2)

What is the proper way of doing this?

1 Like
:bust_in_silhouette: Reply From: Dlean Jeans

Save the Soldier as a scene and instance it:

const Soldier = preload('res://Soldier.tscn')

var soldier = Soldier.instance()