Weird error for a function that is correct.

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.
SuskyTheCorgi
Prole
Posts: 8
Joined: Sat Apr 06, 2024 12:30 am

Weird error for a function that is correct.

Post 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
User avatar
BrotSagtMist
Party member
Posts: 615
Joined: Fri Aug 06, 2021 10:30 pm

Re: Weird error for a function that is correct.

Post 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.
obey
SuskyTheCorgi
Prole
Posts: 8
Joined: Sat Apr 06, 2024 12:30 am

Re: Weird error for a function that is correct.

Post 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.
User avatar
BrotSagtMist
Party member
Posts: 615
Joined: Fri Aug 06, 2021 10:30 pm

Re: Weird error for a function that is correct.

Post 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.
obey
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Weird error for a function that is correct.

Post 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
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.
User avatar
pgimeno
Party member
Posts: 3551
Joined: Sun Oct 18, 2015 2:58 pm

Re: Weird error for a function that is correct.

Post 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
SuskyTheCorgi
Prole
Posts: 8
Joined: Sat Apr 06, 2024 12:30 am

Re: Weird error for a function that is correct.

Post 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.
SuskyTheCorgi
Prole
Posts: 8
Joined: Sat Apr 06, 2024 12:30 am

Re: Weird error for a function that is correct.

Post 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
SuskyTheCorgi
Prole
Posts: 8
Joined: Sat Apr 06, 2024 12:30 am

Re: Weird error for a function that is correct.

Post 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!
User avatar
BrotSagtMist
Party member
Posts: 615
Joined: Fri Aug 06, 2021 10:30 pm

Re: Weird error for a function that is correct.

Post 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
obey
Post Reply

Who is online

Users browsing this forum: No registered users and 39 guests