Setting up a global for using variables across scenes

Same example as before with a resource:
resource class:

class_name DummyResource extends Resource

@export var property := 0

scene:

extends Node

static var foo := preload("dummy_resource.tres")

func _ready():
	print(foo.property)
	foo.property += 1

func _input(e):
	if e.is_action_pressed("ui_accept"):
		get_tree().reload_current_scene()

output:

0
1
2
3
4
5

I don’t have any script with a static. I briefly had a global autoload with the preload in it but it kept resetting the variable to the preload. As I said in my previous post the only script I have is the one I have provided already.

What are we discussing here then? Do a test with static and report back.

Your autoload didn’t work probably because you set up a script that’s in the scene as an autoload. Autoloaded scripts should not be attached to your scene nodes.

The autoload was not attached to any scene. It was created directly from the file system. One of the first lines of the script I have provided is @export var enviro = preload(“res://enviro.tres”) loading the buttongroup for the environment menu. The current toggled button in the group is what I need to persist. With it being in the autoload instead of the script for the environment menu there was still no persistence. According to you I need to change this to @export static var enviro = preload (“res://enviro.tres”)

You can’t @export static vars, ditto it makes no sense to @export vars in your autoloads because you can’t assign them in the editor.

If you had an autoload script then the script you posted is not the only script.

Post everything you have and describe how it behaves vs how you expect it to behave. Otherwise we’re wasting time here.

Had. Past tense. As in no longer have. I used @export because all the examples I found did.

In any case, you’ve got your solution. You can make it work with either autoloads or static vars. Just need to implement it. I’d vote for the static var route as it’s less bug/confusion prone.

So if I have the static var enviro and I enter a scene containing the script var enviro = preload(“res://enviro.tres”) the static var won’t change?

Look at my last example. Do that and you’ll be fine.

As far as I can tell that’s incorrect as I will be using the variable across multiple scenes and scripts so will need to setup a class for it.

No it’s not incorrect. It’s exactly what you need. From technical standpoint you can put the static var in any named class and all other scripts will be able to access it. That’s the whole point of static vars. It can stay in this class as well if it belongs there from the design standpoint, just name the class for easy access. Or you can have a dedicated “static class” that holds all your static vars. We’ve been through that already: Setting up a global for using variables across scenes - #9 by normalized

After changing it to static var enviro = preload(“res://enviro.tres”) I entered the scene, toggled a different button on, left the scene and re entered it. The buttongroup was back to it’s starting configuration with the original button toggled on, not the one I toggled.

What is enviro.tres?

As previously mentioned it’s the buttongroup for the environment menu and the toggled button is what I need to track across scenes.

Well if you re-load the buttons they’ll reset the state of the button group resource they’re referencing. That has nothing to do with resource object persistency. If you want persistent buttons across the scenes then put the buttons in a separate scene and setup that scene as an autoload. In that case you need persistent nodes/scene, not persistent data and you have to do it with autoloads.

Well that finally seems to answer what I actually asked. So what I’ll do is set up a new root scene where a class is defined for the button group and preload it as a static variable. Once that’s done I’ll change scene to my menus that will update the variable and won’t be reentering the root scene. Therefore my changes will persist in the menus.

You communicated you problem poorly because you asked for persistent data.

No. Create a new scene that contains your buttons. Setup this scene as an autoload and you’re done.

That’s going to be an issue because I’m going to have at least 6 more buttongroups that need to have their toggles persisting between scenes.

Your whole persistent gui should be in a single scene that’s set up as an autoload.

That isn’t an option as there’s no room for all the buttons. Only the record of which button in each group is toggled needs to be persistent between scenes. The actual group itself only needs to be present in the scene that is its specific sub menu.