If Statement not functioning as hoped

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
PXLForce
Prole
Posts: 2
Joined: Sat Dec 13, 2014 9:31 pm

If Statement not functioning as hoped

Post 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.
User avatar
Ulydev
Party member
Posts: 445
Joined: Mon Nov 10, 2014 10:46 pm
Location: Paris
Contact:

Re: If Statement not functioning as hoped

Post 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
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: If Statement not functioning as hoped

Post 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.
PXLForce
Prole
Posts: 2
Joined: Sat Dec 13, 2014 9:31 pm

Re: If Statement not functioning as hoped

Post by PXLForce »

D'oh! Thanks to you both for your speedy replies and help!
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Semrush [Bot] and 3 guests