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]
  • \$\begingroup\$ @AlexandreVaillancourt I typed this up on my iPhone, so the controls where a bit clunky. \$\endgroup\$ – BrrrIce May 30 '17 at 13:08
  • \$\begingroup\$ Ah, right :) no problem! \$\endgroup\$ – Vaillancourt May 30 '17 at 13:08
  • \$\begingroup\$ 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 :) \$\endgroup\$ – crabcrabcam May 31 '17 at 8:11
  • \$\begingroup\$ @crabcrabcam Im running 2.12 and having an area2d with Collisionshape2D lets me check collisions for both scenes. \$\endgroup\$ – 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)


# All modifications you want to happen *after* the node(s) have been added

For more information, I recommend taking a look at the Node documentation.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.