[Request] New entry point for registering event handlers

Things that already exist in the current mod API
Post Reply
DragonD
Burner Inserter
Burner Inserter
Posts: 8
Joined: Sun Oct 13, 2019 10:10 am
Contact:

[Request] New entry point for registering event handlers

Post by DragonD »

the following way of doing something every ... amount of time seems performance intensive and unhandy if many mods use it:

Code: Select all

	script.on_event(defines.events.on_tick, function()
	if game.tick % 300 == 0 then
		game.print("HI")
	end
in another mod:

Code: 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)
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:

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, ...)
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:

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 :mrgreen:

[boskid] Added code tags

Bilka
Factorio Staff
Factorio Staff
Posts: 3139
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: [Request] New entry point for registering event handlers

Post by Bilka »

I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

DragonD
Burner Inserter
Burner Inserter
Posts: 8
Joined: Sun Oct 13, 2019 10:10 am
Contact:

Re: [Request] New entry point for registering event handlers

Post by DragonD »

I meant that it should be repeated every ... ticks.
And doing a for loop to add the event a few thousands of times seems a bit messy to me.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2250
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: [Request] New entry point for registering event handlers

Post by boskid »

Code: Select all

/c script.on_nth_tick(2, function(e) game.print(e.tick) end)
76724-on_nth_tick.png
76724-on_nth_tick.png (4.12 KiB) Viewed 1618 times
What is missing?

DragonD
Burner Inserter
Burner Inserter
Posts: 8
Joined: Sun Oct 13, 2019 10:10 am
Contact:

Re: [Request] New entry point for registering event handlers

Post by DragonD »

Whoops, I misunderstood xD ...
Ty

Post Reply

Return to “Already exists”