error i encountered while working on a project (solved)

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
kappaster
Prole
Posts: 3
Joined: Wed Feb 02, 2022 3:39 am

error i encountered while working on a project (solved)

Post by kappaster »

a few months ago i decided to learn this game engine, and now i am sticking with it, so i decided to make a project with it



but i came across an error:

Error

Syntax error: main.lua:2221: 'end' expected (to close 'function' at line 273) near '<eof>'



Traceback

[C]: at 0x7ffd68d228f0
[C]: in function 'require'
[C]: in function 'xpcall'
[C]: in function 'xpcall'


i made sure everything was okay about each end, but then found it was all completely normal. i wondered if i had exceeded some sort of limit for lines...

file : https://mega.nz/file/SAZFVIQK#IEJO3h-5I ... 7n41Mjk5ro
Last edited by kappaster on Wed Feb 02, 2022 11:02 pm, edited 1 time in total.
User avatar
Nikki
Citizen
Posts: 86
Joined: Wed Jan 25, 2017 5:42 pm

Re: error i encountered while working on a project

Post by Nikki »

Your love.update function starts at line 273, it doesnt have an end
that 'end' ought to be somewhere on line 2200

the error message says this too, and an editor with some auto-indent capabilities might help with this issue, another thing , after decade of programming I like the directness of just writing code that does the job without worrying too much about extracting functions etc, but your code would be a lot easier to reason about if you instead of duplicating the same blocks over and over would use a little function and a for loop occasionally .
yal2du
Prole
Posts: 42
Joined: Wed Oct 13, 2021 5:41 pm

Re: error i encountered while working on a project

Post by yal2du »

debugging is a process of elimination; syntax errors are the easiest because the compiler actually finds the error for you. in this case, the function love.update is missing a closing 'end' statement. the reason you can't figure out what is wrong is because love.update is ~2k lines of if then clauses(!) the first one (the one beginning with " if love.keyboard.isDown("return") " is the size of the whole function. this is most easily seen using an editor that supports folding nested statements. break the function down into smaller parts to isolate the issue. maybe make a different function for each scene, and use a state machine to move through them.

e.g.

Code: Select all


scenes = {}
currentscene = 1

local function scene1(...)
  -- do whatever
end

scenes.scene1  = scene1
-- add rest of scene functions to table of scenes

function love.update(dt)
	scenes['scene'..tostring(currentscene)]()   -- update the current scene
end
User avatar
EngineerSmith
Prole
Posts: 38
Joined: Thu Dec 02, 2021 11:38 am
Contact:

Re: error i encountered while working on a project

Post by EngineerSmith »

That's a big update loop. You should check out ways to split your code into multiple files so you don't end up with a blob file like this and it's much more manageable/maintainable.

There's an end missing on Ln375
yal2du
Prole
Posts: 42
Joined: Wed Oct 13, 2021 5:41 pm

Re: error i encountered while working on a project

Post by yal2du »

EngineerSmith wrote: Wed Feb 02, 2022 12:35 pm There's an end missing on Ln375
spoiler alert!
kappaster
Prole
Posts: 3
Joined: Wed Feb 02, 2022 3:39 am

Re: error i encountered while working on a project

Post by kappaster »

yal2du wrote: Wed Feb 02, 2022 12:32 pm debugging is a process of elimination; syntax errors are the easiest because the compiler actually finds the error for you. in this case, the function love.update is missing a closing 'end' statement. the reason you can't figure out what is wrong is because love.update is ~2k lines of if then clauses(!) the first one (the one beginning with " if love.keyboard.isDown("return") " is the size of the whole function. this is most easily seen using an editor that supports folding nested statements. break the function down into smaller parts to isolate the issue. maybe make a different function for each scene, and use a state machine to move through them.

e.g.

Code: Select all


scenes = {}
currentscene = 1

local function scene1(...)
  -- do whatever
end

scenes.scene1  = scene1
-- add rest of scene functions to table of scenes

function love.update(dt)
	scenes['scene'..tostring(currentscene)]()   -- update the current scene
end

thanks for the advice. might try it for future things
kappaster
Prole
Posts: 3
Joined: Wed Feb 02, 2022 3:39 am

Re: error i encountered while working on a project

Post by kappaster »

EngineerSmith wrote: Wed Feb 02, 2022 12:35 pm That's a big update loop. You should check out ways to split your code into multiple files so you don't end up with a blob file like this and it's much more manageable/maintainable.

There's an end missing on Ln375
also finally realizing where the end was made me feel like i was blind, but no worries
thanks
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: error i encountered while working on a project (solved)

Post by pgimeno »

I've written a program to easily detect these kinds of problems:

https://codeberg.org/pgimeno/Gists/src/ ... nt-checker

When applied to this file it says this:

Code: Select all

$ lua indentchecker.lua big.lua 
**** big.lua
Warning at line 376: Misindented elseif (if)
Warning at line 463: Misindented elseif (if)
Warning at line 480: Misindented end (if)
Warning at line 2199: Misindented end (if)
Warning at line 2221: Unexpected EOF looking for block terminator
All errors past the first are a consequence of that one, so fixing the first one makes it pass.
User avatar
dusoft
Party member
Posts: 635
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: error i encountered while working on a project (solved)

Post by dusoft »

pgimeno wrote: Fri Feb 04, 2022 5:48 pm I've written a program to easily detect these kinds of problems:

https://codeberg.org/pgimeno/Gists/src/ ... nt-checker

When applied to this file it says this:

Code: Select all

$ lua indentchecker.lua big.lua 
**** big.lua
Warning at line 376: Misindented elseif (if)
Warning at line 463: Misindented elseif (if)
Warning at line 480: Misindented end (if)
Warning at line 2199: Misindented end (if)
Warning at line 2221: Unexpected EOF looking for block terminator
All errors past the first are a consequence of that one, so fixing the first one makes it pass.
Any decent editor will report this without need to run another script. But that's useful for people who just use some basic notepad or such.

I recommend Sublime and Atom editors. Both feature syntax highlighting for Lua and there are packages specifically made for LOVE including function hinting and autocomplete.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 7 guests