Await Frame / <1 Second Inside Static Function

Godot Version

4.4

Question

I am writing code that includes a static function, inside this as part of the setup I need to remove all the children of a node;

# 'player.action_bar' is a UI element showing turn details, as this is for a turn-based game
player.action_bar.get_children().map(func(node : Node) -> Node: node.free(); return node)

After this I want to then repopulate the action bar with the new data;

for combatant : Node in combatants:
	player.action_bar.add_child(action_bar_item.instantiate())
	#... more code

But this can fail since it is adding them before the last ones are gone, causing certain names to have numbers added to the end, which isn’t ideal since other parts of code rely on the specific name of the nodes.

So I try to await a frame to give the nodes time to free, but I can’t since to do the standard ‘await get_tree.process_frame’ relies on get_tree(), which isn’t static so I can’t.

So I try creating a timer variable using Timer.new(), but trying to call ‘start()’ fails as it isn’t added to the scene tree, and I can’t add it to the scene tree since ‘self’ and ‘add_child()’ aren’t static.

Is it possible to await either a frame or a sub-second amount of time inside a static function? Or will I have to make this a singleton and un-static everything?
Thanks.

I’m not sure if waiting a frame is the best solution in this case. Why are you adding before removing the nodes? When are you calling the static function?

If you really need to wait a frame, you could pass the tree as a parameter. But if you want the function to have access to the scene-tree, I think it’s cleaner to have it as an autoload/singleton.

I am removing the nodes before trying to add new ones, but if I do it all in the same frame then when trying to add new ones with the same / similar names then it would changed them since you can’t have multiple nodes with the same name as siblings.

But passing the tree as an argument did fix it! Though if I need the scene tree more later I will want to change this to a singleton.

Thanks!

You can free/remove in the same frame as you add without any name changes, as long as the freeing happens before the adding.

Btw. I just saw that you have a reference to the player instance in the static function. You can also use this to get the scene-tree instead of passing it (i.e., player.get_tree()).

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