Hello, can’t seem to reset a timer.
Would you please check code below, and tell me if this approach simply won’t work >(
Thank you!
Can I make an FTimerHandle this way?
Supposing the problem isn’t in saving the handle, can I reset timer by handle?
void AChar::Affected (const EState Key) {
FTimerHandle Timer; // make a handle ?
// read Stat, Value, Duration etc from table
// supposing the problem isn't here -----------
if (Effects.Add(Key, Timer)) { /* apply effect */ }
// Effects.Add checks do we already have it, by Key
// if yes, return false and modify InTimer by ref&
// if no, return true and save InKey and InTimer internally
// ---------- should this work ? or is my princess in another castle...
if (Duration > 0.f) {
auto Call = FTimerDelegate::CreateLambda( /* remove effect */ );
GetWorldTimerManager().SetTimer(Timer, Call, Duration, false);
// PROBLEM: doesn't reset the timer, always sets a new one? :(
}
}
I want to set the timer, save its handle, and if same effect is added, restart the timer by handle.
Instead, I’m getting added only once (so we know that check works), but the effect is removed multiple times.
Aka, each use adds a new timer until effect removal, instead of resetting the old one.
Thanks a bunch!
SOLUTION
SetTimer with the same handle indeed does reset the timer as expected.
The problem was that just by declaring an FTimerHandle Timer; you generate an invalid handle (basically an uint64 0).
The handle is only filled once it was used by SetTimer, therefore you need to save it after the timer was set.
// bad
FTimerHandle Timer;
MyTimers* = Timer;
SetTimer(Timer, ...);
// good
FTimerHandle Timer;
SetTimer(Timer, ...);
MyTimers* = Timer;
Cheers