On tick call method in another file

Place to get help with not working mods / modding interface.
Post Reply
mazetar
Burner Inserter
Burner Inserter
Posts: 17
Joined: Fri Jan 02, 2015 7:45 am
Contact:

On tick call method in another file

Post 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)
}

}

gheift
Fast Inserter
Fast Inserter
Posts: 188
Joined: Tue Mar 03, 2015 9:20 pm
Contact:

Re: On tick call method in another file

Post 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

Post Reply

Return to “Modding help”