About the love.load function

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
galaxified
Prole
Posts: 3
Joined: Sun Aug 07, 2022 4:21 am

About the love.load function

Post 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?
User avatar
BrotSagtMist
Party member
Posts: 607
Joined: Fri Aug 06, 2021 10:30 pm

Re: About the love.load function

Post 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.
obey
User avatar
togFox
Party member
Posts: 770
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: About the love.load function

Post 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. :)
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
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: About the love.load function

Post 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.
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.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: About the love.load function

Post 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.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: About the love.load function

Post 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.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: About the love.load function

Post 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).
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
Post Reply

Who is online

Users browsing this forum: Google [Bot], 麻猫和黄猫 and 61 guests