Page 1 of 1
About the love.load function
Posted: Fri Aug 12, 2022 11:35 pm
by galaxified
So my question is, how is it used? Of course, I know that it only happens once, loads stuff and all that, but wouldn't it be the same if you put the stuff that needs to load at the start of the code? Also, if you want to use local, then if you use it in love.load, you can use it only there, which is useless like 50% of the time.
So is the love.load function actually needed? Is there something I'm not getting?
Re: About the love.load function
Posted: Sat Aug 13, 2022 12:33 am
by BrotSagtMist
Well its called AFTER everything in the main code, you can use it for ordering a bit.
But no, it is not really useful at all. Its just an convention that makes understanding other peoples code easier.
The same uselessness applies to every other love core function. Have a look at:
https://love2d.org/wiki/love.run
This is the only place these names are used. There is no _need_ to use draw or update at all (and i dont), but if you are sharing code in this forum and it does not follow this form of functions splited by purpose then you will likely get no support for it.
Ah btw there is a name for a local that is accessable everywhere: Its a global.
Re: About the love.load function
Posted: Sat Aug 13, 2022 2:05 am
by togFox
Brot summed it up. I use love.load simply to keep my code tidy.
A local inside ANY function scopes it to that function. If that's not what you want then don't do that.
Re: About the love.load function
Posted: Sat Aug 13, 2022 5:02 am
by zorg
One thing that might be useful about love.load is that if you restart your game programmatically, love.load will be called again; i'm not sure if the body of main.lua will though.
Also, you can still define locals in the main body outside of any function, but assign values inside love.load, that works as intended.
Re: About the love.load function
Posted: Sat Aug 13, 2022 8:55 am
by ReFreezed
zorg wrote: ↑Sat Aug 13, 2022 5:02 am
One thing that might be useful about love.load is that if you restart your game programmatically, love.load will be called again; i'm not sure if the body of main.lua will though.
The whole Lua state is restarted. All code runs again (including conf.lua).
In fact, if you don't have a main.lua, only a conf.lua, then defining love.load is more useful since no LÖVE modules are loaded yet in the main chunk, so you can't load images etc. at that point.
Re: About the love.load function
Posted: Sat Aug 13, 2022 4:13 pm
by pgimeno
You can declare locals outside and assign them inside love.load:
Code: Select all
local img
function love.load()
img = love.graphics.newImage(...)
end
Then they are local to the Lua file they are in (main.lua for example).
There was a time when the random number generator was initialized between loading main.lua and executing love.load, so if you used random numbers in the top scope of main.lua, they weren't random. That's no longer the case.
Re: About the love.load function
Posted: Wed Aug 17, 2022 5:50 pm
by milon
Personally, I either don't use love.load at all, or else I use it as pgimeno stated - declare things at the top of main.lua, and assign them elsewhere. Helps me keep my code clean(ish).