GET SET...GODOT! Use the Revamped Setter and Getter Functions

Поделиться
HTML-код
  • Опубликовано: 28 сен 2024
  • ▶︎ GODOT 4 COURSES : school.gdquest...
    🎓 FREE APP "Learn GDScript From Zero" : www.gdquest.com
    🎮 Free Interactive Tour - The Godot Editor - www.gdquest.co...
    🗨 GDQuest Discord Community : / discord
    -------------------------------
    🖈 MENTIONED LINKS
    GITHUB CODE EXAMPLE : github.com/gdq...
    -------------------------------
    🖋 SYNOPSIS
    Learn the new syntax for setter functions and explore two examples: Updating player health in a health bar and setting the color of a character in the editor.
    #godot #gamedev #gameengine
    -------------------------------------------
    CC-BY 4.0 LICENSE
    Terms: creativecommon...
    Attribute To: "CC-By 4.0 - GDQuest and contributors - www.gdquest.com/"

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

  • @HypnoSpec
    @HypnoSpec 8 месяцев назад +178

    Missed opportunity to call the video “GET SET… GODOT!!”, but thank you for another one. :)

    • @Gdquest
      @Gdquest  8 месяцев назад +67

      :D I even thought about it! hmm... Maybe I'll still change it.

    • @HypnoSpec
      @HypnoSpec 8 месяцев назад +9

      @@Gdquest Nice!!

    • @user-S853
      @user-S853 8 месяцев назад +12

      @onready

    • @Sylfa
      @Sylfa 8 месяцев назад +5

      Now I'm curious what the original name was…

    • @HypnoSpec
      @HypnoSpec 8 месяцев назад +10

      @@Sylfa It was “GET SET… GO! Use the Revamped Setter Functions in Godot 4”.

  • @computersciencestudentriverbat
    @computersciencestudentriverbat 8 месяцев назад +28

    GDQuest is one of the pillars of the Godot learning community. Nothing but love and respect tho GDQuest crew.

    • @Gdquest
      @Gdquest  8 месяцев назад +4

      Thank you so much for the kind words :) :)

  • @pie75
    @pie75 8 месяцев назад +1

    i think one useful and potentially common usecase for the getter function is actually to combine exported variables. having multiple/duplicated exported variables creates a neater interface for your designers, while combining them stops those multiple/duplicated variables from creating excessive/confusing code for your programmers.
    An example:
    You want to recolour a character, as is shown in the video, or is done in a game like Halo, but that character has an albedo texture.
    Instead of always recolouring the albedo texture after getting the texture, you can write the getter function for the texture, such that it always returns the recoloured asset; assuming you will always want the recoloured texture. (Which is likely, in the case of Halo.)
    Though, i think that in modern times, it would probably be faster for the GPU to handle the recolouring, so you would likely want to stick to combining variables which are not graphical.

  • @Weahl
    @Weahl 8 месяцев назад +3

    I love your videos! Dont stop please, I'm learning a lot 😊

  • @rios7744
    @rios7744 8 месяцев назад +4

    always wondered what was the real purpose of setters and getters outside of java/c++ haha

    • @Sylfa
      @Sylfa 8 месяцев назад

      Properties are available in a lot of languages, and this is essentially a property implementation except with an underlying field to store the data in. They are very, very useful.

  • @FernandoBroca2007
    @FernandoBroca2007 2 месяца назад +1

    I have a question... But I don't know how to make it simpler...
    Let's suppose I have a "health" value and its "set_health" function, what's the difference of binding the function to the set property than not binding it?
    Maybe I'm just confused about something that wasn't clear on the video:
    If I want to set a variable that has a set function, do I need to call the function of the variable or setting the value through operators calls the set function?
    I hope I was clear, my programming vocabulary in general is terrible.

  • @Slapthesushi
    @Slapthesushi 8 месяцев назад

    Can you please make a tutorial for a platformer dash? The other tutorials only have dashes that work when you move

  • @rocketpython
    @rocketpython 8 месяцев назад

    You can write Get or Set only is really convenient. I remember when I used Unity, I had to write them both.

    • @rafasoaresms
      @rafasoaresms 8 месяцев назад

      Really? I remember read-only properties being a thing in C#. But I’ve only really used C# in the .NET framework, not so much in Unity.

  • @brandonjacksoon
    @brandonjacksoon 8 месяцев назад

    Liked and subscribed! Thanks mate

  • @GameUISchool
    @GameUISchool 8 месяцев назад +1

    Great video. thz

  • @michaeljburt
    @michaeljburt 8 месяцев назад

    It's great for addons too, documentation and convenience in updating the editor

  • @unnamedtoaster
    @unnamedtoaster 8 месяцев назад

    I'll try it out myself later when I get the opportunity, but I assume these set get functions are also called when accessing variable properties. For example, if I have a setter for a Vector2 and I type:
    var my_vector: Vector2 = Vector2(1, 1)
    my_vector.x = 3
    It will call the setter fuction with the argument Vector2(3, 1), right?

    • @Gdquest
      @Gdquest  8 месяцев назад

      No, the setter will only be called when the top property changes. Changing internal properties won't trigger it.

  • @S.Korolev
    @S.Korolev 3 месяца назад

    is using get/set a better practice than signals?

  • @jayman1462
    @jayman1462 4 месяца назад

    7:52 does this work with sprites?

  • @Jose.Eduardo.C
    @Jose.Eduardo.C 8 месяцев назад

    I didn't understand if when i change a varialble in another script if i do
    health_bar_carachter.health = x
    will it run the getter function at the beggining of the line and the setter at the end of the line??
    or it will simply not work and ill have to do always
    health_bar_carachter.set_health(x)
    and will never be able to read the variable value until i write a get function?

    • @Gdquest
      @Gdquest  8 месяцев назад +1

      It would call the setter on the left side of the assignment (=)
      It would call the getter on the right side of the assignment (=)
      If there is no getter function, you just read the variable.
      Your question is very logical though.
      A great way to practice and get familiar with some of this is to set up a quick test in your editor.

    • @Jose.Eduardo.C
      @Jose.Eduardo.C 8 месяцев назад

      @@Gdquest Thanks, really!!! Will certainly make experiments to test that out.

  • @Peter-wl5xm
    @Peter-wl5xm 7 месяцев назад

    beautiful thank you

  • @Evitrea
    @Evitrea 8 месяцев назад

    Can you talk about subclass? Seems useful but I can't think of an actual use case

    • @Gdquest
      @Gdquest  8 месяцев назад

      A subclass is a class that inherits from another class. Do you mean inner class?

    • @Evitrea
      @Evitrea 8 месяцев назад

      @@Gdquest yeah my bad, inner class

    • @jirivegner3711
      @jirivegner3711 8 месяцев назад +1

      They can be useful whenever you want to create an object with complex internal structure without exposing this structure. An example is an implementation of a graph (mathematical one), where you can define Vertex and Edge as inner classes inside of Graph class and write methods to manipulate them without working directly with Vertex or Edge classes. The final result should feel similar working with like Dictionary.
      It is of course possible to implement graph in other ways without using any inner classes, but it IMO makes sense to keep classes that are pretty much just boxes for data, fully inside of the master/manager class. It doesn't make much sense to use Vertex or Edge elsewhere in the project, unless you are creating some specialized Graph (map movement calculator, map of diplomatic relations ...)

  • @cellgfx
    @cellgfx 8 месяцев назад +2

    With the inline getter and setter function definitions at 2:15, what are the names of the getter and setter functions that can be called in each case? The coded example using the initial syntax is demonstrated using the defined functions, but the inline version doesn't show this.

    • @keen96
      @keen96 8 месяцев назад

      i believe that the point is precisely not using function calls and simply using the variable as an R or L value. They don't have names because they aren't needed nor wanted.

    • @Sylfa
      @Sylfa 8 месяцев назад

      Indeed, when you define the method bodies right by the variable they are anonymous, they don't have any names nor can you give them a name.

  • @takeshikusao4674
    @takeshikusao4674 7 месяцев назад

    I listened this 4 times already and every time I got lost in the process trying to follow, why and where exactly we need setter and getter, overflowed by example of healing and color picker...

  • @dughdrybhfgdfvgd
    @dughdrybhfgdfvgd 8 месяцев назад

    How can stages be saved on Godot?

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

    Do i need course? I need concrete explanation because I don't have foundation in programming but I'm good at building games just learning in youtube?

    • @Gdquest
      @Gdquest  6 месяцев назад +1

      We don't really like to push our courses on an individual basis because everyone learns differently. What we can tell you from experience is that you should try to pick up programming and learn how to break down game development into bite-size tasks.
      We think courses can be a faster way to learn (not just programming but how to think like a gamedev too). They concentrate knowledge and organize in a single place what would otherwise take years to build up and assemble from pieces.
      If you're not sure, remember you can try the course and you have 2 months to ask for a refund.

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

      @@Gdquest Bite-size task.. okay that's great.. concentrate knowledge in building games... perfect explanation that's what all I need, thank you

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

      @@Gdquest I made it the purchase.. do you guarantee that it will improve my coding skill to be high standard?

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

      Hello!
      While we are sure of the quality of our courses, we cannot guarantee specific outcomes. With education it's never really possible to guarantee anything because even if the learning material is good, the effort that gets you to your goal depends on you: All the practice and the drive to build on the knowledge you're acquiring.
      What we can guarantee is to refund you if you do not find the course beneficial to you. You have up to 2 months to decide.

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

      @@Gdquest Thank for reminding for that.. anyway I live being this kind of environment where you need to take effort everything from scratch.. for many course I bough, not for game development, this is just my early stage.. I become master at that course and always trust my instinct.. If you saying that you are the master of game dev... then we will see each in that stage anyway for future... have fun working with you

  • @skaruts
    @skaruts 8 месяцев назад

    Does Godot automatically know when you're accessing the property inside the setter/getter or something? I had some stack-overflow issues because of that back in 4.0, but maybe it changed in meanwhile...
    Also, is there some way to tell Godot to bypass the setter or getter, from within the class? The *self* keyword no longer works like it did in Godot 3, so this has been on my mind for a while. The only way I know is by using a second "hidden" property, that you can access directly, but I'm not very fond of that.

    • @rafasoaresms
      @rafasoaresms 8 месяцев назад

      I’m using 4.2 and I have no issues with that, so it’s probably fixed now.
      Regarding the bypass, as an experienced programmer, I’d say that if you find yourself needing to bypass a getter or setter, then they’re the wrong tool for the job. You probably need a separate method with the extra logic, like `set_my_var(value)`, while having the getter and setter being the default ones.

    • @majorgnu
      @majorgnu 8 месяцев назад +1

      The stack overflow issue sounds like a loop of setter and/or setter invocations.
      For examle, you could have variables A and B, both with getters and where the getter for A reads B and the getter for B reads A. The getters would just keep calling each other until they reach the stack size limit.

    • @majorgnu
      @majorgnu 8 месяцев назад

      The stack overflow issue sounds like a loop of setter and/or setter invocations.
      For examle, you could have variables A and B, both with getters and where the getter for A reads B and the getter for B reads A. The getters would just keep calling each other until they reach the stack size limit.

    • @majorgnu
      @majorgnu 8 месяцев назад

      The stack overflow issue sounds like a loop of setter and/or setter invocations.
      For examle, you could have variables A and B, both with getters and where the getter for A reads B and the getter for B reads A. The getters would just keep calling each other until they reach the stack size limit.

    • @majorgnu
      @majorgnu 8 месяцев назад

      The stack overflow issue sounds like a loop of setter and/or setter invocations.
      For examle, you could have variables A and B, both with getters and where the getter for A reads B and the getter for B reads A. The getters would just keep calling each other until they reach the stack size limit.

  • @ddxyzz
    @ddxyzz 8 месяцев назад +2

    Why would i do this

    • @KarasawaL30
      @KarasawaL30 8 месяцев назад +7

      When you have a property that requires several other parts of your app to perform operations, you use a setter.
      This way, instead of writing that same code each time you would change the property, you write that code once in the setter, making your code more readable, easier to debug, and saving time.

  • @JakobKobberholm
    @JakobKobberholm 8 месяцев назад +11

    Great video.
    I use setters and getters extensively for tool scripts, to get the results live in the editor, but it is important to note that the variables set in the editor are set before the node is in ready state, so if you just blindly try to apply values to children of the node, you will get a ton of errors.
    You need to check if the child node exists, then check if it is inside the tree and only THEN apply the new values.
    Putting all this boilerplate code inside every inline variable definitions can make the variable section of a script huge, defeating the purpose of inline setters and getters, so this is where using setter and getter functions are handy.
    Mostly I use inline setters, but put the child node update scripts inside functions, that I call from the inline setters.
    It only adds a single line of code to the setter, so the clutter is minimal and it's easy to comprehend when every setter ends with a function call like update_label(), update_material() etc.

  • @seyproductions
    @seyproductions 8 месяцев назад +3

    Getter and Setter is a classic concept in C# so I am sure the See-Sharpers out there would try using this in Godot.

    • @CreativeSteve69
      @CreativeSteve69 8 месяцев назад

      I just leanred about getter and setters in my pygame and gdscript.

  • @nintySW
    @nintySW 8 месяцев назад +1

    I literally just struggled with these when using them for the first time 2 hours ago, and then i see this video 😭

  • @mickeyjenst4693
    @mickeyjenst4693 4 месяца назад

    Watched it 10 times, understood nothing. I still have no idea what a setter getter is, nor what your code is supposed to do. The clusterfuck of health/max_health is so frustrating without a real explanation.
    Why is your health decreasing? You don't tell us.
    When are the func called? You don't tell us.
    Why do you make get the difference if you add it to the new_health?! You don't tell us.

  • @Terminarch
    @Terminarch 7 месяцев назад

    What scenario would you want to have "set_health" directly which then references new_health against current_health?? Surely it would be better to use a "delta_health" function with requested change argument (ie -10 is 10 damage) so that the health function itself is fully in charge of what new_health should be... as well as calling external things conditionally such as death functions. That's why clamping and animations are in there, after all.
    This also adds functionality for things like armor reducing damage taken that you just don't need to manage externally in the enemy's code from player's properties.

  • @azure8247
    @azure8247 2 месяца назад

    Is it not the same to just call any function and use it's return value?

  • @DieDona
    @DieDona 8 месяцев назад +1

    The export really reminds me of unity, I think it's nice to have

  • @RaveYoda
    @RaveYoda 8 месяцев назад +1

    I would love to see videos on Godot's 3D IK system and how to head track a target.

    • @Gdquest
      @Gdquest  8 месяцев назад +1

      Noted. Thanks for the suggestion! :)

    • @RaveYoda
      @RaveYoda 8 месяцев назад

      @@Gdquest Haha, I'm crossing my fingers. You'll be my hero if y'all can get me to understand what I done goofed as what I've implemented through various other tutorials gives me a panic at the disco contortionist nightmare fuel spaz-out palooza party when testing what seems to me a very simple setup. XD

  • @Yipper64
    @Yipper64 8 месяцев назад +1

    does this apply to C# or is this a GD script exclusive thing?

    • @Gdquest
      @Gdquest  8 месяцев назад

      Sure, but the syntax is different.

    • @Yipper64
      @Yipper64 8 месяцев назад

      @@Gdquest Oh neat ill have to look into it, I recently came across a situation where I need setters and getters, I know having public variables for everything isnt the best practice...

  • @danielakomski3940
    @danielakomski3940 8 месяцев назад +1

    love your thumbnail designs. their esthetic is pleasing my senses

    • @Gdquest
      @Gdquest  8 месяцев назад +4

      Thank you for this. I always wish I had more feedback on thumbnails. :)

  • @UltimatePerfection
    @UltimatePerfection 7 месяцев назад

    I usually don't bother with getters and setters in any language that has them unless I need to do something special when variable is read/set.

  • @ThunderPlayStudios
    @ThunderPlayStudios 8 месяцев назад

    it's just keep getting better and setting things good for me

  • @DeadPlant_
    @DeadPlant_ 8 месяцев назад

    Can you add bay a new version of visual script

  • @MattBlend
    @MattBlend 7 месяцев назад

    Using your health change as an example what are the benefits of getter/setter vs signals?

    • @Gdquest
      @Gdquest  7 месяцев назад

      They're not interchangeable. Setters and getters control values going in and coming out of a single entity. Signals communicate a change to another entity.
      I understand your question though because there *are* some cases where you connect a signal from an entity to itself but that's a workaround when the function is not exposed to you.

    • @MattBlend
      @MattBlend 7 месяцев назад +1

      @@Gdquest Ok, I think I was confused because the other video explained making things like Health as components in order to be able to add them to multiple scenes. They seemed to use signals as opposed to setter/getter.

  • @Algardraug
    @Algardraug 8 месяцев назад

    Hey, in an earlier video you said in the comments that the courses were going to get updated to Godot 4 and it also said so in the description. So I bought Node Essentials because of that reason and waited to start it, but now it seems there are separate courses for Godot 4? Did I throw away my money?

    • @Gdquest
      @Gdquest  8 месяцев назад

      Hi! Try logging in to your dashboard on school.gdquest.com. If you bought Node Essentials for Godot 3 before November 30 (which is when we opened up preorders on Godot 4 courses), you will as promised get the Godot 4 updates for free when they launch. That should already be visible on your dashboard.

  • @viniciusantonio2253
    @viniciusantonio2253 8 месяцев назад

    wait, so setters can be interpreted as signals?

    • @viniciusantonio2253
      @viniciusantonio2253 8 месяцев назад

      i've been using the process function whenever I didn't want a signal

    • @Sylfa
      @Sylfa 8 месяцев назад +4

      That's one way of thinking about it. Though with a setter you can reject a change, such as with the current health example in the video, if the health is lower than 0 or higher than max_health it gets corrected to the right range.
      You could ofc also emit a signal from a setter, such as health_changed. Say you want to spawn in extra enemies when a boss reaches half health, simply add the spawner locations into the scene and bind the signals to them, then they can react when the boss' health changes.
      That way you can easily add reinforcements to any encounter in your game by just dropping in the spawners. No need to code each individual encounter, just @export the enemy to observe and the health value that will trigger them to spawn their enemies. And ofc you can @export the PackedScene that is the reinforcements. Making it all adjustable in the editor.

    • @Gdquest
      @Gdquest  8 месяцев назад +1

      @Sylfa Thanks for being around and supporting others.

  • @Kameolontti
    @Kameolontti 8 месяцев назад

    Holy shit that moment when you started cluttering the code by putting everything on same line.
    Human brains and eyes are not good at quickly processing an unknown amount of elements on the same line and in same color.
    There is a lot of research on using paragraph style formatting and multiple lines and colors to assist the brain to more rapidly make sense of what is going on and one of the first things your brain needs to know is "how many elemenets am I to find". Otherwise it seems that the brain has to spend a lot of time trying to evaluate different elements and objects from what the eyes are seeing and the eyes have to keep moving a lot laterally, going repeatedly over same content back and forth while brain is trying to utilize edge finding algorithms.
    Please, do not use inefficient formatting styles. No matter how many shitflies are attracted to shit or how long traditions they have of eating shit, it is still shit and it destroys a part of your efficiency every day every time you look at code while also burdening your brain by the sheer idiocy of saying "ew" when you see a better formatting system.