[Request] New entry point for registering event handlers
Posted: Sun Oct 13, 2019 1:09 pm
				
				the following way of doing something every ... amount of time seems performance intensive and unhandy if many mods use it:
in another mod:
	
the above code is optimized more already than
so what I am suggestion is a new entry point for registering event handlers that is optimized: script.on_every_n_ticks( ticks between calls , optional: tick offset , function)
tick offset is a hard to implement feature:
There 3 ways to solve the problem this would cause with the whole idea underneath because yeah think:
1 Register script.on_nth_tick event in the code underneath example:
script.on_every_n_ticks(300, 20, ...) will be registered as script.on_every_n_ticks(300, script.on_nth_tick(game.tick + 20, ...)) WOW THAT A GREAT WORKAROUND
2 Better fusing algorithm, which does take tick offset into account
3 Don't implement it of course and let people do 1 on their own if they want an offset
than in the control.lua initialisation it should add it to the on_tick event all together and optimized, for example:
	
normally with script.on_event(defines.events.on_tick, ... ) becomes
	
should become the following in the games core:
	
You might wonder where every_n_ticks(600) should go and if elseif causes problem: well idk. To cover all special occasions I will have to study it for a while, but there are definitially solutions to these problems.
And the long chains of if/elseif statements should probably be optimized by some sort of switch statement.
I hope my idea is understood clearly
[boskid] Added code tags
			Code: Select all
	script.on_event(defines.events.on_tick, function()
	if game.tick % 300 == 0 then
		game.print("HI")
	endCode: Select all
	script.on_event(defines.events.on_tick, function()
	if game.tick % 60 == 0 then -- every second
		-- do stuff
		if game.tick % 300 == 0 then -- every 5 seconds
			-- do stuff
		end
	end
	end)the above code is optimized more already than
Code: Select all
	script.on_event(defines.events.on_tick, function()
	if game.tick % 60 == 0 then -- every second
		-- do stuff
	end
	if game.tick % 300 == 0 then -- every 5 seconds
		-- do stuff
	end
	end)tick offset is a hard to implement feature:
Code: Select all
	script.on_every_n_ticks(300, 20, ... ) -> script.on_event(defines.events.on_tick, if game.tick % 300 - 20 = 0, ...)
								or -> script.on_event(defines.events.on_tick, if game.tick % 300 = 20, ...)1 Register script.on_nth_tick event in the code underneath example:
script.on_every_n_ticks(300, 20, ...) will be registered as script.on_every_n_ticks(300, script.on_nth_tick(game.tick + 20, ...)) WOW THAT A GREAT WORKAROUND
2 Better fusing algorithm, which does take tick offset into account
3 Don't implement it of course and let people do 1 on their own if they want an offset
than in the control.lua initialisation it should add it to the on_tick event all together and optimized, for example:
Code: Select all
	script.on_every_n_ticks(300,
	-- do AAA
	)
	
	script.on_every_n_ticks(400,
	-- do BBB
	)
	script.on_every_n_ticks(200,
	-- do CCC
	)
	script.on_every_n_ticks(33,
	-- do DDD
	)normally with script.on_event(defines.events.on_tick, ... ) becomes
Code: Select all
	script.on_event(defines.events.on_tick, function()
	if game.tick % 300 == 0 then
		-- do AAA
	end
	if game.tick % 400 == 0 then
		-- do BBB
	end
	... and so on ...
	end)should become the following in the games core:
Code: Select all
	script.on_event(defines.events.on_tick, function()
	if game.tick % 33 == 0 then
		-- do ddd
	elseif game.tick % 100 == 0 then -- greatest common divisor (gdc)(except 1 that would be useless, then you want to split it into multiple "groups", example comes later)
		if game.tick % 200 == 0 then -- the loader should notice that 200 and 400 have a gcd of 200
			-- do CCC
			if game.tick % 400 == 0 then
				-- do BBB
			end
		elseif game.tick % 300 == 0 then
			-- do AAA
		end
	end
	end)You might wonder where every_n_ticks(600) should go and if elseif causes problem: well idk. To cover all special occasions I will have to study it for a while, but there are definitially solutions to these problems.
And the long chains of if/elseif statements should probably be optimized by some sort of switch statement.
I hope my idea is understood clearly
[boskid] Added code tags