Reparenting control nodes when scenes aren't global/always present

Godot Version

4.4.1

Question

My card game has a board made from control nodes, as it is much easier for positioning and whatnot. However, now I’ve run into the issue that I don’t know how to reparent the entities that are supposed to be on the game board to the containers its made of.
Like how do I tell an enemy to “move” to a position on the board (move to another container in the board scene) if the board isn’t global? How do I send an entity to a specific container that’s the child of another container?

You can reparent to any other node, here’s an example assuming you have a reference to the board’s root node, maybe from an @export variable

var target: Node = board_root.get_node("X12/Y34")
entity.reparent(target)

maybe you would have to share your scene tree for more accurate advice.

1 Like

Alright, here’s the scene trees involved:


combat board, entities are to be reparented to the ones in the red bracket

image
enemy/entity, to become the child of the containers of the board

by reference to the board’s root node, are you referring to a preloaded cont?
like const COMBAT_PANEL = preload("res://combat_panel.tscn")?
Or is this something else? I can’t figure out how I would use @export to get a reference to the root node

What ever is creating the enemies must know about the board somehow, it’s difficult to say without a more context of how the enemies are created and where the combat panel exists relative to that.

My enemies are created by a global scene. So if I create a reference to the combat board in the global scene would that work? How would I make a reference to a node from a different scene and put it in my global scene?
The combat panel exists in a different part of the tree than the Global scene.

I think get_tree().current_scene should give you the root node of your combat board (if that is the main scene you are running), even when called from a global scene.

what do you mean by “main scene” I’m running exactly? The combat board is only in the main scene if a combat is happening.

You can add a variable to your global script, then have the combat panel add itself when it’s ready

# in global
var combat_panel: Control

# in combat_panel.gd
func _ready() -> void:
    Global.combat_panel = self
1 Like

By default the scene set as main scene in the editor, the one you are starting. If you use functions like get_tree().change_scene_to_packed(), that changes your main scene.

perfect! Thank you so much

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