Creating a child based off parent position

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

I have a Node2D with a script. The node2d is located at (0, 200). I have this script attached.

extends Node2D

func init():

func _ready():
	var tile_scene = preload("res://scenes/tile.tscn")
	for i in range(9):
		var tile = tile_scene.instance()
		tile.set_position(Vector2(i * 50, 0))
		get_parent().call_deferred("add_child", tile)

#func _process(delta):
#	# Called every frame. Delta is time since last frame.
#	# Update game logic here.
#	pass

Now when I run this, the 9 objects i create start at 0,0, and increment in the X axis by 50 each time. But they are all situated on y = 0, but I thought since they were children of a Node2D sitting at (0, 200), they would all start at (0, 200) and move in the X direction.

What am I doing wrong? Thanks.

:bust_in_silhouette: Reply From: jobax

Reference the node’s y at the y of your set_position with position.y.

I think it’s not setting the position as a would-be child because it’s just basing it off the Node2D’s parent e.g. root based on where the code is.

:bust_in_silhouette: Reply From: markopolo

“I thought since they were children of a Node2D sitting at (0, 200)…”

I don’t think they are:

get_parent().call_deferred("add_child", tile)

This adds the tile as a child of the Node2D’s parent, not as a child of the Node2D itself. If you change it to just call_deferred("add_child", tile) does that behave as you expect?