Godot Version
Godot v4.2.1
(running on ubuntu-based Linux)
Question
What is the proper way to get the properties of an instantiated object? And how is it different then the code I’ve provided?
Hi, I have the code to create a train with the carts in build_carts. The goal is to allow the carts to communicate their position to each other.
After running the given code, two carts are successfully instantiated and continuously move to the right (in their own script). Each are a CharacterBody2D with a CollisionShape2D and Sprite2D.
Running print(carts) prints “[Node2D:<Node2D#24930944212>, @Node2D@2:<Node2D#25014830289>]” showing it has both nodes in the array.
However, running print(carts[0].position) or even print(get_child(0).position) during _process() to get its location only returns (0,0), even though it is constantly moving (again, in its own code). It is successfully returning a position and printing it, but it isn’t accurate to the actual objects in the scene.
When manually adding a cart to the scene and printing its position, it prints its coordinates properly. There isn’t much in the scene, this is a pretty fresh project.
What am I missing about instantiating objects/scenes and accessing their properties? The original carts are at (0,0) in their scenes, is it accessing that property?
Thank you very much for the help, big thanks
var train_head = preload("res://Sprites/train_head.tscn")
var train_base_cart = preload("res://Sprites/base_cart.tscn")
# array of carts to build, load in from a previous scene later
var build_carts = [train_head, train_base_cart]
# current carts to draw data from
@export var carts = []
func _ready():
for i in range(0, build_carts.size()):
var cart = build_carts[i].instantiate()
add_child(cart)
carts.append(cart) # add to array to
carts[i].visible = true
print(carts)
func _process(delta):
print(carts[0].position) # prints (0,0)
print(get_child(0).position) # also prints (0,0). no other children then the instantiated objects
# same results with 1 rather than 0