# How do I add an object to the node tree at a specific position

I'm making a skiing game, and a part of that is programmatically adding snow to the scene in a specific place. It will all be the child of a single node.

I was looking through the Flappy Bird code but when it instances a scene, it only uses the function add_child(). There doesn't seem to be a way to set its position.

Here is the project on github.

So first I would create a scene called "flag_scene". In that scene, have your code for making the flags do what you want. Every time the script I posted below is ran (and if it's put in main scene), it creates a copy of the flag scene, sets it in whatever position you want, and does the actions you assigned it to do in the script (attached to flag_scene.tscn)

onready var flag_scene = load("location of flag.tscn")

var timer_raw = 0
var timer

func _procces(delta):
timer_raw += delta
timer = round(timer_raw)

if timer > 4:
spawn_flag() #call every 4 seconds
timer_raw = 0

func spawn_flag():
flag = flag_scene.instance()
flag.set_pos([where you want the flag])
# If you use Godot 3.0:
# flag.position = [where you want the flag]

• @AlexandreVaillancourt I typed this up on my iPhone, so the controls where a bit clunky. – BrrrIce May 30 '17 at 13:08
• Ah, right :) no problem! – Alexandre Vaillancourt May 30 '17 at 13:08
• Oh sweet. This is exactly what I needed. I got them spawning in the end just not quite as elegantly. Currently waiting for 3.0 to hit a more stable state since collision detection doesn't work for scenes loaded in with that method. Thanks though. I'll bookmark this for Godot 3 :) – crabcrabcam May 31 '17 at 8:11
• @crabcrabcam Im running 2.12 and having an area2d with Collisionshape2D lets me check collisions for both scenes. – BrrrIce May 31 '17 at 10:40

As you already noticed, when you call add_child(), there isn't a way to input the position at the same time.

To change the position, you modify the position variable on the instanced node.

scene_instance.position = Vector2(x, y)


The structure that this gives instancing/placing code looks something like this.

scene_instance = scene_being_instanced.instance()

# All modification you want to happen *before* the node(s) are added
scene_instance.position = Vector2(x, y)