change value of variable without calling setter [Godot 4]

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By OgGhostJelly

I have a variable attached to a setter function and I want to be able to change the variable’s value without calling the setter. they had this functionality in Godot 3 but removed it in Godot 4.


var my_variable: set = _my_variable_setter

func _my_variable_setter(value) -> void:
    my_variable = value
    print("Hello World")

func _ready() -> void:
    my_variable = 12
    >>> Hello World!
    # prints 'Hello World!' when I don't want it to.
:bust_in_silhouette: Reply From: Wildos

As stated in the documentation, it is not possible anymore in Godot 4.0

You can use a proxy variable to do a similar effect (the proxy variable can be use for the getter too if needed):

var my_real_variable
var my_variable: set = _my_variable_setter, get = _my_variable_getter

func _my_variable_setter(value) -> void:
	my_real_variable = value
	print("Hello World")

func _my_variable_getter() -> int:
	return my_real_variable

func _ready():
	my_variable = 12
	print("%d" % my_variable)
	my_real_variable = 15
	print("%d" % my_variable)


Hello World