Setting up Global Scripts in Godot 4 (a.k.a. Autoloads)
When I first started using Godot, I had no idea how to pass data between scenes cleanly. I ended up with messy spiderwebs of node paths, signals, and function calls — just to do something simple like update the player’s score or share a variable.
If that sounds familiar, this tutorial is for you.
I’m going to show you how to set up a global script in Godot using something called an autoload. This lets you create a script that lives in the global namespace — meaning you can access it from any other script in your entire project, no matter where you are in the scene tree.
It's simple to set up, but incredibly powerful. Once I started using it, everything clicked.
Lets create a new script.
Right-click in the FileSystem. Choose Create New then Script...
Lets call it GlobalScript.gd then click Create.
Next click on the Project tab in the top left corner. Then select Project Settings...
Switch to the Globals tab.
Right where you see it say Path: there is a folder icon. Click it. Then select our GlobalsScript.gd file and click open.
Then over to the far right, click the +Add button.
And you are done.
Any variables or functions you write in the GlobalsScript can be accessed from any other script in your project.
Lets say you have a variable called lives.
To access that variable from any other script simply call GlobalScript.lives
It’s that easy.
No more hard-to-manage references.
No more complex rewrites every time you move nodes around in the scene tree.
You’re welcome.
Comments
Post a Comment