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?
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.
Node Name
field, type in Globals
, and then press the Add button. This will prompt you to choose where to save your new script.Globals.some_variable_name
.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:
class_name MyVariables extends Node
var my_variable : int = 5
@export var my_variables : MyVariables
func _process(delta):
print("My variable is now: " + str(my_variables.my_variable))
my_variables.my_variable += 1
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.
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.
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
Look
Do you autoload the script or the scene?
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