Set / Get functions in Godot 4! | Quick Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 พ.ย. 2024

ความคิดเห็น • 41

  • @DayumAli
    @DayumAli 6 หลายเดือนก่อน +7

    Subbed for 0:23. Still watching the whole vid. BASED

  • @juliangutierrez3734
    @juliangutierrez3734 ปีที่แล้ว +4

    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 🎉

  • @olson5000
    @olson5000 7 หลายเดือนก่อน +1

    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!!

  • @Heulerado
    @Heulerado 10 หลายเดือนก่อน +3

    Appreciate the "just the code" section! I'll use it, so here's a comment and a like in return

  • @Muphet
    @Muphet ปีที่แล้ว +5

    you can also write: *var health: float = 100.0 setget _set_health, _get_health* (but you need to mind the order)

    • @SnowPeaGames
      @SnowPeaGames 5 หลายเดือนก่อน +1

      Doesn't work as of Godot 4.0, they have to be separate.

  • @Tim0909
    @Tim0909 16 วันที่ผ่านมา

    OMG IT FINALLY CLICKED. THANK YOU!!!

  • @CodingQuests
    @CodingQuests ปีที่แล้ว +1

    very good tut :) keep it up

  • @NocturnalPenguin
    @NocturnalPenguin 6 หลายเดือนก่อน

    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?

    • @queblegamedevelopment4143
      @queblegamedevelopment4143  6 หลายเดือนก่อน

      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.

  • @CaiooliveiraRAWR
    @CaiooliveiraRAWR 5 หลายเดือนก่อน

    thanks!! im here just for the code.

  • @ISamAtlas
    @ISamAtlas 4 หลายเดือนก่อน +2

    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?

    • @queblegamedevelopment4143
      @queblegamedevelopment4143  4 หลายเดือนก่อน +1

      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 :)

    • @ISamAtlas
      @ISamAtlas 4 หลายเดือนก่อน

      @@queblegamedevelopment4143 WAIT... so... the functions are triggered simply by changing the variable? also thank you for replying to my very menial questions

    • @queblegamedevelopment4143
      @queblegamedevelopment4143  4 หลายเดือนก่อน +1

      @ISamAtlas yup, that's exactly what they do! And no problem haha

  • @VaunaKiller
    @VaunaKiller 5 หลายเดือนก่อน

    Shorter syntax:
    var hp = 10:
    set(value):
    hp = value
    get:
    return hp

  • @DrNabeel20
    @DrNabeel20 ปีที่แล้ว

    Thank you!

  • @EthernalOynuyor
    @EthernalOynuyor 10 หลายเดือนก่อน

    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

  • @Majewstic_
    @Majewstic_ 8 หลายเดือนก่อน

    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!

    • @queblegamedevelopment4143
      @queblegamedevelopment4143  8 หลายเดือนก่อน +1

      Thanks!
      You should checkout one of latest videos on making the Godot Editor look beautiful!

    • @Majewstic_
      @Majewstic_ 8 หลายเดือนก่อน

      Nvm, found your "Custom Themes!" video!

    • @Majewstic_
      @Majewstic_ 8 หลายเดือนก่อน

      @@queblegamedevelopment4143Eyoo bet! :D

  • @timeSlidrGaming
    @timeSlidrGaming ปีที่แล้ว

    Is the checkerboard at the bottom where the taskbar was? You can toggle full screen by pressing Shift + F11. Great video, btw :)

    • @queblegamedevelopment4143
      @queblegamedevelopment4143  ปีที่แล้ว +1

      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!

  • @VoiceChattin
    @VoiceChattin 11 หลายเดือนก่อน +1

    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?

    • @queblegamedevelopment4143
      @queblegamedevelopment4143  11 หลายเดือนก่อน

      Hmmm, I think this means you're not setting the source variable inside the function?

    • @VoiceChattin
      @VoiceChattin 11 หลายเดือนก่อน

      @@queblegamedevelopment4143 thanks!

  • @AgriasOaks99
    @AgriasOaks99 ปีที่แล้ว +1

    Does it work the same way for 3.X?

  • @serghonest9
    @serghonest9 10 หลายเดือนก่อน

    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?

    • @queblegamedevelopment4143
      @queblegamedevelopment4143  10 หลายเดือนก่อน +1

      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 :)

  • @codewithmax
    @codewithmax ปีที่แล้ว

    This code will works without set and get parts in variables line?

    • @queblegamedevelopment4143
      @queblegamedevelopment4143  ปีที่แล้ว

      No, you'll need to define either set, get, or both on the variable in order for this to work

    • @ThatSommeS
      @ThatSommeS ปีที่แล้ว

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

    • @dibaterman
      @dibaterman ปีที่แล้ว +1

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

  • @JebaliJihed-g5c
    @JebaliJihed-g5c ปีที่แล้ว

    very small complaint, the voice is very low try to get it higher so everybody can hear next time . and nice tutorial

    • @queblegamedevelopment4143
      @queblegamedevelopment4143  ปีที่แล้ว

      Thanks for the feedback, I'll try to work on that 👌

    • @Wisdawms
      @Wisdawms ปีที่แล้ว +1

      It seems fine for me

    • @Feral_Sage
      @Feral_Sage ปีที่แล้ว

      ​@@queblegamedevelopment4143talk like Mario 😂

    • @zdrmlpzdrmlp7672
      @zdrmlpzdrmlp7672 9 หลายเดือนก่อน

      it seems fine for me too