Hello Lambdas! Anonymous Functions in GDScript - Godot 4

Поділитися
Вставка
  • Опубліковано 6 гру 2023
  • 🎟 "EARLYBIRD" coupon - Preorder Godot 4 courses up to 50% below launch price (+ Release Timeline): school.gdquest.com/godot-4-ea...
    🗨 GDQUEST DISCORD COMMUNITY: / discord
    🎓 Learn GDScript From Zero: www.gdquest.com
    ---
    📝 CC-BY LICENSE
    This video is licensed under the CC-By 4.0 license: creativecommons.org/licenses/...
    You can attribute it to "CC-By 4.0 - GDQuest and contributors - www.gdquest.com/"

КОМЕНТАРІ • 57

  • @anhedonie92
    @anhedonie92 6 місяців тому +28

    As a JS dev I love this.

    • @mundomusicaearte
      @mundomusicaearte 6 місяців тому +6

      Just realized that I've been using lambda functions without knowing

    • @kishirisu1268
      @kishirisu1268 4 місяці тому

      As JS dev I am sorry for such horrible lambda and calbacks in Godot. It looks like ancient technology if compare to modern JS.

  • @CreativeSteve69
    @CreativeSteve69 6 місяців тому +15

    I've noticed this as i'm going through a godot course on udemy. I started with godot 3.1.7 now onto 4.0 because a thing on my game wasn't working, which made it work correctly. I'm glad it is adding Lambda onto godot. I am loving it so far. It is a nice welcome into godot. I just got back into godot to the half a year now after a 3 year break. it's been a fun journey.

  • @koresaliva
    @koresaliva 6 місяців тому +10

    Have to say this is an extremely helpful feature and im very glad it was added, saved me tons of headaches and keeping my code clean

  • @Unimportant
    @Unimportant 6 місяців тому +8

    I'm really excited for the Godot 4 tutorials coming up next year.

  • @6xten
    @6xten 6 місяців тому +5

    HELLO GORDON!

  • @user-po4kb2lw5t
    @user-po4kb2lw5t 6 місяців тому +52

    Lambda? I cannot help to think of Black Mesa.

  • @covenautadev7260
    @covenautadev7260 5 місяців тому

    This video changes my life! is the feature I didn't know that I needed but solve a lot of my problems on my recently new programing paradigm focused more on functions as behaviors itself than encapsulating them on objects.

  • @dionelcarreno1832
    @dionelcarreno1832 6 місяців тому +2

    Finally i can understand what lambdas are, they seem pretty useful. I may used them in my code some day.

  • @mySDK3333
    @mySDK3333 6 місяців тому +1

    Thanks for explaining these abstract concepts since these are very hard to understand with mere words!

  • @glassmarble996
    @glassmarble996 6 місяців тому +2

    Hey i can clearly understand this than previous video :)

  • @corey_johnson
    @corey_johnson 6 місяців тому +1

    This is a super useful feature; excited to see it in GDScript! People who are complaining about it should realize they don't have to use them if they don't want to 😅

  • @Amazing_Software
    @Amazing_Software 6 місяців тому

    Nice, thanks

  • @v44n7
    @v44n7 6 місяців тому +2

    i would need to watch this video 10 times but still amazing feature

  • @gorudonu
    @gorudonu 6 місяців тому

    Syntax could be improved but I'm glad that feature is there!

  • @thekthe12345
    @thekthe12345 6 місяців тому

    Nice!

  • @luchinazo
    @luchinazo 3 місяці тому

    amazing

  • @Wilker_uwu
    @Wilker_uwu 6 місяців тому

    what are the dynamics of using those functions with `.bind()` and similar? in a looping function for example, how do i connect a signal to a lanbda that takes the iteration number?

  • @progdevelop
    @progdevelop 6 місяців тому

    Nice. When will interfaces be available in Godot?

    • @Gdquest
      @Gdquest  6 місяців тому

      In GDScript they are planned in the form of traits, and I believe that implementation work has started on this already, but as with everything in a free software project, it will come out when it's ready: github.com/godotengine/godot-proposals/issues/6416

  • @user-pu5vq4vs9u
    @user-pu5vq4vs9u 4 місяці тому

    Can this be used only when connecting signals or as a comparison function for sorting?
    Something like this: sort(list, func(a, b): return a.name < b.name)

    • @Gdquest
      @Gdquest  4 місяці тому

      It can pretty much be used anywhere you can pass a function. So yes, you can use it for filtering, sorting, mapping... (The sorting function in Godot is sort_custom).

  • @oddlytimbotwillison6296
    @oddlytimbotwillison6296 6 місяців тому +4

    Very javascript!

    • @PaulSpades
      @PaulSpades 6 місяців тому

      very scheme.

    • @corey_johnson
      @corey_johnson 6 місяців тому +3

      Lots of languages support anonymous functions; Rust, Kotlin, C++, C#.. They're super useful :)

  • @subzekrom129
    @subzekrom129 6 місяців тому

    WHOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO LETS GOOOOOOOOOOOOOOO

  • @KettLovahr
    @KettLovahr 6 місяців тому

    lambdas are such a godsend

  • @FeniksGaming
    @FeniksGaming 6 місяців тому +2

    I appreciate the added functionality but to me it looks more convoluted and prone to mistakes than the old way of doing it. Dtill more options for people the better

    • @PaulSpades
      @PaulSpades 6 місяців тому +1

      oh, yes... making a function call to the interpreter evaluator with the function name as a string and arguments as array is simpler than a callback?

    • @FeniksGaming
      @FeniksGaming 6 місяців тому +3

      @PaulSpades I have no issue with callbacks, happy they are here I have issue with anonymous functions because without reading whole function I don't know what it does spaw_boss() is good name I know what soul happen immediately func() is rubbish because I need to read the rest of a lines to know what happens in this function.
      Callbacks are NOT lambda I am pleased that Godot is moving away from strings everywhere. But I am yet to see good practical example of use of lambda function that doesn't make it harder for me to read.
      Notice that I am using word ME in my sentences. They work for you then fantastic use them. The old way works for me great I will use that. The way I code my games doesn't in anyway affect you

    • @Gdquest
      @Gdquest  6 місяців тому +6

      Dunno if you saw but you can optionally name them. I do often name them these in my code (typically when it's more than 1-2 lines of code). That way it's more like the functions as we knew them in Godot 3, just with a local scope.

    • @FeniksGaming
      @FeniksGaming 6 місяців тому

      @Gdquest that makes more sense missed the part where you can also give them a name

    • @PaulSpades
      @PaulSpades 6 місяців тому +1

      @@FeniksGaming Well, implementing first order functions into the language was a prerequisite for callbacks, adding support for lambdas is just a cherry on top. I get what you're saying, you can have and use one without the other, and lambdas sort of are an acquired taste.
      Personally, the less things I get to name, the less bad names I have in the codebase, and I'm good at following the flow of callbacks. But everybody's different, you prefer to explicitly declare the function with a name. And that's fine, I pull out bigger lambdas into separate functions, sometimes. Cheers!

  • @God_of_Art
    @God_of_Art 6 місяців тому +1

    ok im switching

  • @ZoidbergForPresident
    @ZoidbergForPresident 6 місяців тому +1

    How do I register to the new school site with my mavenseed data? It doesn't accept my current credentials. :(

    • @Gdquest
      @Gdquest  6 місяців тому +2

      We will transfer all accounts and courses from Mavenseed (webiste where you have an account) to GDSchool (our new platform) by the end of the year. You don't have to do anything for that.
      If you'd like to purchase a Godot 4 course separately, and you have an account on our old platform (Mavenseed), please use the same email to register on school.gdquest.com. We will link your accounts and port your old courses to the new platform at the end of the year.

    • @ZoidbergForPresident
      @ZoidbergForPresident 6 місяців тому

      @@Gdquest Oh so I didn't need to create an account on the new site, but anyway, I used the same email so I should be fine. Merci!

  • @vladimir-sama
    @vladimir-sama 6 місяців тому

    How do you disconnect?

    • @Gdquest
      @Gdquest  6 місяців тому +3

      If you need to disconnect at a specific time while your node/object is still alive, with lambda functions, you'd store the function in a variable.
      Minimal example:
      var my_func = func():
      pass
      pressed.connect(my_func)
      pressed.disconnect(my_func)
      Of course, if you need to disconnect the signal elsewhere in your script, and you need to access the function in different scopes, then you'll need to define a method/regular script function.
      And more often than not we don´t need to manually disconnect signals as they automatically get cleaned up when deleting the node/object, so lambda functions are especially useful in that common case.

  • @Lambdapod
    @Lambdapod 6 місяців тому

    It's me

  • @flourfree2K
    @flourfree2K 6 місяців тому

    But you can't reuse lambda functions. They are nameless, counter-intuitive, and .... what for?!

    • @Gdquest
      @Gdquest  6 місяців тому +5

      You can name and reuse them. You can use them as locally scoped functions, so, to avoid spreading code too much, as shown in the video. When you make complex applications, code is much easier to read if the signal connection and callbacks are all in one place.

    • @ezg5221
      @ezg5221 6 місяців тому +4

      They're "first-class functions"! You can store lambdas in arrays, use them as input to a function and return them from functions. There's a whole world of "functional programming" which uses this, including modern web development, and most popular languages have added these in the last ~10 years if they didn't already have them (they were invented on *paper* in the 1950's :D)

    • @charlesabju907
      @charlesabju907 4 місяці тому +1

      Ngl I understand what you were thinking when you wrote this comment. This was my opinion about lambdas the first time I heard about them. But I can assure you lambdas are the bomb. (at least in other languages because I'm a uber noob in Godot and haven't used them in it yet)

    • @charlesabju907
      @charlesabju907 4 місяці тому

      What's actually counterintuitive is the fact that GSCript doesn't support closures yet. It's when you return a function from a function AND the returned function "remembers" the context it had inside the fuction that returned it. It sounds very abstract, and it is, but it's supported in almost all languages that support lambdas.

  • @justSomeOthaGuy
    @justSomeOthaGuy 4 місяці тому

    can you store one in a dictionary?

  • @greendsnow
    @greendsnow 6 місяців тому

    lambdas are.... not clean code... :/