Godot 4.3
I have a static variable counting the number of instances of a scene, I need that variable to reset when the main scene is reloaded, @static_unload is probably what I need but it seems to be bugged at the moment, is there a way for me to manually reset a static variable?
I am not sure what you mean by a static variable in Godot.
However, when your main scene is reloaded, the _ready function would be the place to reset any variables like scene_counter back to zero.
If the counter is in an autoload file, you could alternatively have a initialize_counters function that you call wherever you tell the main scene to reload.
If you mean a const, then these cannot be re-assigned a value. Normally in this situation I would have a const DEFAULT_VALUE set and a variable that is set to the Default Value whenever a reset is called.
const COUNTER_DEFAULT_VALUE: int = 1
var counter_value: int = COUNTER_DEFAULT_VALUE
If I have completely misunderstood your question, apologies in advance.
Assuming you use the static keyword in the scene you load multiple times, create an init function to call
var nonstat_var: int = 0
static var stat_var: int
func init():
stat_var = 1
return self
Anytime you instantiate the scene that contains it, the static variable is not reset. To reset it, you would have to do it call init().
func run_scene():
var node_2d1:Node=load("res://node_2d1.tscn").instantiate().init()
node_2d1.stat_var += 1
node_2d1.nonstat_var+=1
prints(node_2d1.stat_var, node_2d1.nonstat_var)
var node_2d2:Node=load("res://node_2d1.tscn").instantiate()
node_2d2.stat_var += 1
node_2d2.nonstat_var+=1
prints(node_2d2.stat_var, node_2d2.nonstat_var)
# Initialize static and then increment
var node_2d3:Node=load("res://node_2d1.tscn").instantiate().init()
node_2d3.stat_var += 1
node_2d3.nonstat_var+=1
prints(node_2d3.stat_var, node_2d3.nonstat_var)
return