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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
enginerd
Prole
Posts: 1
Joined: Wed Jun 14, 2023 8:30 pm

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

Post 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?
yal2du
Prole
Posts: 42
Joined: Wed Oct 13, 2021 5:41 pm

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

Post 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
yal2du
Prole
Posts: 42
Joined: Wed Oct 13, 2021 5:41 pm

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

Post 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
User avatar
dusoft
Party member
Posts: 524
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post by dusoft »

You can check this working library for state management:
https://hump.readthedocs.io/en/latest/gamestate.html
User avatar
darkfrei
Party member
Posts: 1186
Joined: Sat Feb 08, 2020 11:09 pm

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

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Amazon [Bot], Bing [Bot], Google [Bot], Majestic-12 [Bot] and 5 guests