Creating an event/function
Creating an event/function
Hello there!
This question might sound stupid but how do I create event/function which only happens every 60 ticks?
Thanks!
This question might sound stupid but how do I create event/function which only happens every 60 ticks?
Thanks!
Re: Creating an event/function
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.
Re: Creating an event/function
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)
local function onesecond(event)
if event.tick % 60 = 0 then
--do stuff here
end
end
script.on_event(defines.events.on_tick, onesecond)
Re: Creating an event/function
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.
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 116 times
Re: Creating an event/function
You made a simple mistake: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.
Code: Select all
script.on_event(defines.events.on_tick, function(event)
if game.tick % 60 ~= 0 then
playerloop()
end
end)
Code: Select all
script.on_event(defines.events.on_tick, function(event)
if game.tick % 60 ~= 0 then return end
playerloop()
end)
Code: Select all
script.on_event(defines.events.on_tick, function(event)
if game.tick % 60 == 0 then
playerloop()
end
end)
Re: Creating an event/function
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)
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.
Re: Creating an event/function
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.
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.
- bobingabout
- Smart Inserter
- Posts: 7352
- Joined: Fri May 09, 2014 1:01 pm
- Contact:
Re: Creating an event/function
Change your loop structure so it doesn't drop out after performing the first buildtoggle.
Re: Creating an event/function
And how would you do that?bobingabout wrote:Change your loop structure so it doesn't drop out after performing the first buildtoggle.
- bobingabout
- Smart Inserter
- Posts: 7352
- Joined: Fri May 09, 2014 1:01 pm
- Contact:
Re: Creating an event/function
I don't know, I can't see your code.
- aubergine18
- Smart Inserter
- Posts: 1264
- Joined: Fri Jul 22, 2016 8:51 pm
- Contact:
Re: Creating an event/function
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.