love.draw() seems to ignore if statements

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
yabobay
Prole
Posts: 3
Joined: Tue May 05, 2020 1:11 pm

love.draw() seems to ignore if statements

Post by yabobay »

In love.load(), I have (along with some other stuff, check main.lua):

Code: Select all

mode = line
And later on, in love.draw(), I have:

Code: Select all

function love.draw()
   if mode == point then
      -- draw all four corners of the first square
      love.graphics.points( square1[1] )
      love.graphics.points( square1[2] )
      love.graphics.points( square1[3] )
      love.graphics.points( square1[4] )
   end
end
Which runs just fine when mode is set to point. The problem is that it also runs when it's set to ANYTHING else. :x Did I mess up the if statement? Am I stupid? Is Lua stupid? Is LÖVE stupid?
Attachments
main.lua
(738 Bytes) Downloaded 128 times
MrFariator
Party member
Posts: 559
Joined: Wed Oct 05, 2016 11:53 am

Re: love.draw() seems to ignore if statements

Post by MrFariator »

Because you've never defined what the variables "line" and "point" are, this is effectively what's happening:

Code: Select all

function love.load()
  mode = nil
  -- ...
end

function love.draw()
  if mode == nil then
    -- because mode IS nil, this will be run every time love.draw is called
  end
end
Lua treats undeclared variables as globals with the value of nil (signifying the absence of a value), so you need to assign said "line" and "point" variables an actual value prior assigning them to "mode". As such, I believe maybe you intended was something like this?

Code: Select all

function love.load()
  line = "line" -- note the quotes; this makes it so we're assigning a string of value "line" to the variable
  	        -- we could assign anything else, like a number, so long the variables line and point do not share the same value
  point = "point"
  mode = line 
  -- ...
end

function love.draw()
  if mode == point then
    -- because mode's value is "line" and not "point", this code is not run
  end
end
You can read more about lua's types and values from lua pil.
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: love.draw() seems to ignore if statements

Post by zorg »

Otherwise, you can just put single or double quotes around point and line in love.draw, if you, you know, don't want to treat those as variables, but literal strings; this goes for love.load as well, since you're setting mode to the undefined variable line:

Code: Select all

function love.load()
  mode = 'line'
end

function love.draw()
  if mode == 'point' then
    -- runs if mode is the string 'point'
  elseif mode == "line" then
    -- runs if mode is the string "line"
  end
end
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
yabobay
Prole
Posts: 3
Joined: Tue May 05, 2020 1:11 pm

Re: love.draw() seems to ignore if statements

Post by yabobay »

I ended up putting the quotations at

Code: Select all

mode = "point" -- which is only for the start, then there's a toggle
and there at the if statement. Problem solved. Thanks!

P.S.: Y'know, the worst part of that is that I always keep making that mistake.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests