Project unique nodes? #4601
Replies: 5 comments
-
Seems like the "current" approach is completely fine. Tell me, how would you identify, even across multiple instances of the same exact scene, project-unique nodes? You may also find that thinking with global Node references is prone to tie code like shoelaces and that becomes really tricky to manage as your project increases in scope. |
Beta Was this translation helpful? Give feedback.
-
Yes, the closest I've come to implementing global node references is storing all nodes in a dictionary in a singleton. I just accept the fact that nodes with the same name will overwrite each other in the dictionary (it's fine if they overwrite each other - I only need to make sure my nodes have unique names if I actually want to reference them). On your main scene root node:
_enter_tree() occurs before _ready(), in which we use godot's Nodelist singleton looks like this:
I prefix all my node references with lowercase letter 'o' so that I know what they are and they don't conflict with any node's existing variables. But the prefix is up to you. Then at the top of each node I write onready lines like so:
So you might think this is no different to writing onready NodePaths, however there's some big advantages here:
If you're speaking of "multiple instances" then that should be considered outside the scope of this feature. I accept that there's no good way to uniquely track multiple copies. No, this is about having one individual node that does its own thing, no copies.
GameMaker allows you to reference any object from any other object at all times, they have plenty more prominent games than Godot. This is due to little conveniences that GM provides that Godot doesn't. I don't like you guys trying to protect devs from themselves. |
Beta Was this translation helpful? Give feedback.
-
I don't see much of a difference between this and the AutoLoad (singleton) feature that we already have. |
Beta Was this translation helpful? Give feedback.
-
Scene unique nodes are associated with the Global names are hard to track down as they refer to the whole project (many individual scenes). It is not clear what to do with several different instances that have the same global name. And in general, global variables make code more difficult to understand, unpredictable, and prone to errors. Global names have already been discussed several times: #2812 See also singletons and the find_child and find_children methods. |
Beta Was this translation helpful? Give feedback.
-
I usually make a custom resource where I store the node references and preload it whenever I need it. |
Beta Was this translation helpful? Give feedback.
-
There is new feature - scene unique nodes.
You can make a node in the scene accessible by name.
This is handy but what about more global thing:
project unique nodes?
project.godot
could store the data about these nodes.Currently you can use singletons instead.
you can create a singleton with a var like
my_global_node
then in the script of the "global" node you you type like
but as you can see this is not so handy.
Beta Was this translation helpful? Give feedback.
All reactions