How do I override/shadow the setter/getter of a variable in a child class in GDScript?

You can use the getter/setter alternative syntax and override the getter/setter functions in the extended scripts:

Example:

extends Node


var my_var:float: get = get_my_var, set = set_my_var


func _ready() -> void:
	my_var = 5
	printt(name, my_var)


func get_my_var():
	return my_var


func set_my_var(value):
	my_var = value

Extended script:

extends "res://script.gd"


func get_my_var():
	return my_var * 2

It will print:

Child	10 
Parent	5

as Child has attached the extended script.

1 Like