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
extends Node2D
func _ready():
	
	#tell gdscript v_sprite is Sprite2D type 
	var v_sprite : Sprite2D = Sprite2D.new()
	
	#access a property of sprite2d
	#(there is no texture so it is null)
	print(v_sprite.texture)

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?

use global_position to see where it is on screen. position is where the node is in relation to its parent.

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

gdscript i don’;t deal with it so i am unsure. maybe someone else will know.

to test i created a simple project with a main and a test scene. the test scene script sets its global position to the mouse position. then from main scene to prints test scene’s global position., it works on my end.

script for root node2d in test scene:

extends Node2D
func _process(delta):
	global_position = get_global_mouse_position()

script for root node2d in main scene:

extends Node2D
var m_test_scene = preload("res://test_scene.tscn")
var m_test : Node2D = null
func _ready():
	m_test = m_test_scene.instantiate()
	add_child(m_test)
func _process(delta):
	print(m_test.global_position)

2 Likes

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

that is weird but it seems weird there would be such a simple bug on linux. i imagine if there was a lot of people would run into it. can you try rebuilding the project?

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

we’ve all been there. if a node isn’t moving and you know you set its position, try printing the name of the node to the console. always good to verify and make no assumptions. glad you got it working. hopefully you don’t have to fallback on windows, either.