Page 1 of 1

If Statement not functioning as hoped

Posted: Sat Dec 13, 2014 9:45 pm
by PXLForce
I'm trying to create an if statement that will display text depending on the value of "playerState". As seen below:

Code: Select all

function love.load()	
	playerState = ghost
	normalText = "Player State is Normal"
	ghostText = "Player State is Ghost"
end

function love.draw()
	if playerState == normal then
		love.graphics.print(normalText, 10, 10)
	end
	if playerState == ghost then
		love.graphics.print(ghostText, 10, 10)
	end	
end
However, upon running this it displays both pieces of text at once, no matter the value of playerState. I'm sure this is such a simple error, but I'm very new to Lua and LOVE.

If somebody could point me in the direction of where I am going wrong and how to correct it, I will be very thankful.

Re: If Statement not functioning as hoped

Posted: Sat Dec 13, 2014 10:19 pm
by Ulydev
Use strings.

Code: Select all

function love.load()   
   playerState = "ghost"
   normalText = "Player State is Normal"
   ghostText = "Player State is Ghost"
end

function love.draw()
   if playerState == "normal" then
      love.graphics.print(normalText, 10, 10)
   end
   if playerState == "ghost" then
      love.graphics.print(ghostText, 10, 10)
   end   
end

Re: If Statement not functioning as hoped

Posted: Sat Dec 13, 2014 10:21 pm
by bartbes
As to why, you were using variables 'ghost' and 'normal', neither of which were ever set, so they were both nil, so both conditions matched.

Re: If Statement not functioning as hoped

Posted: Sat Dec 13, 2014 10:43 pm
by PXLForce
D'oh! Thanks to you both for your speedy replies and help!