how to check for errors in the syntax of scripts for love2d?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
borr
Prole
Posts: 42
Joined: Wed Oct 16, 2019 7:39 pm

how to check for errors in the syntax of scripts for love2d?

Post by borr »

can i check all project files?
how to output the check result to the console?
which tool is best for this?
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: how to check for errors in the syntax of scripts for love2d?

Post by ivan »

can i check all project files?
Yes, when you use require/loadscript/dofile the Lua interpreter checks for syntax errors. All you have to do is "require" all of your files.
how to output the check result to the console?
By using the --console option: "love.exe mygame.love --console"
which tool is best for this?
My SUPERSTRICT tool finds undefined variables and other mistakes which the Lua interpreter ignores:
https://love2d.org/forums/viewtopic.php?f=5&t=90074
MrFariator
Party member
Posts: 510
Joined: Wed Oct 05, 2016 11:53 am

Re: how to check for errors in the syntax of scripts for love2d?

Post by MrFariator »

To add to ivan's response, you could also use Luacheck.
borr
Prole
Posts: 42
Joined: Wed Oct 16, 2019 7:39 pm

Re: how to check for errors in the syntax of scripts for love2d?

Post by borr »

thanks
User avatar
togFox
Party member
Posts: 774
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: how to check for errors in the syntax of scripts for love2d?

Post by togFox »

I read the super strict git hub page but don't know where to start. Do I run it as a separate script? Do I REQUIRE it from my own project and it magically works? Sorry for the dumb questions. It just seems I am currently below the assumed level of knowledge. :(
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: how to check for errors in the syntax of scripts for love2d?

Post by ivan »

togFox wrote: Fri Feb 12, 2021 6:50 am Do I REQUIRE it from my own project and it magically works
Yes, but do it at the start. It doesn't check files that have already been loaded.
User avatar
togFox
Party member
Posts: 774
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: how to check for errors in the syntax of scripts for love2d?

Post by togFox »

Well, I gotta say, I did the

Code: Select all

require "sstrict.sstrict"
as instructed and nothing happened. Thinking I was the goose for not understanding this, I didn't think about it and moved on.

Today, after perhaps a week, I added a 5 year old module I found on an old github (dabutton gui library) and all of a sudden sstrict has gone bonkers and forcing runtime errors on bad code. :joker:

It was working all along, but my code passed all the tests so I thought it wasn't working. lolz.

Thanks for this one! I'm a bit of a junkie with this sort of thing so it will be a part of my staple from now on.

Any way to make it check main.lua?
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: how to check for errors in the syntax of scripts for love2d?

Post by ivan »

togFox wrote: Mon Feb 15, 2021 8:47 am as instructed and nothing happened.
AFTER you require superstrict it will intercept and check every following call to: require/dofile/loadfile/loadstring
That's why you have to include superstrict at the beginning of your code, before including any other files.
Don't forget to turn it off in production code because it's slow.
sstrict has gone bonkers and forcing runtime errors on bad code. :joker:
For an old file that you DON'T want to check just add the "--!strict" comment at the top.
Also, make sure you get the latest version on bitbucket because I pushed some updates yesterday.
Any way to make it check main.lua?
The easiest way is to just require main twice:

Code: Select all

require("sstrict")
require("main")
A nicer way is to add superstrict to your "conf.lua":

Code: Select all

for k,v in pairs(arg) do
  if v == "--strict" then
    require("sstrict")
    break
  end
end
Then you can enable it using the command line:

Code: Select all

love mygamefolder --console --strict
You don't need to use the lib in realtime.
The best way is to just iterate all the files in your game folder and run them through superstrict.

Code: Select all

require('sstrict').panic = false
will print out all of the errors to the console without throwing errors.
User avatar
togFox
Party member
Posts: 774
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: how to check for errors in the syntax of scripts for love2d?

Post by togFox »

The module I added had a global variable declared on the very first line:

Button = {}

sstrict doesn't like this. I can change the library to declare it LOCAL so sstrict is happy, but the module breaks (because BUTTON is now local).

Would the correct way to do this is to add

local button = {}

In main.lua? That would satisfy sstrict and still enable the module to function.

Thoughts? (Remember the module is 5 year old freeware).
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: how to check for errors in the syntax of scripts for love2d?

Post by ivan »

The whole point of superstrict is that it doesn't allow assigning globals.
You don't want to be assigning and changing globals from everywhere because it makes the code hard to follow and debug.
If this is not your code and you don't want to check it, just add the following line at the top of the problematic file:

Code: Select all

--!strict
Generally speaking you have to declare your globals before including superstrict:

Code: Select all

Button = {}
require("sstrict")
The "standard" way of writing modules is to use a local and then return it at the end:

Code: Select all

local Button = {}
...
return Button
Post Reply

Who is online

Users browsing this forum: No registered users and 96 guests