Search code examples
c++unreal-engine4

UE4 using a Function as parameter with it's own parameters, C++


I am trying to set a timer, and use a function with in. I read the timer documentation and figured out how it works. You setting a timer with a timer_handler and a function to execute. Here's the problem. I can only give function without parameters.

GetWorldTimerManager().SetTimer(timer_handle1, this, &actor_class:Foo, 2.f, false, 1.f);

This "Foo" function can't take any arguments. But it should be. Basically, how can i translate it the form of it taking parameters? I read about the Function Pointers and DELEGATE . Are these useful for this situation? If they are, is there any example or tutorial you can suggest?

Thanks in advance.


Solution

  • Use the overload of SetTimer that takes in a lambda. Then call your function with parameters inside the lambda.

    GetWorld()->GetTimerManager().SetTimer(
                    timer_handle1,
                    [&]() { this->Foo(123); },
                    2.f,
                    false,
                    1.f);