[Solved] Callback functions executed from a folder?

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
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

[Solved] Callback functions executed from a folder?

Post by Eglaios »

I was looking at Simple Tiled Implementation for making tilemaps, but there's something I don't understand in the code...

So this is how I call STI to draw my map (map.lua) :

Code: Select all

local sti = require "sti"

function love.load()
 map = sti("map.lua")
end

function love.update(dt)
 map:update(dt)
end

function love.draw()
 map:draw()
end
Works perfefctly, but I'm not sure about this one :
So at the beginning, "sti", is actually the directory containing the components to make it work.
The map is loaded, then... it runs callback functions from the sti folder?

I'm not very familiar to that kind of things, but... How does this even work?
When calling map:update(), what does it actually call?
(I seem to be pretty novice in there, as, until now, I thought I could only require specific files, but not whole folders)


Thank you for giving your time to answer this if you ever do!
Last edited by Eglaios on Sun Sep 05, 2021 3:29 am, edited 1 time in total.
Currently working on game music...
MrFariator
Party member
Posts: 509
Joined: Wed Oct 05, 2016 11:53 am

Re: Callback functions executed from a folder?

Post by MrFariator »

Guess I'll break the whole thing down, just to cover everything...

When you run the require() call, it looks for either a file called "sti", or a folder by that name. If it's a folder, the require() call then checks if it contains an init.lua file, runs it, and returns a value, usually a table containing the library API. This kind of folder that contains a init.lua is common among bigger or older lua libraries, that serve a bigger purpose that can not neatly be contained within a single lua file. So, in an essence the require() call loads and initializes STI before anything else is done within your main.lua.

Some smaller libraries instead ask you to require a single file, so it's understandable why you might be unfamiliar with the way STI works.

After that first line, you have defined the usual LÖVE callbacks. These are run in a specific order, as specified by love.run, the main loop function. If you don't override or love.run, what tends to happen is:

1. LÖVE looks up conf.lua, runs it, and returns (conf.lua is optional, but recommended)
2. LÖVE runs main.lua
3. LÖVE runs the contents of love.load, if it has been defined
4. LÖVE starts running the main loop returned by love.run, processing things in the order of
--> Poll events (keyboard strokes, controller button presses, etc), and run the relevant handler functions (eq. love.keypressed)
--> Run the contents of love.update, passing in the delta time between frames (or 0 if love.timer is disabled)
--> Run the contents of love.draw, if a window and graphics modules have been initialized
--> Sleep the LÖVE process for a moment (so not to take too much CPU time from the host PC), before looping again

So, with STI loaded and the love.load, love.update and love.draw callbacks defined, you pass a file path to STI in love.load. If you look at the STI library's init.lua's contents, you'll find something like "STI.__call", which calls a local function called "new". You might notice that the require() returns a table, but STI uses lua's metatable mechanisms to say that "use this __call function when this table is called like a function" (you can read more about metatables here and here).

Anyway, those functions basically take a filepath (and optional arguments) and constructs an object. This object will represent the map you're loading, containing some functions such as update() and draw() - that will help you do the respective tasks. After the object has been constructed, it's returned, and you simply assign it to a variable like you do in love.load. And then every frame you call the respective functions in love.update and love.draw contained within the object.

As such, if you later wanted to load a new map, you'd basically reassign another map into the 'map' variable by calling the STI library with another filename, and that's that.
Eglaios
Prole
Posts: 36
Joined: Mon May 03, 2021 8:45 pm

Re: Callback functions executed from a folder?

Post by Eglaios »

That was such a great answer!! Thank you very much, I now understand this completely!
Currently working on game music...
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 52 guests