About Store Forum Documentation Contact



Post Reply 
Lambda in EE
Author Message
Houge Offline
Member

Post: #1
Lambda in EE
Hi!

Just wanted to share a micro code snippet, hope you like it!
Most people know Lambda-functions in C++11, i think this way of using them is very cool:

Code:
D.screen_changed =
    [] (Flt old_width, Flt old_height) -> void
    {
        // paste your screen changes here
    };

No need to declare separate functions to perform screen changing! You can paste this code in Init() state smile

EDIT:
Of course it works with button functions also smile
Code:
button->func([](Ptr)
    {
        Gui.msgBox("Test", "Lambda test in button function");
    }       );

EDIT2:
And, and, and... No need to create static void's inside classes! smile

Code:
struct MyStruct
{
    Window wnd;
    Button btn;
    
    //... here we create window and button ...//
    
    btn->func(
        [](Ptr strct) -> void
        {
            ((MyStruct*)strct)->wnd.fadeToggle();
        }, this);            
}
(This post was last modified: 03-27-2014 06:17 PM by Houge.)
03-27-2014 04:54 PM
Visit this user's website Find all posts by this user Quote this message in a reply
rstralberg Offline
Member

Post: #2
RE: Lambda in EE
Yup. Lamda's are really cool smile
03-27-2014 07:10 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #3
RE: Lambda in EE
I'm old. What is happening in this code?! D:
03-27-2014 07:54 PM
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #4
RE: Lambda in EE
Lambdas allow to define custom functions "inline", although I've never used them since I try to keep up compatibility with VS 2008
But thanks for sharing smile
03-27-2014 08:47 PM
Find all posts by this user Quote this message in a reply
para Offline
Member

Post: #5
RE: Lambda in EE
Rubeus, as Esenthel said, lambda functions allow you to define functions or functors 'inline' .. useful when you have to provide a function as an argument, and rather then having to define this function elsewhere you type it in as an argument..

it's define like:
Code:
[capture list] (argument list) -> return_type { code }

Old way
Code:
// have to define this function prior to Init()
void screenchngf ( Flt old_width, Flt old_height ) { // do stuff }
..
// inside init
D.screen_changed = screenchngf;

New way
Code:
// inside init
D.screen_changed = [] ( Flt old_width, Flt old_height ) -> void { // paste your screen changes here };
This defines a lambda function and assigns a function pointer to D.screen_changed, the "-> void" isn't really necessary here as the compiler can deduce the return type.
03-27-2014 10:29 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #6
RE: Lambda in EE
Black magic! Sorcery!

That is pretty cool. I did some reading up on it. Look like a really shortcut for some smaller things(like screen_changed). Looks like it could potentially get out of hand quickly and turn into spaghetti code, though. Also, I use VS2008 on one of my dev machines.

I would also be wary of using it excessively every frame in games- if I'm not mistaken, it will still go through the stack allocation process and all that jazz. If this is the case, some if/switch statements would probably be a more efficient option. Each ms counts!
03-27-2014 11:26 PM
Find all posts by this user Quote this message in a reply
Sherincall Offline
Member

Post: #7
RE: Lambda in EE
Lambdas actually have a good chance of getting inlined, so the performance should the same. Either way, you really shouldn't try to save the stack allocations for something that happens so rarely, even once per frame.
Another cool thing about lambdas is that if you put an & between the brackets, (like this: [&]), you can reference all the variables from the calling function, usually without any overhead. More details here.

They can be pretty handy, but can make your life extra miserable if you didn't properly design your code. Personally, I avoid them as much as possible, but I tend to do that with a lot of C++ features.
(This post was last modified: 03-27-2014 11:50 PM by Sherincall.)
03-27-2014 11:50 PM
Find all posts by this user Quote this message in a reply
Houge Offline
Member

Post: #8
RE: Lambda in EE
(03-27-2014 10:29 PM)para Wrote:  the "-> void" isn't really necessary here as the compiler can deduce the return type.

Yes, sure, return value needs to be set if it's not obvious, my examples just show where it shall be and how it shall look smile

(03-27-2014 11:50 PM)Sherincall Wrote:  They can be pretty handy, but can make your life extra miserable if you didn't properly design your code. Personally, I avoid them as much as possible, but I tend to do that with a lot of C++ features.

Sure, everything shall be used when it really makes sense.

By the way, Greg, autocomplete system in EE 2.0 doesn't allow to write first func(); and then lambda inside it. And it doesn't show lambda arguments. Please can you check this? smile
(This post was last modified: 03-28-2014 05:05 AM by Houge.)
03-28-2014 05:03 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply