Godot 4 A separate file with a lot of lists

Godot 4

I want to make a separate file with a lot of list, vars etc. how can i access to these info in my main script?

u can use FileAccess to create files. FileAccess — Godot Engine (stable) documentation in English

If you mean a script to store some of your variables at runtime (not something you want to store when the game closes), then there are two main ways of doing it.

Solution 1: Autoload


In the project settings (see image above), you can add scripts or scenes as autoloads. This means they’ll be automatically loaded in when the game starts, and will be accessible from any other script in the game. For your use case, I’d suggest doing the following:

  1. Open the autoload menu shown above (screenshot is from Godot 4.3, might look slightly different in other versions)
  2. In the Node Name field, type in Globals, and then press the Add button. This will prompt you to choose where to save your new script.
  3. Once you’ve saved the script somewhere, add some variables to it.
  4. From any other script, you can now access these as Globals.some_variable_name.

Solution 2: Just put the script on a node

If it annoys you that the autoload is accessible anywhere - say, if you want some variables accessible in a menu scene, and a different set available in the actual game - then you can instead do this:

  1. Create a script for your variables, and give it a class_name:
class_name MyVariables extends Node

var my_variable : int = 5
  1. Add it to a node in your scene
  2. In the script where you want access to these variables, you can do this:
@export var my_variables : MyVariables

func _process(delta):
    print("My variable is now: " + str(my_variables.my_variable))
    my_variables.my_variable += 1
  1. Select whatever node the script from step 3 is on. It should have a field called “My Variables” (from the export variable in the script), which should have an Assign button. Click on that button, and select the node you created in step 2.

The result of all this is that the script from step 3 gets a reference to the script from step 1, and will therefore be able to access all its variables. This is a bit more limited than the autoload solution, because you manually have to set up the reference. It’s also possible to get a reference to a specific node (and its script) via code, but this is already a long post, so I won’t get into that for now.

2 Likes

For example, if i have a @export variables, when i use them in other script they are null, how can i solve that?
Thanks

Each export variable stores in each scripts, if you want to share it in other scripts then you can store this in a autoload script and then you can access the variable from any script.

Thats what i’m doing but when i call a @export variable it’s null, and all the other variables are ok except the @export ones.

1 Like

Do you added a value in export variable? In the node property

Yes i did’t, i don’t know whats worng. I thought it was because the variable it’s call before it is loaded or smething like that.

Call? Can you show a screenshot of the codes, I think you not using this properly

image
Look

Do you autoload the script or the scene?

1 Like

Mmm the scene.

try to use @onready instead of @export. export sometimes has weird behaviour

I mean the script* jajaja

Well if you want children, you have to autoload the scene