How to call update() or draw() from two different lua files?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How to call update() or draw() from two different lua files?
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?
Re: How to call update() or draw() from two different lua files?
In love2d, the top level file is main.lua. You can use require() to include objects defined in other scripts. For example, you could have main depend upon two scripts, say a.lua and b.lua. Each of these return a table with one key set, "update" whose value is the function defined in the script.
You could call those defined functions in the love callback love.update() main.lua:
https://love2d.org/wiki/love.update
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
https://love2d.org/wiki/love.update
Re: How to call update() or draw() from two different lua files?
Note if you require() the same file twice:
script_b and script_c will be pointing to the same table:
i.e. it does not load the file, parse and initialize again. So if you want new objects, return a factory method:
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
Re: How to call update() or draw() from two different lua files?
You can check this working library for state management:
https://hump.readthedocs.io/en/latest/gamestate.html
https://hump.readthedocs.io/en/latest/gamestate.html
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: How to call update() or draw() from two different lua files?
Code: Select all
function love.update (dt)
for i, state in ipairs (ActiveStates) do
if state.update then state.update(dt) end
end
end
Who is online
Users browsing this forum: Google [Bot] and 4 guests