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:
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
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.
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.
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.
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.