How to use setter and getter functions in Godot 4

Поделиться
HTML-код
  • Опубликовано: 28 сен 2024

Комментарии • 39

  • @refeals
    @refeals 2 года назад +9

    I like this syntax in Godot 4 much more, looks cleaner and easy to understand

    • @TheShaggyDev
      @TheShaggyDev  2 года назад +1

      Me too! I find this to be much more readable

  • @megalukes
    @megalukes 7 месяцев назад +2

    This info about context was exactly what I was looking for. Thank you!

  • @HojuMedia
    @HojuMedia 2 года назад +29

    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.

    • @TheShaggyDev
      @TheShaggyDev  Год назад +6

      Thanks! I try to provide a fuller picture of the stuff I talk about :)

  • @mudinlange
    @mudinlange 3 месяца назад

    THANK YOU! I was going crazy dealing with Resources and it solved all the problems

  • @VickylanceMedia
    @VickylanceMedia 2 года назад +4

    The last examples for getters were awesome

    • @TheShaggyDev
      @TheShaggyDev  2 года назад +2

      Thanks! I tried to provide some better examples than in my Godot 3 video based on viewer feedback and suggestions.

    • @VickylanceMedia
      @VickylanceMedia 2 года назад

      @@TheShaggyDev awesome

  • @accumulator4825
    @accumulator4825 Год назад +1

    That last bit is gold, les go

  • @bentheking997
    @bentheking997 2 года назад

    Great, concise tutorial! Looking forward to more about Godot 4 as I learn it

  • @davidjohnhamm
    @davidjohnhamm Год назад

    and now setget finally makes perfect sense to me lmao. thanks for that

  • @Kio_Kurashi
    @Kio_Kurashi 2 года назад +1

    I still don't understand how these functions would be used, but at least I know the syntax for them in Godot 4!

    • @TheShaggyDev
      @TheShaggyDev  2 года назад +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.

  • @nicksieben8142
    @nicksieben8142 Год назад

    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

    • @TheShaggyDev
      @TheShaggyDev  Год назад

      Hmm, try reloading the scene and/or editor. Sometimes that can get wonkiness with Tool scripts fixed.

  • @444-w8k
    @444-w8k Год назад

    Where do I go to learn to understand this?

    • @TheShaggyDev
      @TheShaggyDev  Год назад +1

      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.

  • @proximacentauri1172
    @proximacentauri1172 9 месяцев назад

    Bro, whyd they have to change literally everything in godot 4

  • @saulnores3477
    @saulnores3477 2 года назад +1

    Too fast explanation. Didn't understand. It's a pity.

  • @bigass97
    @bigass97 Год назад

    i tried to change the property of a node with a set and when trying to get the node it just got null

  • @testoftetris
    @testoftetris 2 года назад +11

    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!

  • @Xero_Wolf
    @Xero_Wolf 2 года назад +4

    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.

  • @VoltitanDev
    @VoltitanDev Год назад

    Nice examples, I was wondering how setting up a health bar in Godot 4 is different from 3.5

  • @miaoumixed4268
    @miaoumixed4268 6 месяцев назад

    Thank you for this video, this is exactly what I was looking for.

  • @LocherYT
    @LocherYT Год назад

    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)

    • @TheShaggyDev
      @TheShaggyDev  Год назад +4

      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.

  • @RodrigoSelis
    @RodrigoSelis Год назад

    Thanks!

  • @wakkowb
    @wakkowb Год назад

    clear and to the point tyvm

  • @HomeOnTheEdge
    @HomeOnTheEdge 2 года назад

    love these!

  • @badunius_code
    @badunius_code Год назад

    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 🤔

    • @TheShaggyDev
      @TheShaggyDev  Год назад

      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

    • @badunius_code
      @badunius_code Год назад

      @@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 😅

    • @TheShaggyDev
      @TheShaggyDev  Год назад

      @@badunius_code Lol. Don't blame you at all!

  • @GDScriptDude
    @GDScriptDude 2 года назад

    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?

    • @TheShaggyDev
      @TheShaggyDev  2 года назад +1

      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 😅

  • @caveirainvocada9438
    @caveirainvocada9438 Год назад

    Hey cool video, can you explain how _set and _get works?

    • @TheShaggyDev
      @TheShaggyDev  Год назад +1

      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.

    • @caveirainvocada9438
      @caveirainvocada9438 Год назад

      @@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.