I have some scripts that read and modify member variables of a script globally registered with class_name
. However, they call the class directly via name, without ever instantiating it (eg. ClassName.variable = 1
instead of var class = ClassName.new(); class.variable = 1
)
These modifications persist across scenes, so I'd like to know; what exactly is happening to this class under the hood? Is it secretly being instantiated each time I call it in script? Or are its parameters just being stored a single time in memory, waiting to be instantiated with the changes?
This question might be unclear without example, so I'll post one similar to what I'm doing in my project. I ask this mainly because I chose to extend the built-in Object
class, and I'm wondering if this repeated class calling is inadvertently creating objects and causing memory leaks.
# player_stats.gd
class_name PlayerStats
extends Object
static var health = 10
# player.gd
class_name Player
extends CharacterBody2D
func heal(amount):
PlayerStats.health += amount