Overriding a constant/static variable from inherited class without instantiating

Godot Version

4.3

Question

Hi!
Does anyone know a good way to (effectively) override constants or static variables in an inheriting class without instantiating the class? This is the effect I would like to achieve:

# script 1
class_name ParentClass
extends Object

static var biz := 0
# script 2
class_name ChildClass
extends ParentClass

static var biz := 1

I’ve tried setting the variable in _init() but because the class is never actually instantiated the function isn’t called, and this obviously wouldn’t work with constants because they cannot be set after their definition.

It works with functions, maybe that’s enough for your usecase?

class A:
	static func testf(): return 1

class B extends A:
	static func testf(): return 2

func _ready():
	print(A.testf()) # 1
	print(B.testf()) # 2
1 Like