Page 1 of 1

Function/While loop run in background

Posted: Sun Jul 23, 2017 6:54 pm
by conquestingturtle
I have a function which contains a rather large loop. This loop, during its execution, (as would be expected) hangs the game for about 2 seconds (but will grow before I'm finished). I'm looking for a way to push the loop (or function) into the background/onto another thread so that the game may continue running while the loop evaluates.

Thanks.

Re: Function/While loop run in background

Posted: Sun Jul 23, 2017 7:09 pm
by Ranakastrasz
I THINK ks power has a thing where it only runs a few windmills and burner generators per tick in a cycle but I couldnt understand it that well.

Re: Function/While loop run in background

Posted: Sun Jul 23, 2017 7:44 pm
by d3x0r
can you maybe break up the execution time and just do one pass of the loop each tick?

Re: Function/While loop run in background

Posted: Sun Jul 23, 2017 7:45 pm
by prg
There is no way to create a background thread with the modding API.

You'd need to only process a few iterations of the loop per tick, then keep the state around in global for further processing the next tick. I'm doing something like this in the Automatic Belt Planner mod.

Re: Function/While loop run in background

Posted: Sun Jul 23, 2017 8:20 pm
by conquestingturtle
Will look into it. Thanks.

Re: Function/While loop run in background

Posted: Sun Jul 23, 2017 9:47 pm
by Nexela
Unfortunately coroutine (which would be perfect for this) has been been disabled for Factorio. The major reason being the current library used to serialise the game state doesn't support saving/loading running "threads" to the map which will lead to desyncs. There is a much better library available that can handle this but the time/effort to swap over to it is probably not feasible at this point in development.


So in the meantime spreading out over the ticks manually as has been discussed is the best way :)