I am trying to access my Options Variables that is in main_menu scene form my Game level scene, since i am changing scenes with get_tree().change_scene_to_file(“res://Scenes/game_level_pvp.tscn”) i cannot access to nodes or use @onready var **** = %**** or anything. i tried to use Autoload but i seems like is loads the variables once and it doesnt update. is there a way i can access changed options variabes form another script? i am new to godot so i dont know much about engine or gdscript, just the bare minimum to make a simple Pong game
change_scene_to_file() simply swaps the current scene in the scene tree from one node to another. That is, if you run SceneA first and then change to SceneB, you replace the instance of SceneA with an instance of SceneB, consequently losing access to all variables inside SceneA. An AutoLoad circumvents that problem by being a sibling of the current scene, i.e. it’s located on the same level in the tree and is not swapped out when you change scenes. So it is the right tool for the job here.
What do you mean by “it doesn’t update”. Let’s say your AutoLoad is called Options and contains a variable ball_speed, then you can access it from anywhere via Options.ball_speed. It’s a normal variable, so if you want it to change you have to provide a value, e.g.: Options.ball_speed = 500.
my options.gd script is located under main menu scene which doesnt contain my levels, so i made my options.gd autoload. when i am at main menu and change my opstion settings such as ball_speed it changes the variable as i can see with a print() but the problem is after i switch the scene. in the switched scene it reads the variables from the autoload as they are defined and not the as updates ones from when i updated them in my main menu. so it seems like my variables change but i cannot access these changed variables from another scene that doesnt have another connection with the script other than autoload which reads them as their defined values.
Autoloads are automatically added to the tree. So if you add the same script somewhere else again (like in your case: as a child of the main menu scene), it will be a separate instance with separate variables that won’t outlive the instance (which will still get freed alongside the main menu scene once you change the scene).
So remove the options.gd script from your main menu scene, and use the global one instead. Main menu scene sets them, game scene reads them out:
options.gd (only used as an autoload, nowhere else)
extends Node
var ball_speed := 250 # default value
main_menu.gd
extends Node
func _ready():
Options.ball_speed = 500 # set a new value
game.gd
extends Node
func _ready():
print(Options.ball_speed) # will be 500