Page 1 of 2

Weird error for a function that is correct.

Posted: Tue Apr 09, 2024 11:57 pm
by SuskyTheCorgi
I am trying to make a platformer engine by detecting if an object is inbetween the corners of a platform. An error I didn't get before which I didn't do anything wrong. It said expected 'end' after 'function love.load()', but as you can see below, I wrote end.

My program:

Code: Select all

function love.load()
    tile = love.graphics.newImage('grassTile.png')
    platformer = {}
        platformer.a = {}
            platformer.a.x = 600
            platformer.a.y = 400
    platformer.scroll = 0
    platformer.y = 200
end

function love.update(dt)
    platformer.a.corner_lb_x = platformer.a.x - (tile:getWidth()/2)
    platformer.a.corner_lb_y = platformer.a.y - (tile:getHeight()/2)
    platformer.a.corner_rt_x = platformer.a.x + (tile:getWidth()/2)
    platformer.a.corner_rt_y = platformer.a.y + (tile:getHeight()/2)
    if (0 > platformer.a.corner_lb_x) and (0 < platformer.a.corner_rt_x) and (platformer.y > platformer.a.corner_lb_y) and (platformer.y < platformer.a.corner_rt_y) then
        platformer.touching = 1
    else if true then
        platformer.touching = 0
    end
    if platformer.touching == 0 then
        if love.keyboard.isDown("d") then
            platformer.scroll = platformer.scroll - 2
        end
    end
     platformer.y = platformer.y + 1
end

function love.draw()
    love.graphics.setBackgroundColor(0,0.2,0.8)
    love.graphics.rectangle("fill",400,400,10,10)
end

Re: Weird error for a function that is correct.

Posted: Tue Apr 09, 2024 11:59 pm
by BrotSagtMist
line 11 is not closed.
Get an editor with block checks, really handy.
Also look at line 18 where you have a mess.

Re: Weird error for a function that is correct.

Posted: Wed Apr 10, 2024 12:05 am
by SuskyTheCorgi
Wdym line 11 is not closed? I do have an editor with block check too btw. At line 18 that is what I need to type, what ELSE am I supposed to do there? Thank you for replying, and please reply back when you can.

Re: Weird error for a function that is correct.

Posted: Wed Apr 10, 2024 12:30 am
by BrotSagtMist
It means my editor flags the function as not having a matching end, it gets eaten at line 18.
"if true then" < why have you came up with this? This line effectively is not exectuted as if true cancels itself. But it eats an extra end.

Re: Weird error for a function that is correct.

Posted: Wed Apr 10, 2024 7:08 am
by zorg
SuskyTheCorgi wrote: Wed Apr 10, 2024 12:05 am Wdym line 11 is not closed? I do have an editor with block check too btw. At line 18 that is what I need to type, what ELSE am I supposed to do there? Thank you for replying, and please reply back when you can.
I doubt that's what you need to type.

Code: Select all

    if (0 > platformer.a.corner_lb_x) and (0 < platformer.a.corner_rt_x) and (platformer.y > platformer.a.corner_lb_y) and (platformer.y < platformer.a.corner_rt_y) then
        platformer.touching = 1
    else if true then
        platformer.touching = 0
    end
This part makes no sense like this; you set platformer.touching to 1 if those 4 conditions are all true, but then you set it to 0 regardless, since "if true then" is always going to execute... and that also needs an end, hence the error in the first place.
What did you want to do there?
Did you want to just put an else there, like this?:

Code: Select all

    if (0 > platformer.a.corner_lb_x) and (0 < platformer.a.corner_rt_x) and (platformer.y > platformer.a.corner_lb_y) and (platformer.y < platformer.a.corner_rt_y) then
        platformer.touching = 1
    else
        platformer.touching = 0
    end

Re: Weird error for a function that is correct.

Posted: Wed Apr 10, 2024 11:56 am
by pgimeno
This:

Code: Select all

if x then
  something()
else if y then
  something_else()
end
is exactly the same as this:

Code: Select all

if x then
  something()
else
  if y then
    something_else()
  end
In the latter case, you can clearly see how the else is not closed. That's why we have elseif. This is properly closed:

Code: Select all

if x then
  something()
elseif y then
  something_else()
end

Re: Weird error for a function that is correct.

Posted: Wed Apr 10, 2024 12:20 pm
by SuskyTheCorgi
NOPE. I need that incase the if statement is false, that is definitely what I need. I have not programmed every tile into a read/count system for the if condition because it's just a test. That will make it faster in the future.

Re: Weird error for a function that is correct.

Posted: Wed Apr 10, 2024 12:21 pm
by SuskyTheCorgi
pgimeno wrote: Wed Apr 10, 2024 11:56 am This:

Code: Select all

if x then
  something()
else if y then
  something_else()
end
is exactly the same as this:

Code: Select all

if x then
  something()
else
  if y then
    something_else()
  end
In the latter case, you can clearly see how the else is not closed. That's why we have elseif. This is properly closed:

Code: Select all

if x then
  something()
elseif y then
  something_else()
end
oops wrong person sorry

Re: Weird error for a function that is correct.

Posted: Wed Apr 10, 2024 12:23 pm
by SuskyTheCorgi
pgimeno wrote: Wed Apr 10, 2024 11:56 am This:

Code: Select all

if x then
  something()
else if y then
  something_else()
end
is exactly the same as this:

Code: Select all

if x then
  something()
else
  if y then
    something_else()
  end
In the latter case, you can clearly see how the else is not closed. That's why we have elseif. This is properly closed:

Code: Select all

if x then
  something()
elseif y then
  something_else()
end
TYSM!

Re: Weird error for a function that is correct.

Posted: Wed Apr 10, 2024 1:22 pm
by BrotSagtMist
Just adding: Youre trying to set a bolean using if cases on a bolean.
Thats just adding extra steps, if you dont have anything else that uses it just do it in one go:

Code: Select all

    if (0 > platformer.a.corner_lb_x) and (0 < platformer.a.corner_rt_x) and (platformer.y > platformer.a.corner_lb_y) and (platformer.y < platformer.a.corner_rt_y)  then
        if love.keyboard.isDown("d") then
            platformer.scroll = platformer.scroll - 2
        end
    end