Looking for something different from tasks?

Forgive me if I have asked something like this before:

Task is a function meant to run per frame (or by a given time period).
But what if I want to run a function as many times as possible per frame wthout slowing down the framerate?

For that to work you would have to cap framerate.
Then you would need to get current frame time, and while current frame time is lower than maximum frame time minus some margin call that function. I had some thoughts on changing game world (economy,generating quests, etc…) in background in similar way.

So you would create task and put this while loop in it.
That task would need to be LAST task executed.

What do you mean cap framerate? use video-sync?
So have a while loop in a task and set it’s sort to maximum? Thats a neat idea actually. I wonder if there’s some downside to this, I cant think of any.

I personally need something like this for an “interpreter” for the custom kiddy language of the game used by some stuff not meant to be written by programmers.

Yeah, video-sync would be great for that. There were some topics on getting monitor refresh rate recently i think, from that you can calculate max frame time.

I dont remember if you can get time spent in current way via panda, but if you cant, just make other task and make it run first and set time on some object, and in end “while” task, just get time and calculate difference.

Also, if you really need that function to be called as many times as possible, you could write that part in c++ or cython?

I don’t think that’s required, because it’s meant to run short simple event scripts. But one statement per frame is too slow. In other words, no need to fix it if it isn’t broken. :slight_smile:

I don’t know if I got this question right, but how about running your function as many times as possible, measuring every call’s time and pushing the next frame when the required time exceedes the function call time?

This is only applicable if the function call always needs about the same time.
In pseudocode it would be something like this:

accumulator = globalClock.getDt()  # time the last frame needed
delta_time = 0
while accumulator > delta_time:
    time_a = currentTime()
    time_b = currentTime()
    delta_time = time_a -time_b
    accumulator -= delta_time

This way you’d stretch frames after lags, but this technique should appoximate the number of function calls to stay right under the frame time, independent of the monitor’s refresh rate.

Another topic that might interest you are asynchronous tasks. E.g. you can load models over multiple frames.