Page 1 of 1

error i encountered while working on a project (solved)

Posted: Wed Feb 02, 2022 3:46 am
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

Re: error i encountered while working on a project

Posted: Wed Feb 02, 2022 12:29 pm
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 .

Re: error i encountered while working on a project

Posted: Wed Feb 02, 2022 12:32 pm
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

Re: error i encountered while working on a project

Posted: Wed Feb 02, 2022 12:35 pm
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

Re: error i encountered while working on a project

Posted: Wed Feb 02, 2022 12:58 pm
by yal2du
EngineerSmith wrote: Wed Feb 02, 2022 12:35 pm There's an end missing on Ln375
spoiler alert!

Re: error i encountered while working on a project

Posted: Wed Feb 02, 2022 11:01 pm
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

Re: error i encountered while working on a project

Posted: Wed Feb 02, 2022 11:03 pm
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

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

Posted: Fri Feb 04, 2022 5:48 pm
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.

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

Posted: Sun Feb 06, 2022 12:30 am
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.