0

I am currently making a simple tree which looks like this :

MainNode (Parent) -> Character (Child)

MainNode can add child or free one from any files such as maps (i.e. terrain.tscn) or events (i.e. event.tscn). All events and maps will be stored in a dictionary for easy usage. The codes would look like this :

func loadNewScene(key):
   # load the corresponding tscn file from dictionary
   add_child(instanced_scene)

func freeScene(key):
   # find the corresponding target node using key
   target.queue_free()

func _ready():
   # load Mainmenu.tscn
   loadNewScene("Mainmenu")

My problem begins when I press start in the Mainmenu.tscn. If I had Mainmenu.tscn as a child (in default), I can always emit a signal for confirming the player press the button to fire a signal to proceed to the next scene, such as :

func _on_Button_pressed():
    var target_scene = "Opening Scene"
    freeScene(current_scene)
    loadNewScene(target_scene)

But since the node isn't actually in the MainNode (It will be added as a child after _ready() ), I can't find a way to emit a signal from "Mainmenu.tscn" to MainNode, because initially it's not in the MainNode's tree.

Is there a way to make a signal to a different tree? Or is there a alternative solve for this?

Shortcake
  • 1
  • 1
  • Signals are inherited from `Object` and can be connected and emitted even when the target `Node` is not in the scene tree. I recommend updating your post with the code that is causing the issue and describing your desired outcome. – hola Oct 12 '20 at 02:39

2 Answers2

0

I believe you are a bit confused about what a tree is in Godot.

By default you have one SceneTree, which holds the game loop. And scenes can enter or leave that tree (by calling respectively add_child and remove_child on a node inside the tree).

Now, I believe the best way to solve your is issue is by giving all your modules (any node which is not MainNode if I understood your design properly) the responsibility to call whatever they need from MainNode. In other words, your modules could depend on MainNode but not the other way around.

It means that MainMenu could have code that looks like that:

var _main_node: MainNode

func _enter_tree():
    _main_node = get_parent()

func _on_Button_pressed():
    var target_scene = "Opening Scene"
    _main_node.loadNewScene(target_scene)
    _main_node.freeScene(_main_node.current_scene)
Er...
  • 526
  • 4
  • 10
0

If I understand your problem correctly you try to connect to a child signal dynamically at runtime. You can do that in code:

Connecting to signals in code

Secondly I believe you try to use this to change the level. I believe a more Godotic way can be found here:

How to load and change scenes