Lua Help - Loop with waits, run per nth tick

This is the place to request new mods or give ideas about what could be done.
adventureclass
Manual Inserter
Manual Inserter
Posts: 1
Joined: Sun May 03, 2020 7:36 pm
Contact:

Lua Help - Loop with waits, run per nth tick

Post by adventureclass »

Hello,
I am new to working with the factorio api and am trying to determine a way i can have code applied for X seconds to a target play determined by a chat integration.

the idea is to allow chat in a live stream be able to spawn things in over a duration or cause random attacks over an amount of time, the interface is using rcon and sends the command as 1 string.

I was looking at trying to use the game.tick value and while loops to get this behaviour although the busy wait tends to crash the game. I have been unable to figure out how to use the script events to run on tick in this situation allow me to code spawning in groups of tree's around the player every second for 10 seconds then quit. it appears the script on_nth_tick would work although i am unsure how to stop it after the specified time has elapsed.

Please help me out and i appreciate any idea's on how to implement a time based loop that terminates after X seconds
Honktown
Smart Inserter
Smart Inserter
Posts: 1058
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: Lua Help - Loop with waits, run per nth tick

Post by Honktown »

Run per nth tick is implemented with roughly the same behavior as:

Code: Select all

for nth_tick, nth_function in pairs(registered_by_this_mod) do
	if (game.tick % nth_tick) == 0 then
		nth_function()
	end
end
There is no ability to apply an offset to the nth_tick, so the start of a function's "cycle" can be a little bit off (doesn't usually matter).

To make biters spawn every 30 seconds, you want to have the nth_tick be 30 * 60 ticks per second = 1800, and the function be what you're using to make them. To stop the event from running, you use the same nth_tick, but use nil as the "function", so (1800, nil). If you want to stop every nth_tick you've already registered, pass nil as the nth_tick (or nil, nil, not sure) as arguments

Edit:
To have it stop itself, you can use a global.my_function_counter = number_of_times * nth_ticks. When you register the function, use global.yaddayadda = 0, and everytime the function runs, increment it. If it's >= number_of times, have the then-end register nil to that nth_tick.

One big complication is each mod can only register one function to each nth_tick. Some mods handle nth_ticks seperately ( a new function is defined when needed that runs through a tick-function table itself) or more easily, have an all-event handler than runs every tick, and for each nth_tick table, check if that table should be ran. A few helper functions like MyEventHandler.add("nth_tick", n, function) and a remover can make it easier to handle it all. Recommend taking a look at more complicated mods if run into timing collisions (you can do every 599, 600. 601 ticks for almost perfectly 10 seconds, but we can do better).
Last edited by Honktown on Wed May 06, 2020 9:08 pm, edited 1 time in total.
I have mods! I guess!
Link
User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3749
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Lua Help - Loop with waits, run per nth tick

Post by DaveMcW »

I recommend you run on_nth_tick all the time and don't try to stop it. Un-registering events is dangerous and can lead to desyncs if you do it wrong.

Inside on_nth_tick, use an if statement to run the code when needed.
Post Reply

Return to “Ideas and Requests For Mods”