Suppose I want to perform slow movement for some time. In simple simulation I would just write something like this:
float x = 0; //position, our out value
float v = 0; //velocity, varies in time
float a = 1; //acceleration, const in time
for(float t=0;t<5;t+=0.1){
v += a*dt;
x += v*dt;
Tick(dt); //let the system advance for time dt
} //etc
Sadly, in UE4 it’s not that simple, we need to think in one Tick of time, if we want to expand some function for more than one tick we either need to run a function in other thread (i know there is some tutorial about this, but I didn’t check that yet), or make complicated function which is called many times and continues it’s actions from before.
Or is it? I hope that we have actually something prepared for this situation and I don’t have to write myself some workaround code - maybe it already exists. Any suggestions?
There’s probably a way built in to do this already but it’s easy enough to write yourself a central “RandomUpdates” place in your game mode Tick (or wherever makes sense).
I’d be thinking - TArray of objects with TFunction m_Tick; members. Call any registered objects each frame and remove those that return false.
Or even (disclaimer - compiles but I haven’t used it):
TArray < TFunction<bool(float dt)> > MyRandomUpdates;
void MyTick(float dt)
{
for (int i = 0; i < MyRandomUpdates.Num(); i++)
{
if (!MyRandomUpdates[i](dt))
{
MyRandomUpdates.RemoveAtSwap(i);
--i; //Counteracts the i++
}
}
}
Watch our for the copies going on with the TFunctions - depending on what you capture in your lambda it could get pretty hefty.
Good idea! I’ll leave for now this question as unanswered, because maybe someone will post something as good, but I’ll try your solution for sure. Thanks for advance!
I expanded a little your answer to work with internal class methods. In the language of UE4 C++ :
typedef void(AOptics::*MyFunT)(float dt);
TArray<MyFunT> CurrentTickCalls;
MyFunT Fun = &AOptics::TestFun;
(*this.*Fun)(0.1);
CurrentTickCalls.Add(Fun);
(this->*CurrentTickCalls[0])(0.1);
For some method void AOptics::TestFun(float dt)
. I’m not sure how to involve the TFunction type here, and probably I won’t use this solution since I rather need some functions run dependent of multiple flag values instead of simple call.