Page 1 of 1

How to call update() or draw() from two different lua files?

Posted: Wed Jun 14, 2023 8:32 pm
by enginerd
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?

Posted: Thu Jun 15, 2023 3:31 am
by yal2du
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.

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
You could call those defined functions in the love callback love.update() main.lua:

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?

Posted: Thu Jun 15, 2023 3:46 am
by yal2du
Note if you require() the same file twice:

Code: Select all

local script_a = require("a")
local script_b = require("b")
local script_c = require("b")
script_b and script_c will be pointing to the same table:

Code: Select all

assert(script_b == script_c,"Not the same!")
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

-- 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?

Posted: Thu Jun 15, 2023 10:52 am
by dusoft
You can check this working library for state management:
https://hump.readthedocs.io/en/latest/gamestate.html

Re: How to call update() or draw() from two different lua files?

Posted: Thu Jun 15, 2023 1:34 pm
by darkfrei

Code: Select all

function love.update (dt)
  for i, state in ipairs (ActiveStates) do
    if state.update then state.update(dt) end
  end
end