#How do I setget in Godot 4

8 messages · Page 1 of 1 (latest)

wet pelican
#

here is some working code:

var hearts = 4:
    get:
        return hearts
    set(new_value):
        set_hearts(new_value)

of course, with this new syntax you don't even need to call the setter anymore (if you don't need it as a separate function) but instead can also directly write the code in the setter:

var hearts = 4:
    get:
        return hearts
    set(new_value):
        hearts = new_value
        // do whatever you want here
wet pelican
#

You can't call the function of you delete it. Either have a function that you call, or have the code directly in the setter.

#

Also if you use a function, then you can't use the hearts variable as that would recursively call the setter. You would then use a separate backing variable.

#

You would just set the value instead of calling a function, like this:

PlayerStats.connect("health_changed", func(h): self.hearts = h)
#

(that is using a lambda that is called when the health changes and then sets the health)

#

The best you can do is probably reading all the GDScript progress reports in the news section of the Godot website to get an idea of how the new GDScript works. Without that you will have trouble porting any code at all. As Godot 4.x isn't stable yet the docs are only partially updated. Do check them out though, they explain quite a lot already! But it's definitely a tough job porting a full project during beta testing phase

#

Any reason for switching to 4.x then? Godot 3 is quite nice to work with and for the next time still the current version, so if you're just starting out then Godot 3 may be more fun

#

yeah I can understand those reasons, have fun!