Static variables and functions
The next keyword we will look at is static
. We can declare a variable or function as static by putting this keyword in front of it:
class Enemy: static var damage: float = 10.0 static func do_battle_cry(): print("Aaaaaargh!")
Static variables and functions are declared on the class itself. This means that they can be accessed without creating an instance of the class:
print(Enemy.damage) Enemy.do_battle_cry ()
Static variables are made to contain information that is bound to a complete class of objects. But watch out – the following are two big gotchas for static variables and functions:
- In GDScript, static variables can be assigned a new value, and you can change them during the execution of the game. Ideally, you don’t want to do this because it can impact your program in ways that are hard to debug.
- From a static function, you can call...