Hi SophiaWolfie,
I’ve never tried to end a timer from a lambda but I can’t think of any reason why not. But shouldn’t you be setting the last parameter to True to loop?
sorry yes. thats a typo, it should be true.
In all my tests i cant make it stop from within. It seems the variable is lost somewhere, and the settimer just keeps going in an infinite loop.
So im having to create a separate function everytime i need to use SetTimer, and call it again recursively.
There is no way i can pass the FTimerHandle to it so far…
You shouldn’t ever lambda-capture local variables by reference. Local variables are de-allocated by the time function is done executing. Then your counter++ statement is modifying something in memory which may or may not be used by another part of code. This is undefined behavior.
FTimerHandle is a struct that can be copied around safely. You can pass this one by value and it should work when using ClearTimer.
The only way such a counter could work, is if you make it global or static. But then it’s gonna be shared accross all function calls.
If you want a counter that is local to each function call, while still working as a counter within the lambda, you need to provide a a bigger global (or static) data structure to hold the counters of the (potential) multiple functions calls. For example you can use a TMap to map timer handles to counters :
Basically it doesnt seem to be possible to stop the timer from the inside of the function.
In your solution you passed the Handle without referencing, that wont compile because of the GetWorld()->GetTimerManager().ClearTimer(Timer)
Hmm the Handle might be captured before being set.
Looks like we need a custom index, this is gonna get messier.
The following should work I tested them.
To illustrate what I mentioned earlier I made both cases.
The simple case is when you don’t need multiple timers running concurrently. In this case you can just use static Handle and static Counter, it behaves just like global variables / single global timer :