This is a great tutorial because you dont just tell us the 'what' and the 'how' of it. You explain why to use it. So many tutorials just tell you how to do the thing but dont explain why its useful or how to even implement whatever the tut is for.
Ha, well that's something at least! It can be highly dependent on your game, so it's really just a tool to keep in your toolbox you can pull out if you ever find yourself thinking "X should always happen when I set or read this variable" UI updates are one of my main uses for setters, for instance.
export(bool) var is_lit = false setget switch func switch(condition): is_lit = condition func _process(_delta: float) -> void: $OmniLight.visible = is_lit can you tell me why this doesnt work in godot 4? Ive changed the first line to @tool @export var is_lit:bool: get: return is_lit set(value): is_lit=value I cant get my tool button to turn the omnilight visibility on and off
May just want to study general programming concepts a bit more, but it really just comes down to "I have some code that needs to run when this variable is accessed" so it's highly game dependent.
oh, man, setters and getters in GDScript are so convenient! I used to really dislike using them in C# because their syntax was kinda clunky, but this is one area where GDScript really knocks it out of the park. Thank you for highlighting this!
But how do you use the setter and getter functions after declaring it, it's not like you named them. Is it _get_value()? Or value.get()? or something entirely different? Edit: Okay i found out, it's get("value") and set("value", new_value)
That works, but you also DON'T have to reference them directly. When you do "value = x", it will automatically run the setter function under the hood. That's kind of the idea behind setter and getter functions. Externally, you don't have to know / care how you get or set a value. You just use the variable like normal and it works.
I was thinking of making a property read only for an external user by not assigning a new value in setter. But if setters are now also applied to all the internal calls it's not going to work 🤔
Yeah, that was the one nice thing about getter and setter functions in Godot 3. Now, I suppose you'd probably have to have an internal variable that you can access externally via a different name to get the same functionality? Something like: var _internal_var = 5 var external: get: return _internal_var
@@TheShaggyDev that doesn't stop me from accessing this internal variable from the outside though. "If something can go wrong, eventually it will". If I'd trust myself that much I won't need a read-only property in the first place 😅
The new property syntax is very neat I think. I haven't switched my latest project to Godot 4 yet but may well do now that Beta 2 is out. How about you Shaggy Dev?
Agreed that it's solid! A lot better than in Godot 3. And nah, I'm not switching until after the production release of 4.0, other than checking it out for this channel. I'm too impatient to deal with beta bugs 😅
Are you referring to the functions on the Object class? docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-set I've typically seen those functions used to customize how values appear in the editor and how they get written back, but I'm not overly familiar with them.
@@TheShaggyDev Thank you for responding! I am trying to override a setter for a variable in an inherited script but i'm having a hard time making the _set() work, but i'll try more things and reply the answer if i'm successful in case anyone is having the same trouble.
I like this syntax in Godot 4 much more, looks cleaner and easy to understand
Me too! I find this to be much more readable
This info about context was exactly what I was looking for. Thank you!
This is a great tutorial because you dont just tell us the 'what' and the 'how' of it. You explain why to use it. So many tutorials just tell you how to do the thing but dont explain why its useful or how to even implement whatever the tut is for.
Thanks! I try to provide a fuller picture of the stuff I talk about :)
THANK YOU! I was going crazy dealing with Resources and it solved all the problems
The last examples for getters were awesome
Thanks! I tried to provide some better examples than in my Godot 3 video based on viewer feedback and suggestions.
@@TheShaggyDev awesome
That last bit is gold, les go
Great, concise tutorial! Looking forward to more about Godot 4 as I learn it
and now setget finally makes perfect sense to me lmao. thanks for that
I still don't understand how these functions would be used, but at least I know the syntax for them in Godot 4!
Ha, well that's something at least! It can be highly dependent on your game, so it's really just a tool to keep in your toolbox you can pull out if you ever find yourself thinking "X should always happen when I set or read this variable" UI updates are one of my main uses for setters, for instance.
export(bool) var is_lit = false setget switch
func switch(condition):
is_lit = condition
func _process(_delta: float) -> void:
$OmniLight.visible = is_lit
can you tell me why this doesnt work in godot 4? Ive changed the first line to
@tool
@export var is_lit:bool:
get:
return is_lit
set(value):
is_lit=value
I cant get my tool button to turn the omnilight visibility on and off
Hmm, try reloading the scene and/or editor. Sometimes that can get wonkiness with Tool scripts fixed.
Where do I go to learn to understand this?
May just want to study general programming concepts a bit more, but it really just comes down to "I have some code that needs to run when this variable is accessed" so it's highly game dependent.
Bro, whyd they have to change literally everything in godot 4
Too fast explanation. Didn't understand. It's a pity.
i tried to change the property of a node with a set and when trying to get the node it just got null
oh, man, setters and getters in GDScript are so convenient! I used to really dislike using them in C# because their syntax was kinda clunky, but this is one area where GDScript really knocks it out of the park. Thank you for highlighting this!
Thank you! I was having a hard time remembering the syntax for the first method of setget in 4.0. Apparently both methods have their merits.
Nice examples, I was wondering how setting up a health bar in Godot 4 is different from 3.5
Thank you for this video, this is exactly what I was looking for.
But how do you use the setter and getter functions after declaring it, it's not like you named them.
Is it _get_value()? Or value.get()? or something entirely different?
Edit: Okay i found out, it's get("value") and set("value", new_value)
That works, but you also DON'T have to reference them directly. When you do "value = x", it will automatically run the setter function under the hood. That's kind of the idea behind setter and getter functions. Externally, you don't have to know / care how you get or set a value. You just use the variable like normal and it works.
Thanks!
clear and to the point tyvm
love these!
I was thinking of making a property read only for an external user by not assigning a new value in setter. But if setters are now also applied to all the internal calls it's not going to work 🤔
Yeah, that was the one nice thing about getter and setter functions in Godot 3. Now, I suppose you'd probably have to have an internal variable that you can access externally via a different name to get the same functionality? Something like:
var _internal_var = 5
var external:
get:
return _internal_var
@@TheShaggyDev that doesn't stop me from accessing this internal variable from the outside though. "If something can go wrong, eventually it will". If I'd trust myself that much I won't need a read-only property in the first place 😅
@@badunius_code Lol. Don't blame you at all!
The new property syntax is very neat I think. I haven't switched my latest project to Godot 4 yet but may well do now that Beta 2 is out. How about you Shaggy Dev?
Agreed that it's solid! A lot better than in Godot 3. And nah, I'm not switching until after the production release of 4.0, other than checking it out for this channel. I'm too impatient to deal with beta bugs 😅
Hey cool video, can you explain how _set and _get works?
Are you referring to the functions on the Object class? docs.godotengine.org/en/stable/classes/class_object.html#class-object-method-set
I've typically seen those functions used to customize how values appear in the editor and how they get written back, but I'm not overly familiar with them.
@@TheShaggyDev Thank you for responding! I am trying to override a setter for a variable in an inherited script but i'm having a hard time making the _set() work, but i'll try more things and reply the answer if i'm successful in case anyone is having the same trouble.