How to use modules?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to use modules?

Post by pgimeno »

Yes it works, but.

If you keep doing things global, that will end up biting you in the butt sooner than later.

Whatever the module (in this case Audio.lua) returns, is what the function require() returns in turn. That will help you make things local and looking nice.

This works without adding 'a' to the global scope:

Audio.lua:

Code: Select all

local a = {}  -- now a is a local table

--Audio
-- add functions to the local table a
function a.load()
    source = love.audio.newSource( "Backgroundsong.wav", "stream" )
end

function a.update(dt)
	if not source:isPlaying( ) then
	    love.audio.play( source )
    end
end

return a -- return the value of the local, that is, the table
main.lua:

Code: Select all

--main
local a = require("Audio") -- the returned value is what the Audio module returned, and we're setting the local variable 'a' to that

function love.load()
    a.load()
end

function love.update(dt)
    a.update(dt)
end
You can also require the same file from other locations. The nice thing about require() is that it doesn't load the file multiple times; instead it caches the return value and returns it immediately if the file was loaded previously. Therefore, using require() only has a performance penalty the first time it's executed; other modules don't need to worry about that.
User avatar
PixelHero
Prole
Posts: 45
Joined: Sat Apr 16, 2022 3:16 am

Re: How to use modules?

Post by PixelHero »

Thanks for the AMAZING insight everybody. Does anyone want to hear "Backgroundsong.wav? I made it myself. The .love file it's in is on the first page of this topic.
Dragons are great.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: How to use modules?

Post by milon »

I just downloaded your .love file from page 1, and it crashes upon execution. The way you created your module is faulty.

In love (and lua) required files have their own separate scope. Functions defined in one file won't be accessible from another file unless they're global - and it's good practice to avoid globals where possible. What you want to do instead is put everything into a table and return that table at the end of the function. See my attached version as one possible way to do it.


About the song, it reminds me of some old school RPGs from the 80s, but I can't think of a particular one at the moment. It's a decent short loop, but it would get hard on the ears of a player after a while I think. Personally, I'm going to use sounds & songs from Eric Matyas if/when I need audio.
Attachments
Audio.love
(1.62 MiB) Downloaded 66 times
Last edited by milon on Tue May 17, 2022 4:40 pm, edited 1 time in total.
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
PixelHero
Prole
Posts: 45
Joined: Sat Apr 16, 2022 3:16 am

Re: How to use modules?

Post by PixelHero »

milon wrote: Mon May 16, 2022 6:01 pm I just downloaded your .love file from page 1, and it crashes upon execution. The way you created your module is faulty.

In love (and lua) required files have their own separate scope. Functions defined in one file won't be accessible from another file unless they're global - and it's good practice to avoid globals where possible. What you want to do instead is put everything into a table and return that table at the end of the function. See my attached version as one possible way to to it.


About the song, it reminds me of some old school RPGs from the 80s, but I can't think of a particular one at the moment. It's a decent short loop, but it would get hard on the ears of a player after a while I think. Personally, I'm going to use sounds & songs from Eric Matyas if/when I need audio.
Thanks for the tip, I got it to work for my game, I did not update that .love, though. As for the song, I created it on Anvil Studios. I am making more to play as background music.
Dragons are great.
Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests