0 votes

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?

in Engine by (32 points)

1 Answer

0 votes
Best answer

Save the Soldier as a scene and instance it:

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

var soldier = Soldier.instance()
by (4,155 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.