This has been the clearest video so far for a newbie like me who has never coded a game! I've tried several tutorials to wrap my head around set and get. SUBBED!!
Thanks! Yeah ofc, the new_value argument corresponds to the "new value" of that variable. So for example, if you set a health variable to 10, the new value is just that, 10. So inside the setter function, 10 will be passed as the new_value or first argument. This is why you need to set health to this new value (if desired), otherwise the health variable will be un-settable since the setter function overrides this functionality.
im here by almost complete chance... I was looking for a tutorial for the set() and get() function for variables... but now that I'm here this looks massively useful, but I just don't know how it would be applicable? Why not just reference the variable itself? And if you want to run a function then why even bother linking it back to the variable it's changing? It would work reguardless wouldn't it?
Glad you found this video! So regarding how the actual uses of this, it's more of The fact that you can execute any code when a variable gets set. This thing is, when you define the setter function, it overrides godot's built in method for actually setting variables. This is why you have to manually add the functionality back into Godot by setting it to yourself. In addition to this though, you're also able to add extra code to this method, so this could be used to execute logic to update a health bar for example when the health actually changes. Hope this made sense to you :)
@@queblegamedevelopment4143 WAIT... so... the functions are triggered simply by changing the variable? also thank you for replying to my very menial questions
I don't understand how can you call another function that it's not going to be available when initializing the variable heatlh. Won't Godot try to access _get_health and give an error because it has not been initialized?
Well Godot will actually load the entire script before searching for the method with setters and getters, so you dont need to think about load order :)
@@queblegamedevelopment4143 I now understand how to set this up, but how can this be triggered and how does it differ from just directly calling the set and get functions?
@@ThatSommeS What you are missing is a "why?" use this, which I think queble didn't answer I think. The main reason to do this is for safety. Lets say you at some point in your game change health from the property. Then the health displayed to the player may not be updated. This ensures that if you call the method or if you change the property all systems that need to know about health are informed.
Subbed for 0:23. Still watching the whole vid. BASED
Thanks for the tut, im having a hard time wrapping my head around this concept, but your video has cleared it a little for me 🎉
This has been the clearest video so far for a newbie like me who has never coded a game! I've tried several tutorials to wrap my head around set and get. SUBBED!!
Appreciate the "just the code" section! I'll use it, so here's a comment and a like in return
you can also write: *var health: float = 100.0 setget _set_health, _get_health* (but you need to mind the order)
Doesn't work as of Godot 4.0, they have to be separate.
OMG IT FINALLY CLICKED. THANK YOU!!!
very good tut :) keep it up
Just found this. Great video. Could you explain the setter parameter a bit more? What does “new_value” do since there’s no value there?
Thanks!
Yeah ofc, the new_value argument corresponds to the "new value" of that variable.
So for example, if you set a health variable to 10, the new value is just that, 10.
So inside the setter function, 10 will be passed as the new_value or first argument.
This is why you need to set health to this new value (if desired), otherwise the health variable will be un-settable since the setter function overrides this functionality.
thanks!! im here just for the code.
Glad I could help! :)
im here by almost complete chance... I was looking for a tutorial for the set() and get() function for variables... but now that I'm here this looks massively useful, but I just don't know how it would be applicable? Why not just reference the variable itself? And if you want to run a function then why even bother linking it back to the variable it's changing? It would work reguardless wouldn't it?
Glad you found this video!
So regarding how the actual uses of this, it's more of The fact that you can execute any code when a variable gets set.
This thing is, when you define the setter function, it overrides godot's built in method for actually setting variables. This is why you have to manually add the functionality back into Godot by setting it to yourself.
In addition to this though, you're also able to add extra code to this method, so this could be used to execute logic to update a health bar for example when the health actually changes.
Hope this made sense to you :)
@@queblegamedevelopment4143 WAIT... so... the functions are triggered simply by changing the variable? also thank you for replying to my very menial questions
@ISamAtlas yup, that's exactly what they do! And no problem haha
Shorter syntax:
var hp = 10:
set(value):
hp = value
get:
return hp
Thank you!
It doesn't work for detecting changes in array ?
The docs says it does but this exact same code does nothing for arrays.
Using Godot 4.2
Hey man! Loved the video thank you so much for it! Could you tell us how you made Godot look like that? I just love the color scheme you got going on!
Thanks!
You should checkout one of latest videos on making the Godot Editor look beautiful!
Nvm, found your "Custom Themes!" video!
@@queblegamedevelopment4143Eyoo bet! :D
Is the checkerboard at the bottom where the taskbar was? You can toggle full screen by pressing Shift + F11. Great video, btw :)
Yeah it is haha, and I would, but I usually switch between windows when recording so I just set this up to keep a standard aspect:)
Thanks!
I get an error saying "function cannt be used as setter because of its signature". Can't seem to find a fix through google. Can you help?
Hmmm, I think this means you're not setting the source variable inside the function?
@@queblegamedevelopment4143 thanks!
Does it work the same way for 3.X?
I don't understand how can you call another function that it's not going to be available when initializing the variable heatlh. Won't Godot try to access _get_health and give an error because it has not been initialized?
Well Godot will actually load the entire script before searching for the method with setters and getters, so you dont need to think about load order :)
This code will works without set and get parts in variables line?
No, you'll need to define either set, get, or both on the variable in order for this to work
@@queblegamedevelopment4143
I now understand how to set this up, but how can this be triggered and how does it differ from just directly calling the set and get functions?
@@ThatSommeS
What you are missing is a "why?" use this, which I think queble didn't answer I think.
The main reason to do this is for safety. Lets say you at some point in your game change health from the property. Then the health displayed to the player may not be updated. This ensures that if you call the method or if you change the property all systems that need to know about health are informed.
very small complaint, the voice is very low try to get it higher so everybody can hear next time . and nice tutorial
Thanks for the feedback, I'll try to work on that 👌
It seems fine for me
@@queblegamedevelopment4143talk like Mario 😂
it seems fine for me too