Hi everyone,

in my global.gd I am trying to call a node which is a direct child of my root-scene with the following code:

# Calculate the level based on the levels of the scene instances
	level = 0
	for i in range(1, 11):
		# Get the current scene instance
		var instance = get_node("scMain/player_" + str(i))

		# Add the level of the scene instance to the total level
		level += instance.level

Unfortunately it returns the following Error:

get_node: (Node not found: "scMain/player_1" (relative to "/root/global").)

Here my Scene-Structure

Where did I go wrong?
I am fairly new to Coding, so I please excuse if this is obvious.

Thanks already for evereyone helping!

  • jooscher
    Correct me if I am wrong, autoload files are always in root node.
    To get scMain node, use one of these:

    • var instance = get_tree().get_root().get_node("scMain")
    • var instance = get_tree().get_root().get_child(1) #if you know index(position)
    • var instance = get_tree().get_root().find_node("scMain", true, false)

Click the Remote tab while the game is playing and look at the scene tree on the left. It's possible global and scMain are siblings, or something else, you will see.

    cybereality
    Great Tip, they are indeed siblings!
    Can you help me out as to how it should be?

    EDIT: I figured global.gd should be a child of scMain - but I can't figure out how to move it there 🙁

      jooscher
      Correct me if I am wrong, autoload files are always in root node.
      To get scMain node, use one of these:

      • var instance = get_tree().get_root().get_node("scMain")
      • var instance = get_tree().get_root().get_child(1) #if you know index(position)
      • var instance = get_tree().get_root().find_node("scMain", true, false)

        AspireMint
        EDIT: IT WORKS!

        Thanks for the reply!

        You're right to mention that global.gd is autoloaded, apparently I should have mentioned that.
        I tried your solutions but neither of them work 🙁

        Also my Godot ( I am using Godot 3.5.1) doesn't suggest get_root as a possibility when I enter get_

          jooscher Also my Godot ( I am using Godot 3.5.1) doesn't suggest get_root as a possibility when I enter get_

          Are you trying to get_root() on it's own or are you trying to get_tree().get_root()?

            Megalomaniak
            var instance = get_tree().get_root().get_node("scMain/player_" + str(i))

            works! I was too silly earlier and had a typo in my "player_"

              jooscher
              Yay 🙂 but you are right, editor doesn't suggest get_root (in my 3.5.stable version), but root is property of SceneTree, and root getter is get_root. Weird.

              AspireMint Correct me if I am wrong, autoload files are always in root node.

              Autoloaded nodes are children of the root node, and precede the other nodes in the scene tree.

              9 months later

              Im having an similiar problem. And i tried to get the node i want on serveral different ways. First the one's who worked good before, then as you can see in my screeshot i played with any combination i could think of. Im keeping on my research but for now im pretty lost what's wrong here. And what does the "relative to /// mean? Its not the right path, otherwise it should have worked by now.
              Well, okay looks like i could fix the error in just this moment..
              Looks like i can only use the "/root'" inside of get_node() if i put ".root" right after get_tree(), so: get_tree().root.get_node("/root/main_scnene/...). The get_root() function does not work there.

              • xyz replied to this.