Archive 17/01/2023.

Delayed execution in C++

smellymumbler

Unity allows your to delay the execution of a function by using something like this: Invoke(“DoSomething”, 2);

DoSomething will be called after 2 seconds. I can call Invoke inside Update(), Awake() or any other callback.

ppsychrite

You could launch another thread that waits two seconds, executes the function and stops. There’s many ways to do that.

smellymumbler

What’s the “Urho” way?

ppsychrite

I haven’t looked into it that much but it looks like Urho3D has it’s own way of doing it instead of using c++11 threads.

smellymumbler

Yeah, but it seems to be just a task queue. WorkItem does not contain any delay info, or the WorkQueue. You can just add new WorkItem’s to the WorkQueue, no scheduling.

ppsychrite

There appears to also be a Sleep function in the Timer class that you could call. https://urho3d.github.io/documentation/1.5/class_urho3_d_1_1_time.html

If all else fails you could just use c++11’s chrono and thread headers for multithreading and do this_thread sleep_for :wink:

Pencheff

How about creating a scheduler that you add tasks to. A task holds the invoke time and a delegate to a method or a function. Subscribe to E_UPDATE for example and on every tick check if a task is to be executed.