What is the point of love.load?

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.
128Gigabytes
Prole
Posts: 14
Joined: Wed Jan 04, 2017 5:53 pm

What is the point of love.load?

Post by 128Gigabytes »

I already know lua pretty well and last night I decided to learn to use Love2D, and I just have a quick question about love.load

I tried looking up the answer myself, but all I got was things saying what it does (Runs once at the very start of your code)

Why should I do this

Code: Select all

function love.load()
	example = "Hello world."
end

function love.update(elapse)

end

function love.draw()
	love.graphics.print(example)
end
and not this

Code: Select all

local example = "Hello world."

function love.update(elapse)

end

function love.draw()
	love.graphics.print(example)
end
The only thing the love.load key seems to change is I can't just local variables (Since they will be local to the load function)
Is it just about keeping things organized, or does love.load actually do something special that means I should use it?
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: What is the point of love.load?

Post by airstruck »

Take a look here: https://love2d.org/wiki/love.run

The RNG is seeded after the body of main.lua runs, but before love.load is called, so if you want stuff to happen on startup that needs the RNG to be seeded, it should probably go in love.load. You could seed the RNG yourself in the body of main, but it'll get re-seeded by love.run, so you'd need to set your seed again in love.load. In my opinion the RNG should be seeded earlier in the app lifecycle to avoid that problem, and love.load should be deprecated, but that's just my opinion (because it's not obvious that the only point of love.load is that the RNG is seeded before it, and because that behavior's not particularly useful anyway).

Also the "arg" table (command line args, as you probably know) is passed to love.load, but that doesn't really matter since you have access to it globally anyway.
128Gigabytes wrote:does love.load actually do something special that means I should use it?
Nope, not really, except for the RNG thing.
128Gigabytes
Prole
Posts: 14
Joined: Wed Jan 04, 2017 5:53 pm

Re: What is the point of love.load?

Post by 128Gigabytes »

Thank you @airstruck, that actually helped my understand what is going on. I know for a fact I won't be using rng, so I think I skip using love.load. (I'm not actually making a game, I'm making a data manager so I can't see why I would need that to be random.)
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: What is the point of love.load?

Post by zorg »

This is more or less the whole process, from top to bottom in order:

Löve loads in boot.lua:
- require "love"

love.boot() called -- (1st xpcall in boot.lua)
- Require love.filesystem (the only module that you can't disable in conf.lua, it's mandatory)
- Parse command line arguments (they will reside in the global arg table)
- Determine if game is fused or not
- Set game identity
- Early check whether main.lua or conf.lua exists or not (if not, then errors later with the game being badly packaged)

love.init() called -- (2nd xpcall in boot.lua)
- Create default configuration settings
- Load in configuration settings from conf.lua if it exists (or if love.conf exists, even without conf.lua)
- Require modules
- Create event handlers
- Check version (compatibility related)
- Setup window (if window module is loaded)
- Set first timestep
- Require main.lua -- (Loads in the body of main.lua)
- Error if game is badly packaged.

love.run() called -- (3rd xpcall in boot.lua (if using the default love.run))
- seed löve's default PRNG
- love.load(args) -- (Actual game startup... usually)
loop:
- poll events -- (other love.___ callback functions)
- love.update(dt)
- love.draw

- sleep

Most of the lines can be safely ignored. Bold ones matter more, in terms of flow, in my opinion. Italics are optional, more or less.
Now, the two underlined lines are the two places you were asking about.

There's really only two reasons in my opinion, to use love.load instead of just calling stuff from the body of main.lua itself (different ones from what airstruck had)

1. If you put your startup stuff into love.load, you can actually reset your game/program/project/whatever by calling love.load again (that is, if you're mindful of how you code things), since under normal circumstances, loading the whole main.lua in again instead, may not be that good of an idea...

2. If you package the game into a .love, you may try to load in stuff with require, into the main.lua file scope, despite the fact that it may be badly packaged, hence you won't get the more usable error message about it being badly packged, instead you'll get one about not finding the stuff you'd require in, for example, which might not tell you obviously what the issue is.

In short, it's cleaner, but that one's just my opinion.
Also, your love.load example is a bit flawed, this would be the correct code:

Code: Select all

local example -- unless you like globals all over the place and have the brain to keep track of them all :3
function love.load()
   example = "Hello world."
end

function love.update(elapse)

end

function love.draw()
   love.graphics.print(example)
end
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
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: What is the point of love.load?

Post by airstruck »

zorg wrote: 1. If you put your startup stuff into love.load, you can actually reset your game/program/project/whatever by calling love.load again
I thought about that, but I'm not sure there's any real advantage over just defining whatever function you want to reset everything (and then calling it from main.lua, and wherever else you want). Not sure about the second thing, never noticed that.

One place love.load is actually useful is in conf.lua (if you don't want a main.lua for whatever reason). You'd need to use it there because many of the modules haven't been loaded when conf.lua runs.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: What is the point of love.load?

Post by zorg »

The only advantage i can think of in contrast to defining your own reset function is that it's already there, so why not use it?
Also, you still can define a function that'd reset the game elsewhere, but call it from love.load.
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
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: What is the point of love.load?

Post by airstruck »

zorg wrote:The only advantage i can think of in contrast to defining your own reset function is that it's already there, so why not use it?
I guess you might as well if your reset function doesn't take any parameters.
Also, you still can define a function that'd reset the game elsewhere, but call it from love.load.
Or you could call that function from main.lua.

Code: Select all

-- main.lua
local game = require 'game'
game.reset()
vs

Code: Select all

-- main.lua
local game = require 'game'
function love.load (arg)
    game.reset()
end
Although it would be nice if game.reset could seed the RNG without getting overridden by love.run the first time.

edit: fixed example code
Last edited by airstruck on Thu Jan 05, 2017 1:44 am, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: What is the point of love.load?

Post by zorg »

You can define your own non-seeding love.run though, and seed the rng wherever else... though if i want to use prng-s with custom seeds, i'd create a dedicated prng object instead.
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
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: What is the point of love.load?

Post by airstruck »

IMO love.run is doing too many things for an overridable function. I don't feel good about overriding it unless it's really necessary.

I forgot all about RandomGenerator, that's probably a good way to go.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: What is the point of love.load?

Post by raidho36 »

Oddly enough, you can override the main run function, but you can't override shader initialization snippet.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 33 guests