Greetings, my first post !

I started with Godot about 3 days agio, have enjoyed the awesome documentation, tutorials, videos, demo etc ... There's quite a wealth of information available for this gorgeous engine.

Question at hand is this; I'm having problems finding any information regarding global namespacing. I'm finding it hard to believe we can carry no global variables or class/types to another scene.

Maybe it'll be better if I first explain my problem.

I have my entry scene setup, it actually handles which scene to load ( via the level or map number the player is on ). Once that map is completed I return back to the main entry scene ( the original that loads up on fresh start ), and branch off to another map (scene).

The problem is without being able to carry a global with me I have no way to keep track of what map number, player inventory, score or whatever.

I've been stumped on this for a day now, I turn to the wealth of this forum for some guidance. :)

\hank you.

Oooohh, not actually globalized namespace but hey ... it will serve the purpose!

Thank you very much for pointing towards that article,.

So everything is global in Godot already. There is no concept of private members and everything is accessible to any other Node in the scene, provided it is in the Scene Tree (a child on the Root Node). However, when you change scenes, you are replacing the Root Node, and thus all data is lost.

Autoloads (aka Singletons) are the easiest method to do what you want. They are classes or Nodes that stay resident in memory the whole time your game runs, and can hold data that persists between level or scene changes (such as the player's life amount or ammo, stats and inventory, etc.).

Another way is just to not replace the Root Node ever. So under the Root would be some "Global" classes and then under that is the game level. You can remove the level using remove_child() (and queue_free()) and then dynamically load and replace the old level with add_child(). This is a little more work as you have to do it manually, but it gives you more control, and would probably be necessary for a large open world game.

Thank you 'cybereality', for that in-depth explanation. So basically globalizing a handful of variables in the root node VS auto-loading a custom Global.gd is pretty much the same thing - other then the root node option requiring a bit more work when loading and unloading maps.

I like both methods to be honest, and as I continue to dig deeper and deeper into Godot I'm certain I'll eventually see the pros/cons of each option.

I can't express enough how much I'm in love with this Godot package, I'm just pissed that I only stumbled onto it a few days ago.

I just create a script call it "Global.gd" then in project settings add that to autoloads. Then in any other script I can access any variables in it by typing "Global." plus the variable name.

example: if I had a variable called life in Global.gd

Var life=99

then in anotherscript I could call it as: var mylife=Global.life

Yeah, the Autoload is the best method, and you can use it like @jbskaggs explained. This is easy and built-in and will always work. The second method I told you just so you can have an idea, but it is more advanced and only really used for larger, open-world kind of games where you need to add or remove only parts of a scene.

@cybereality said: So everything is global in Godot already. There is no concept of private members and everything is accessible to any other Node in the scene, provided it is in the Scene Tree (a child on the Root Node). However, when you change scenes, you are replacing the Root Node, and thus all data is lost.

Pretty sure that's not quite right, the ROOT node is the viewport everything is parented under and rendered to. This is talking about the actual runtime scene-graph, I'm going to assume you meant the "root" of the "scenes" we make in the editor tho, in which case it's valid, though I'd avoid calling those root in godot.

edit: typo.

Well you have access to the real root viewport and can modify it (that's how I got Godot Super Scaling working). So everything is accessible at all times to all nodes in the tree.

But yes, the root viewport is not replaced when changing scenes, so I could have been more clear.

Any idea why I get an error when i call this in my process() function.

		var distance2Hero = ent2.get_global_pos().distance_squared_to(ent.get_global_pos())

error reads: Invalid call. Nonexistent function 'get_global_pos' in base 'MeshInstance'. No matter where i place get_global_pos in my code I get this error.

Try replacing get_global_pos() with global_transform.origin. The global_position property is only for Node2D-based nodes. You can also try using translation instead of get_global_pos(), but translation is the position of the node relative to it's parent while global_transform.origin is the position of the node relative to the world origin ((0,0,0)).

Bingo, I must of been in the 2D Docs when looking up distance information. Much appreciated for all you guys help.

@UpAllNight said: Bingo, I must of been in the 2D Docs when looking up distance information. Much appreciated for all you guys help.

Yeah- this happens to me alot. Sometimes the detailed examples I find are usually only in 2d. So when I write my game from 2d over to 3d I had a lot of errors like this. But eventually we figure it out.

a year later