How to use modules?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
User avatar
PixelHero
Prole
Posts: 45
Joined: Sat Apr 16, 2022 3:16 am

How to use modules?

Post by PixelHero »

Hi, I'm having a problem. I tried to load a module, but it came out with this error:
Error

main.lua:5: attempt to index global 'a' (a boolean value)


Traceback

[love "callbacks.lua"]:228: in function 'handler'
main.lua:5: in function 'load'
[love "callbacks.lua"]:136: in function <[love "callbacks.lua"]:135>
[C]: in function 'xpcall'
[C]: in function 'xpcall'

Here's my code:

Code: Select all

--main
a = require("Audio")

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

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

Code: Select all

--Audio
function load()
    source = love.audio.newSource( "Backgroundsong.wav", "stream" )
end

function update(dt)
	if not source:isPlaying( ) then
	    love.audio.play( source )
    end
end
Does anyone see the problem? If you do, PLEASE tell me. Also, 'Backgroundsong' is the actual name of my .wav file. Any questions or answers?
Dragons are great.
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: How to use modules?

Post by togFox »

Code: Select all

a = {}

--Audio
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
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: How to use modules?

Post by togFox »

edit: I'm on mobile so someone point out the main.lua needs a.load and not a: load
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
PixelHero
Prole
Posts: 45
Joined: Sat Apr 16, 2022 3:16 am

Re: How to use modules?

Post by PixelHero »

Tried what you said, but now I'm getting this:
Error

main.lua:5: attempt to call field 'Load' (a nil value)


Traceback

[love "callbacks.lua"]:228: in function 'handler'
main.lua:5: in function 'load'
[love "callbacks.lua"]:136: in function <[love "callbacks.lua"]:135>
[C]: in function 'xpcall'
[C]: in function 'xpcall'
Here's my new code:

Code: Select all

--Audio
a = {}
function load()
    source = love.audio.newSource( "Backgroundsong.wav", "stream" )
end

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

return a

Code: Select all

--main
require("Audio")

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

function love.update(dt)
    a.update(dt)
end
I don't know what's happening. HELP!
Dragons are great.
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: How to use modules?

Post by pgimeno »

Pay attention to what togFox wrote, as it's not the same that you have written in Audio.lua.
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: How to use modules?

Post by Gunroar:Cannon() »

ais a table, you index a.

The thing you indexed should be in a. Hence

Code: Select all

--Audio
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
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
PixelHero
Prole
Posts: 45
Joined: Sat Apr 16, 2022 3:16 am

Re: How to use modules?

Post by PixelHero »

pgimeno wrote: Thu May 12, 2022 12:28 am Pay attention to what togFox wrote, as it's not the same that you have written in Audio.lua.
OOPS!!! My bad. Thanks for pointing that out. My code will be modified.
Dragons are great.
User avatar
PixelHero
Prole
Posts: 45
Joined: Sat Apr 16, 2022 3:16 am

Re: How to use modules?

Post by PixelHero »

Oh, uh, Gunroar, sorry for not quoting you with pgimeno. Your post came just as I submitted.
Dragons are great.
User avatar
PixelHero
Prole
Posts: 45
Joined: Sat Apr 16, 2022 3:16 am

Re: How to use modules?

Post by PixelHero »

IT WORKS!!!
Dragons are great.
User avatar
PixelHero
Prole
Posts: 45
Joined: Sat Apr 16, 2022 3:16 am

Re: How to use modules?

Post by PixelHero »

Here's the .love file:
Attachments
Audio.love
.love file for my audio/module testing.
(1.62 MiB) Downloaded 91 times
Dragons are great.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 60 guests