Looping for X seconds - Easiest Approach?

Hi,

I am interested to see if there is an easy approach to running a function for a designated amount of seconds. I have tried this a few ways, my first approach was to create a variable that contained the current game + the to run the function (this would be the end ) and then use a while loop with the condition of while(current game < end ) however this always ended in an infinite loop, I am not sure why? It was as if the condition was not being reassessed on each iteration of the loop.

After searching Answer Hub I changed this to use a sequence, essentially using a modified while loop. My end result works perfectly, the function runs for the designated however it seems to be a very long winded approach to something relatively simple (I also really dislike node connectors crossing, so the sequence node connecting back to the branch node upsets me and it appears there is no way to rotate a reroute node)

Is their an easy way to do this? Initially I was thinking about timers however they won’t work for this. It may well be this is the only way but I would rather ask than assume!

EDIT: I also tried to create this as a function (to keep things tidy) however I could not use a delay node within a function which I found strange.

As I understand it, Timers call a Function/Event after so many seconds. The purpose of the function is simply to simulate a flashlight flickering for a predefined number of seconds so that it can be re-used. So say I have a level event and I need a flashlight to flicker for 2 seconds - This function does that. The delay is something I may add as an input so that I can change the flicker rate but essentially I just wanted to call a function that would loop for so many seconds and then end, I am unsure of why I couldn’t use a while loop to achieve this though.

I would use delta , that way it is the same for everyone.

Regards

Why weren’t you able to do it using timers?

Just to confirm that I understand what you want to achieve: You want to call a function/event during a specific of X seconds and each call should have a delay between them or should it be called every frame?

As @ said the best approach is using the DeltaTime and the Tick event for it.

Add a bool flag that controls whether to call you function in the tick and use the DeltaTime to animate your flashlight. After you did a simulation step check if the has exceeded to end set when you set the flag to true, if it does just set the flag to false. Make sure to use Game Seconds instead of Real Seconds, this way it will pause when paused. You can also use a timer to set the bool flag to false if you like. Adding a delay between each simulation step would be as simple as using a counter that subtracts the DeltaTime and once it reaches 0 or lower perform the step and set the timer again to it initial value to count to the next step.

A way of doing it using timers would be doing a fixed step simulation, the timer would then do the simulation step using a fixed delta (the of the timer itself) and launch a new timer event if the has not exceed the .

I would go the tick router with a timer that controls the flag and the delay system, this way it is quite flexible.

Because in a while loop you will block the whole game thread, my aproach will perform one step on each frame. If you do a while loop using the engine will never exit because the is computer at the beginning of each tick, so if you never exit that tick the engine wont increase your value and thus you will never exit the loop.

The problem of your solution is that your use a fixed step, which is not based. The animation will be different depending on the FPS you currently have, while using the DeltaTime each tick will give you a -based animation.

This is a basic idea on how you perform an animation, you advance each frame in your logical animation.

Sorry ,

Would you be able to explain how this will help? Do you mean instead of using get real seconds?

Thanks a lot Moss,

I will try this over the weekend and let you know - my first impressions are that it will be just as lengthy as my above solution which appears to work okay, I could be wrong though.

Out of interest do you know why this won’t work as a while loop? I thought the condition being if current is less than end would be perfectly acceptable?

EDIT: This condition works as part of a branch, so do while loops not re-evaluate each “loop”?

Thankyou,

Your answer is incredibly helpful.

Much appreciated!