I need help with loading lua files (my brain cannot comprehend the wiki)

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
User avatar
MoreMending
Prole
Posts: 1
Joined: Sun Dec 31, 2023 3:09 am

I need help with loading lua files (my brain cannot comprehend the wiki)

Post by MoreMending »

So I'm really new to love2d, and tried goofing around with loading files and the sort, but when I tried loading and running this lua file, I got this error:
Error

main.lua:35: attempt to call global 'GAME_START' (a nil value)


Traceback

[love "callbacks.lua"]:228: in function 'handler'
main.lua:35: in function 'update'
[love "callbacks.lua"]:162: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'

Armed with the line where the error occurred, I changed the variable that could be described as pointing to a function with the arguments filled out into a fully defined function (its lines 33-35 now, also I'm a little perplexed on why the little variable thingy I originally tried didn't work, but nonetheless), with there still being a line right after it that calls the function that loads the lua file. When I tried this, I didn't see a blue error screen, from that, I was hoping that the lua file would end up running, but of course, it didn't. I'm just trying to learn how to load files and stuff at this point because thats gonna be like the most important thing if I'm gonna use this a lot. I honestly just need help on how this sort of stuff works and how I messed up and then I'll be on my merry way
Attachments
thingy123123.love
did you know today is 123123 in the mm-dd-yy format
(2.62 KiB) Downloaded 41 times
I come from the majestical lands of roblox luau and doomworld
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: I need help with loading lua files (my brain cannot comprehend the wiki)

Post by zorg »

Hi and welcome to the forums.

So, first off, löve uses a virtual filesystem where the root '/' points to two places; one being the save directory, and one being the place where main.lua is in.

- You can run a folder containing main.lua with löve, and it'll run your project.
- You can put the *contents* of that folder into a zip archive, rename it to have the .love extension (not really necessary though) and run that with löve, and it'll run your project; it's also easily shareable that way, but a pain to do if you're developing, so you shouldn't do that.
- On windows, you can also combine the executable with that .love file, which is what fusing is; this way, you can bundle the dll-s next to love.exe with your fused executable to distribute the game simply for windows.

Usually people use lua's require function to load in other scripts, which has the upside of the returned code being stored so no matter how many times you require a file with the same path, you'll get back the cached code that was already loaded the first time.

If you don't want that behaviour, then love.filesystem.load is the one you want, however, you need to call the loaded chunk yourself with an extra set of parentheses for it to be executed; your code is not doing that.


Your GAME_START function definition should probably not be in love.update, considering löve calls that function (if it exists) every frame; you do not want to redefine it every frame. Calling is fine though.

You also don't want to nest callback functions in general; like defining love.keypressed inside love.update as well, that's bad.


In love.load, you don't need to mount the assets directory, considering it's already accessible with the path `Assets`... if you want it to have a different folder name, just rename the folder... :v
As for the fused part of it; considering most use-cases put all assets into the executable file itself, you don't necessarily need to do that either, unless you do need the assets to be outside of the file... and this obviously doesn't work with a .love file in the first place...


Finally, you can only have one of each callback functions, but you're re-defining love.draw in both main.lua and START.lua (lower/upper-case consistency would also be good to keep, btw), which is bad, since the one defined later will overwrite the older one... If you want to separate things into different files while also having functions be called from update, draw or wherever else, you can define those functions with different names, and then call those within the callbacks you defined in one place... assuming they exist at the time of calling.
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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 74 guests