Function/While loop run in background

Place to get help with not working mods / modding interface.
conquestingturtle
Burner Inserter
Burner Inserter
Posts: 5
Joined: Tue Jul 05, 2016 5:07 pm
Contact:

Function/While loop run in background

Post 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.
User avatar
Ranakastrasz
Smart Inserter
Smart Inserter
Posts: 2171
Joined: Thu Jun 12, 2014 3:05 am
Contact:

Re: Function/While loop run in background

Post 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.
My Mods:
Modular Armor Revamp - V16
Large Chests - V16
Agent Orange - V16
Flare - V16
Easy Refineries - V16
d3x0r
Filter Inserter
Filter Inserter
Posts: 316
Joined: Sun Jun 04, 2017 8:56 am
Contact:

Re: Function/While loop run in background

Post by d3x0r »

can you maybe break up the execution time and just do one pass of the loop each tick?
User avatar
prg
Filter Inserter
Filter Inserter
Posts: 947
Joined: Mon Jan 19, 2015 12:39 am
Contact:

Re: Function/While loop run in background

Post 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.
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
conquestingturtle
Burner Inserter
Burner Inserter
Posts: 5
Joined: Tue Jul 05, 2016 5:07 pm
Contact:

Re: Function/While loop run in background

Post by conquestingturtle »

Will look into it. Thanks.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Function/While loop run in background

Post 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 :)
Post Reply

Return to “Modding help”