Set a position of a newly instances scene

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

Hi I know this must have been asked but I can’t find what I’m looking for so reluctantly asking. I have the following.

A scene with:

| board (2d node) as root
| - pieces (2d node) that sits inside board

At top of script:

  var location = preload("res://location.tscn")

The above is just a 2d node with a sprite as a child
Then in my function below:

func setup_board():
	for x in range(boardWidth):
		var locationNode = location.instance()

I want to be able to then set the position of the most recently created locationNode before I loop again and create the next one.

As you can see it’s been placed as a child of the 2DNode called pieces. It’s in a loop, so in my example boardWidth = 5 and it will create 5 of them, with the following names:


I tried various things and have been searching for some time but I keep getting errors. Does anyone know a way to set the location of the last added instance?

Thanks a tonne… Rob

:bust_in_silhouette: Reply From: atze

This should work:

func setup_board():
    for x in range(boardWidth):
        var locationNode = location.instance()

Thank you!

My mistake in all my attempts was as such:

locationNode.set_pos(xPos, yPos)

It would give an error, stating:

Invalid call to function 'set_pos' in base 'Node2D'. Expected 1 arguments.

I had to put in a simple Vector2 call!!! OMG.

So it is now, for future reference for others needing help:

func setup_board():
	for x in range(boardWidth):

		var locationNode = location.instance()

Robster | 2016-09-01 05:33

I will add that for Area2D’s position it will be locationNode.position instead of locationNode.set_pos(newPos)

Ingeniou5 | 2022-01-20 16:27