Creating an event/function

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
IG2
Burner Inserter
Burner Inserter
Posts: 19
Joined: Wed Mar 30, 2016 11:53 pm
Contact:

Creating an event/function

Post by IG2 »

Hello there!

This question might sound stupid but how do I create event/function which only happens every 60 ticks?

Thanks!

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Creating an event/function

Post by DaveMcW »

Code: Select all

script.on_event(defines.events.on_tick, function()
  if game.tick % 60 ~= 0 then return end

  -- This part runs every 60 ticks

end)
Last edited by DaveMcW on Tue Nov 08, 2016 1:58 am, edited 1 time in total.

Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: Creating an event/function

Post by Nexela »

Something like this


local function onesecond(event)
if event.tick % 60 = 0 then
--do stuff here
end
end

script.on_event(defines.events.on_tick, onesecond)

IG2
Burner Inserter
Burner Inserter
Posts: 19
Joined: Wed Mar 30, 2016 11:53 pm
Contact:

Re: Creating an event/function

Post by IG2 »

Thanks for your help!

Sadly the changes didnt worged out as i hoped. I tried to increase a Mods performance (up to 23 ms) by reducing the amount of scriptupdates to 1/60 (once a seceond istead of once a tick) but it seems like my Lua understanding is not good enough.
I attached my modified control.lua in case someone has got the time to have a look.
Attachments
control.lua
(7.21 KiB) Downloaded 79 times

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5151
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

Re: Creating an event/function

Post by Klonan »

IG2 wrote:Thanks for your help!

Sadly the changes didnt worged out as i hoped. I tried to increase a Mods performance (up to 23 ms) by reducing the amount of scriptupdates to 1/60 (once a seceond istead of once a tick) but it seems like my Lua understanding is not good enough.
I attached my modified control.lua in case someone has got the time to have a look.
You made a simple mistake:

Code: Select all

script.on_event(defines.events.on_tick, function(event)
  if game.tick % 60 ~= 0 then
    playerloop()
  end
end)
it should be

Code: Select all

script.on_event(defines.events.on_tick, function(event)
  if game.tick % 60 ~= 0 then return end
  playerloop()
end)
or

Code: Select all

script.on_event(defines.events.on_tick, function(event)
  if game.tick % 60 == 0 then
    playerloop()
  end
end)

daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: Creating an event/function

Post by daniel34 »

IG2 wrote:Sadly the changes didnt worged out as i hoped. I tried to increase a Mods performance (up to 23 ms) by reducing the amount of scriptupdates to 1/60 (once a seceond istead of once a tick) but it seems like my Lua understanding is not good enough.
I attached my modified control.lua in case someone has got the time to have a look.

Code: Select all

script.on_event(defines.events.on_tick, function(event)
	if game.tick % 60 ~= 0 then
	playerloop()
	end
end)
You used the wrong comparator. The way it is currently coded it runs the playerloop 59 out of 60 ticks, or in other words it only skips 1 tick every 60 ticks and runs the other 59 times. You need to replace ~= (unequal) with == (equal), then the code only runs once per second.

The reason DaveMcW's code (if game.tick % 60 ~= 0 then return end) works with unequal is because he returns 59 times a second while you continue 59 times a second.
quick links: log file | graphical issues | wiki

IG2
Burner Inserter
Burner Inserter
Posts: 19
Joined: Wed Mar 30, 2016 11:53 pm
Contact:

Re: Creating an event/function

Post by IG2 »

Thank you very much!

It kinda works now (performance is much better now while only scanning).
Sadly I didnt recognise earlier that this change also reduces the buildToggle and demoToggle to once a second. I thought it would build/deconstruct everything it found as fast as before but only check for stuff to do every second. Seems I have to find a work around.

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Creating an event/function

Post by bobingabout »

Change your loop structure so it doesn't drop out after performing the first buildtoggle.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

IG2
Burner Inserter
Burner Inserter
Posts: 19
Joined: Wed Mar 30, 2016 11:53 pm
Contact:

Re: Creating an event/function

Post by IG2 »

bobingabout wrote:Change your loop structure so it doesn't drop out after performing the first buildtoggle.
And how would you do that?

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Creating an event/function

Post by bobingabout »

I don't know, I can't see your code.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Creating an event/function

Post by aubergine18 »

IG2, at some point you'll have to learn basics of Lua scripting. There's loads of free references online that you can use. http://lua-users.org/wiki/ControlStructureTutorial
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

Post Reply

Return to “Modding discussion”