Variable seen as nil for some reason.

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
User avatar
Hedgo
Prole
Posts: 6
Joined: Fri May 13, 2022 7:54 pm
Location: The Bottom of the Barrel
Contact:

Variable seen as nil for some reason.

Post by Hedgo »

I'm having an issue where a variable which is definitely defined is still showing up as nil.
No goddamn clue why it's like that.

error:
sound.lua:16: attempt to compare number with nil

Traceback

[love "callbacks.lua"]:228: in function 'handler'
sound.lua:16: in function 'update'
[love "callbacks.lua"]:162: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'


sound.lua:

Code: Select all

function love.load()
    coolSound = 0
    soundCool = 0
    local playingSound = 0
    local jamAudio = 0
    local jamLoopable = 0
end

function love.update()
    if coolSound == 0 then
        soundCool = 0
    end
    
    if soundCool > 150 then
        coolSound = 0
    end
end

--"sound" is a string that's defined when the programmer runs jamSound. 
--It's the path to the sound they want to play.

--I.E. jamSound("sounds/fuckyou.wav")
function jamSound(sound, cd) --cd stands for cooldown. Toggles if the cooldown stuff is ran.
    if cd == 1 then --the cooldown thing is for times when jamSound() is run multiple times, while the sound is meant to play once.
        coolSound = 1 -- soundCool can't be set to 0 anymore
        soundCool = soundCool + 1 --soundCool goes up every time the code is ran
        if soundCool == 1 then --on the first frame soundCool goes up,
            jamAudio = love.audio.newSource(sound, "static") --decides what audio to play
            love.audio.play(jamAudio) --actually plays the audio
        end
    elseif (cd == 0) or (cd == nil) then
        jamAudio = love.audio.newSource(sound, "static") --decides what audio to play
        love.audio.play(jamAudio) --actually plays the audio
    end
end
all main.lua code relating to sound.lua (some lines were removed):

Code: Select all

function love.load()
	defineVar()
	require("sound") --adds jamSound(sound) and jamMusic(music, loop?)
	sonicSprite = love.graphics.newImage("sonic.png")
	print("Loaded Sonic sprite ", sonicSprite:getWidth(), "x", sonicSprite:getHeight(), "px")
	jamMusic("music/test.WAV")
end

function love.update(dt)
	if love.keyboard.isDown("left") then --moving left?
		if grndSpeed > 0 then --if moving right still,
			grndSpeed = grndSpeed - decelSpeed --decelerate
			jamSound("sounds/skid.WAV", 1)
			if grndSpeed <= 0 then
				grndSpeed = -0.5 --deceleration quirk
			end
		elseif grndSpeed > -topSpeed then --if actually moving left,
			grndSpeed = grndSpeed - accelSpeed --accelerate
			if grndSpeed <= -topSpeed then
				grndSpeed = -topSpeed --stay at the top speed
			end
		end
	end

	if love.keyboard.isDown("right") then --moving right?
		if grndSpeed < 0 then --still moving left?
			grndSpeed = grndSpeed + decelSpeed --decelerate
			jamSound("sounds/skid.WAV", 1)
			if grndSpeed >= 0 then
				grndSpeed = 0.5 --deceleration quirk
			end
		elseif grndSpeed < topSpeed then --if finally moving right,
			grndSpeed = grndSpeed + accelSpeed --accelerate
			if grndSpeed >= topSpeed then
				grndSpeed = topSpeed --stay at top speed
			end
		end
	end
end
Drawing like it's the wild west.

Discord: currentlyjammin
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: Variable seen as nil for some reason.

Post by pgimeno »

You can't have multiple love.load and love.update in different files (unless you know what you're doing and how to handle the switching between them, which is obviously not the case here, and even if you know what you're doing it's pretty discouraged in general, with pretty few exceptions).

You haven't provided enough context either. Line 16 of sound.lua is an 'end' statement. The program simply can't crash there, therefore your sound.lua must differ in some way from the one you have posted.

Anyway, it's more or less clear what's happening here. First main.lua executes, which defines love.load and love.update. Then love.load executes, which loads sound.lua. Then sound.lua executes and redefines (overwrites) both love.load and love.update, but love.load won't be called by the system again, because it was already called and it's only called once. As a result:
  1. coolSound and soundCool won't be defined, because the love.load that is redefined in sound.lua isn't ever called.
  2. The love.update in main.lua will be completely lost and never execute, because it was overwritten by the one in sound.lua.
  3. When love.update in sound.lua executes, soundCool will not be defined (because the love.load in sound.lua wasn't executed) and therefore the program will crash in the comparison "soundCool > 150".
User avatar
Hedgo
Prole
Posts: 6
Joined: Fri May 13, 2022 7:54 pm
Location: The Bottom of the Barrel
Contact:

Re: Variable seen as nil for some reason.

Post by Hedgo »

pgimeno wrote: Sun Oct 22, 2023 7:52 am You haven't provided enough context either. Line 16 of sound.lua is an 'end' statement. The program simply can't crash there, therefore your sound.lua must differ in some way from the one you have posted.
I removed a few empty lines when posting this, sorry. Line 16 in the original is the "soundCool > 150" comparison as you've figured out.
Thanks for the response.
Drawing like it's the wild west.

Discord: currentlyjammin
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests