Page 1 of 1

On tick call method in another file

Posted: Sat Nov 07, 2015 8:46 am
by mazetar
Let's say I want to have a entity with a long onUpdate function to be called every x ticks.
Could I have that method in a seperate file (ex: EntityMobFile.lua) such that all code related to this entity is eaisly found there and not inside a miles long control.lua?

I assume there must be a simple way to get some organization and sepperation when creating a mod with several entities :)

psudo code to illustrate my thinking:

int y = 0
game.onevent(defines.events.ontick, function(event)
{

y++
if (x %y )
{
EntityMobFile.onUpdate(someData)
}

}

Re: On tick call method in another file

Posted: Sat Nov 07, 2015 9:58 am
by gheift
control.lua

Code: Select all

defines = require "defines"
local mod = require "mod"

script.on_event(defines.events.on_tick, function(event)
    if event.tick % 60 == 0 then -- every second
        return
    end

    mod:update(event)
end)
mod.lua

Code: Select all

local mod = {}

mod.update = function(self, event)
   -- do something here   
end

return mod