Hey, I'm new but come from GameMaker. Godot seems to do many same stuff easily and even better too. But there is at least one thing I am still too stupid to understand: how should I refer other objects (scenes) and affect their stuff like variables? I know there are at least groups and signals (which I think I somewhat understand but I find them complicated and still cannot apply them well). And there probably should be some other functions to refer other scenes too, but I don't know if there are any and how to search for them. Do you know any?

I would like to have some sort of overview about this issue. What are the ways to affect a scene from another? And if you have patience to tell how they work, ELI5. If you are still unsure what I want to do, in GM there are mostly two simple ways referring and changing stuff in other objects (scenes):

with (obj_other) {stuff = 1} obj_other.stuff = 1

(Even though I'm dummy coder I'd prefer to apply this exclusively via code in Godot too.)

Thanks in advance

In general, you would use "get_node()" to get the node (it can be another scene or object) and then you can access the properties or member function on that node.

If the script is on the top root node, you can find a child node like this:

get_node("Player")

For two levels deep, you can do:

get_node("Level/Player")

Or you can start from the root (if the script is far down in the tree):

get_node("/root/Level/Player")

You can save this so you don't have to call "get_node()" all the time:

var player = get_node("Player")
player.points += 10

Hope that helps.

Okay, looks nice! Does this work also for nodes from other scenes (when the nodes don't share the same node tree within a scene)?

Or should I plan my scene hierarchies so that in the end every scene (and their nodes) can be found in one tree? So, if there are now separate scenes, I create a new scene where all the scenes so far become its nodes. Maybe this is a Godot specific thing I still have to understand better...

Think of it like this: A Godot game is just one giant tree. The game starts at the "root" and calculates through the tree attached to it. A "scene" is just a sub-section of that tree.

You can attach and detach scenes to parts of this big tree. Once attached they will be processed and become accessible. If they aren't attached then they aren't.

Instead of "changing rooms" you swap out scenes. Instead of "accessing objects" you reference a branch.

Any leaf or branch can reference any other leaf or branch so long as it is attached to the main tree and you know the traversal to get there. You can use relative or absolute paths, whichever matches your design best.

Godot also has some node searching functions to find nodes if you don't know the path.

You can kind of deign your hierarchy to match GMs design if you want. Have a main node, Game, that you can attach your "rooms" to. Have a scene for each "room" that you swap in and out one at a time. For each room scene you can contain your "object" scenes.

The complexity of your object scenes depends on you. You'll figure out what you like as you go.

As a hint: try to design your scenes so they don't hard-code a reliance on other scenes. If you avoid this then you can test each element individually and swapping things in and out is much easier. Not the end of the world if you do, though.

Lastly, in terms of get_node paths, since some Windows users don't know this, you can backtrack up the tree with "./" to grab a sibling, "../" to grab a parent, uncle, whatever, and "../../" to get a grandparent etc.

Alright. It might be very difficult to always plan scenes/objects without reliance in interaction, GUI elements etc., but now I think I got it. Thanks!

EDIT: One small bonus question: in terms of optimization when referring, is there difference whether you should avoid nodes closer to root/different branch/sub-nodes, or does it matter much?

@Woffelson said: EDIT: One small bonus question: in terms of optimization when referring, is there difference whether you should avoid nodes closer to root/different branch/sub-nodes, or does it matter much?

From what I remember when making my decal system, Godot does slow down a little bit when there are many, many nodes in the exact same location in 3D space. Outside of that, I have not noticed any slowdowns with node placement, in the world or in the scene tree.

@TwistedTwigleg said: Godot does slow down a little bit when there are many, many nodes in the exact same location in 3D space.

This sounds like it's due to Z-fighting, which will happen in any 3D engine.

@Calinou said:

@TwistedTwigleg said: Godot does slow down a little bit when there are many, many nodes in the exact same location in 3D space.

This sounds like it's due to Z-fighting, which will happen in any 3D engine.

Yeah, I think that is probably the case. I found that limiting the number of nodes in the same location helped and that performance was better when farther away from the mass of nodes, which would suggest that Z-fighting is the issue.

Outside of the Z-fighting issue though, I cannot remember ever having an issue with positioning and Godot nodes, while I have had a few minor issues with position in other game engines I have used in the past.

3 years later