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.
Function/While loop run in background
-
- Burner Inserter
- Posts: 5
- Joined: Tue Jul 05, 2016 5:07 pm
- Contact:
- Ranakastrasz
- Smart Inserter
- Posts: 2171
- Joined: Thu Jun 12, 2014 3:05 am
- Contact:
Re: Function/While loop run in background
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.
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
Re: Function/While loop run in background
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
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.
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.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
-
- Burner Inserter
- Posts: 5
- Joined: Tue Jul 05, 2016 5:07 pm
- Contact:
Re: Function/While loop run in background
Will look into it. Thanks.
Re: Function/While loop run in background
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
So in the meantime spreading out over the ticks manually as has been discussed is the best way