Running simulation asynchronously to _process(delta)

Godot Version

v4.3.stable.mono.official [77dcf97d8]

Question

Hello

i have a c# simulation function called WorldEngine that changes some state vectors. it contains a loop that runs much faster than godot’s deltatime. i also have a gd script called DrawWorld that can draw WorldEngine’s states.

How can i make the simulation to run as fast as possible and just draw whichever state is available when the _process(delta) gets called? (meaning that states between real time t and t+delta will not be displayed)

You want to synchronize the threads. Physics engine does this exact behavior. I would maybe deploy a “double buffer” strat, so if your async stuff is working and process wants data. You want to avoid waiting of the more important process thread, and just grab the last completed computation.

So I would have a full state variable and lock it when _process reads or your worker writes to it. The hope is that writing and reading data to a locked resource will take minimum time. If it’s so large that you don’t want to make a copy for the process thread. Ping pong between two instances, the “double buffer”.

thanks for the reply

as another approach, is it necessary to use the process(delta)? maybe i can call queue_redraw()/draw() by myself in convenient intervals?

Certainly! I would personally probably try to make a cadence with it. I would rather not have the simulation eat the CPU going fast as possible. Work with a delta, if relevant, and sleep X amount of time before the next run. The amount of time to sleep, and stay synchronized will probably vary. So you should keep track how long it took to do a simulation step. (This really does sounds like how Godot’s physics engine works)

So you need to make a decision on the delta being fixed or variable…

Although I don’t know your specific running goals and the real complexity of the data being passed around. So I am trying to stay general as possible.

my simulation involves a lot of math and interactions between objects (nodes). But the user just needs the display of the state in real-time intervals and some interactions by the use of some buttons/pause etc.

Actually, my goal eventually is to build a kind of slider to control the simulation speed, but i wouldn’t mind an “as fast as possible” setting…

edit: in my simulation, in my PC, the state updates on average every ~0.077 msec. So it is over 200 times between typical process(delta) calls at 16.67 msec