events.on_tick not working

Place to get help with not working mods / modding interface.
Verlak
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sun Jan 15, 2017 10:15 am
Contact:

events.on_tick not working

Post by Verlak »

I'm modifying an Oarc scenario to add PvP and diplomacy between teams, however the on_tick event isn't being processed.
This is the code in question:

Code: Select all

script.on_event(defines.events.on_tick, function(event)

	-- Runs every 5 seconds
	if (game.tick % 300 == 0) then
		CheckDiplomacy()
		CheckPlayerColors()
	end

	-- Runs every 30 seconds
	if (game.tick % 1800 == 0) then
	end
end)
I have tested the CheckPlayerColors function. If I call the function somewhere else, it works exactly as intended, but inside the on_tick event nothing is being called.
The code is located inside the control.lua file along with all of the other event code.
Does anyone know what might be preventing the on_tick code from running?

Verlak
Manual Inserter
Manual Inserter
Posts: 2
Joined: Sun Jan 15, 2017 10:15 am
Contact:

Re: events.on_tick not working

Post by Verlak »

It appears that the problem is to do with an RSO function which calls event.on_tick, so it conflicts and breaks my code.
If anyone knows how to get around it, please do tell.

orzelek
Smart Inserter
Smart Inserter
Posts: 3921
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: events.on_tick not working

Post by orzelek »

Verlak wrote:It appears that the problem is to do with an RSO function which calls event.on_tick, so it conflicts and breaks my code.
If anyone knows how to get around it, please do tell.
RSO should have it's own on_tick when running unless you copied it into scenario.
If thats the case then you need to change the on_tick to register only once and register all others into one common handle.

I think that Factorio stdlib has support for something like this so you might look there for help/example code.

As an alternative... since on_tick in RSO is temporary and used to initialize it only you could make the on_tick method to register your method instead of setting the handler to null.

User avatar
cpeosphoros
Inserter
Inserter
Posts: 40
Joined: Fri Dec 23, 2016 10:57 pm
Contact:

Re: events.on_tick not working

Post by cpeosphoros »

Have you tried using event.tick instead of game.tick?

Also, it's slightly faster, and also easier to read and maintain, if you localize your handler, doing something like this:

Code: Select all

local function checkCycle (event)
	if event.tick % (60 * seconds) == 0 then
		work()
	end
end

script.on_event(defines.events.on_tick, function (event) checkCycle(event) end)

Post Reply

Return to “Modding help”