How to call update() or draw() from two different lua files?
Posted: Wed Jun 14, 2023 8:32 pm
In unity, if I create two scripts I can define an update function in both, and they both update. Is there something similar in love2d?
Code: Select all
-- script a.lua
local a = {}
a.update = function(self,some_text)
print(tostring(self),some_text)
end
return a
Code: Select all
-- script b.lua
local b = {}
local updater = function(self,some_text)
print(tostring(self),some_text,some_text)
end
b.update = updater
return b
Code: Select all
-- main.lua
local script_a = require("a")
local script_b = require("b")
-- ... other stuff
-- define the standard lov2d update callback
local timer = 0
function love.update(dt)
timer = timer + dt
script_a:update("Foo")
script_b:update("Bar")
end
Code: Select all
local script_a = require("a")
local script_b = require("b")
local script_c = require("b")
Code: Select all
assert(script_b == script_c,"Not the same!")
Code: Select all
-- c.lua defines a factory method
local updater = function(self,some_text)
print(tostring(self),some_text)
end
return function(...)
local an_object = {}
an_object.update = updater
return an_object
end
Code: Select all
-- main.lua
local script_a = require("a")
local script_b = require("b")
local script_c = require("c")
-- instantiate an object defined in script c.lua
local object_c = script_c()
-- instantiate another
local object_d = script_c()
-- ...
local timer = 0
function love.update(dt)
timer = timer + dt
script_a:update("Foo")
script_b:update("Bar")
object_c:update("Wut")
object_d:update("Now")
assert(object_c ~= object_d,"The same!")
end
Code: Select all
function love.update (dt)
for i, state in ipairs (ActiveStates) do
if state.update then state.update(dt) end
end
end