Instantiation creates the node, but having trouble accesing properties after they change

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
1 Like

Okay, looks like I have the right idea for getting properties - saving the instantiated object as a variable, then grabbing a property from it like you put.

However, the problem I had specifically was that its like it didn’t update when called later - cart.position printed as (0,0) both when it was first instantiated and after it moved off screen.

Is it different when saving objects and accessing properties at a later point?

Still got the same result with global_position and get_global_position(). Anything else to try?

(screenshot with console output and object moving right of 0,0)

1 Like

well, looks like there is something broken on my end. i re-made the test project with your exact code and it still only gave (0,0) as the position haha. I’ll give it a test on a windows machine (I’m running on the Linux build rn) and maybe that would change it?

I’ll come back and report later if I can get it working later or not. Thank you again for your help, it is very appreciated

Okay, I think something is just wrong with the linux build of Godot 4. I got it working perfectly fine with the exact code on my windows machine.

Thanks for working with me on it shrooms, looks like I’m using windows for the foreseeable future haha

Thanks for the solution. I also faced this issue.

Well, after more testing and time to think about it since the original post- it is a much more simple error on my part. (jumping to the conclusion the linux build was bugged is my bad)

I had attached the movement script to a child of the node, and not the node itself. The movement moved the child, but the parent node didn’t move. (insert facepalm here)

Turns out I managed to screw that up TWICE in a row (during the original project and during the test project after to test code given by shrooms), but for some reason did it different in my other tries.

It took a third and fourth project to realize the difference, and now I feel real stupid haha. This is the real solution, thanks for helping me through it.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.