Page 1 of 1
Creating an event/function
Posted: Tue Nov 08, 2016 1:28 am
by IG2
Hello there!
This question might sound stupid but how do I create event/function which only happens every 60 ticks?
Thanks!
Re: Creating an event/function
Posted: Tue Nov 08, 2016 1:42 am
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)
Re: Creating an event/function
Posted: Tue Nov 08, 2016 1:45 am
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)
Re: Creating an event/function
Posted: Tue Nov 08, 2016 4:31 pm
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.
Re: Creating an event/function
Posted: Tue Nov 08, 2016 4:47 pm
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)
Re: Creating an event/function
Posted: Tue Nov 08, 2016 4:48 pm
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.
Re: Creating an event/function
Posted: Tue Nov 08, 2016 5:04 pm
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.
Re: Creating an event/function
Posted: Wed Nov 09, 2016 8:52 am
by bobingabout
Change your loop structure so it doesn't drop out after performing the first buildtoggle.
Re: Creating an event/function
Posted: Wed Nov 09, 2016 6:19 pm
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?
Re: Creating an event/function
Posted: Thu Nov 10, 2016 2:55 pm
by bobingabout
I don't know, I can't see your code.
Re: Creating an event/function
Posted: Thu Nov 10, 2016 8:04 pm
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