Dont understand how get_node() works
Asked Answered
S

4

0

Hello there,
I am quite new to Godot and am having a blast trying out new things with it, but currently I am stuck with a problem that I can't seem to figure out, although it seems pretty simple. Assume a project structured like this:
res://
--levels (folder)
----level1.tscn
----level2.tscn
--game (Node2D)
----level (Node2D)
----player (KinematicBody2D)

This aren't all the parts of my tree but the others don't matter here. The idea is to keep the levels in a seperate folder to keep everything ordered and tidy, and I want to get the nodes with a script attached to level in order to add and remove them as child to it, depending on what level the game is supposed to show. After researching for 3 hours and trying everything out I find, nothing seems to work as I saw it. Since I need the node in order to it beeing added as a child, the get_node() method is required, but I cant find ANY valid possibility for the path argument to find the levels in the folder. The eror message mostly is the following:
E 0:00:00.338 get_node: (Node not found: "/../levels/level1" (absolute path attempted from "/root/game/level").)
<C++ Error> Method failed. Returning: nullptr
<C++ Source> scene/main/node.cpp:1462 @ get_node()
<Stack Trace> level.gd:13 @ _ready()
I think it has something to do with the path being kinda "fixed" to /root/game/level, but I have no idea to fix it. I also tried lots of variations with get_tree(), get_parent(), the "../.." method but nothing worked out for me.
Does anyone have an idea how to corretly implement this and furthermore, is this even a good project structure or should i manage this in a different way?

Shem answered 3/12, 2022 at 11:34 Comment(0)
P
0

So get_node() works like the file browser on your operating system. If you are familiar with the command line and changing directories like cd documents cd games cd ..

However, the get_node() function operates on the scene tree (this is what you see on the left side of the inspector in the editor). The box on the bottom left if the file system, the files on your hard drive in the project folder.

The files in the project folder mean they are ready to be used in the engine, but they are not in the game yet, they are still on your hard drive. You have to load them first. Either by dragging them onto the scene tree (drag and drop with the mouse) or loading them manually with code usingload() or preload(). To load something you can do this at the top of the file (under the extends and classname).

onready var level_1 = preload("res://levels/level1.tscn")

Now you can use the object level_1, which already is a node, you don't need get_node(). Get node is for things already in the scene tree, meaning visible in the inspector on the left.

Pockmark answered 3/12, 2022 at 12:28 Comment(0)
S
0

Pockmark
Thanks a lot! I'm just wondering how to structure my project the best, and I thought if I would put the levels in the scene tree they would take up a lot of loading time since they all would be loaded all the time, and additionally if I were to put them in there, wouldn't they all show up in the game? I'm just curious since I am new to Godot and still figuring things out ^^
Or do you have any advice on any better method to store and access lots of levels?

Shem answered 3/12, 2022 at 12:34 Comment(0)
P
0

Yes, you would keep them in files like you have and load them with code. Like this:

get_tree().change_scene("res://levels/level1.tscn")
Pockmark answered 3/12, 2022 at 12:51 Comment(0)
S
0

Pockmark
Ok, that helps a lot, thank you!

Shem answered 3/12, 2022 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.