Lambda functions

Share any helpful tutorials or guidance related to the Godot Engine or game development.
Post Reply
User avatar
DaveTheCoder
Posts: 38
Joined: Tue Jul 18, 2023 1:52 am

Lambda functions are cool. (New feature in Godot 4)

Connecting a signal:

Code: Select all

$VisibilityNotifier2D.screen_exited.connect(func(): queue_free())
Connecting a signal with a parameter:

Code: Select all

Events.volume_changed.connect(func(v: float): volume_db = v)
Deferring a signal emission:

Code: Select all

(func(): value_changed.emit(value)).call_deferred()
https://docs.godotengine.org/en/4.1/tut ... -functions
User avatar
grey
Posts: 26
Joined: Tue Jul 18, 2023 10:01 am
Contact:

maybe i should get in the habit of writing one-liners as lambdas

what does deferring a signal emission do ? does it emit the signal at the very end of the frame or something like that ?

edit : i think that's it (source : https://docs.godotengine.org/en/4.1/cla ... umerations)
@lifeisspoon on twitter
User avatar
DaveTheCoder
Posts: 38
Joined: Tue Jul 18, 2023 1:52 am

what does deferring a signal emission do ? does it emit the signal at the very end of the frame or something like that?
Yes. I'm doing that when emitting a signal in a node's _ready() method, since the nodes that receive the signal may not be ready at that time.
Owen Lloyd
Posts: 30
Joined: Wed Jul 19, 2023 2:37 am
Location: Brisbane Australia

How would you define a lambda function without using the words/term "lambda function".

I struggle to grasp the concept and the value, This is a reflection of my limited brain capacity, not of the concept, lol. But I don't get it, it seems to me to be taking python readability and making it C++ readability and I cannot work out the advantage.
User avatar
DaveTheCoder
Posts: 38
Joined: Tue Jul 18, 2023 1:52 am

I think the first couple of paragraphs of this article answer your question. The "lambda" comes from the historical origin. "Anonymous function" might be a better term.
In computer programming, an anonymous function (function literal, lambda abstraction, lambda function, lambda expression or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function.[1] If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

Anonymous functions originate in the work of Alonzo Church in his invention of the lambda calculus, in which all functions are anonymous, in 1936, before electronic computers.[2] In several programming languages, anonymous functions are introduced using the keyword lambda, and anonymous functions are often referred to as lambdas or lambda abstractions. Anonymous functions have been a feature of programming languages since Lisp in 1958, and a growing number of modern programming languages support anonymous functions.
https://en.wikipedia.org/wiki/Lambda_fu ... ogramming)
Owen Lloyd
Posts: 30
Joined: Wed Jul 19, 2023 2:37 am
Location: Brisbane Australia

Hi thanks. :)

I will pretend I understand until I do.
User avatar
DaveTheCoder
Posts: 38
Joined: Tue Jul 18, 2023 1:52 am

I think it's hard to understand new programming constructs by merely reading about them. You have to use them in your own code and experiment.
Owen Lloyd
Posts: 30
Joined: Wed Jul 19, 2023 2:37 am
Location: Brisbane Australia

Okay, I think I get it.

I am still not sure why they are advantageous (outside of typing less). Speedier execution maybe? Less memory used? Easier for project maintenance?

Not a biggie, thanks for you feedback. :)
User avatar
DaveTheCoder
Posts: 38
Joined: Tue Jul 18, 2023 1:52 am

In my opinion, it can make the code more compact and easier to read.

But there's a risk of making it harder to read. Less code is not always better.

It also gives you bragging rights:
I'm superior to you because I know how to use this feature and you don't. :P
Owen Lloyd
Posts: 30
Joined: Wed Jul 19, 2023 2:37 am
Location: Brisbane Australia

lol :lol:

Code: Select all

var call_out = func(): print('It must be true')
Thanks for the explanations.
Post Reply