load() function?

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.
User avatar
napco
Party member
Posts: 129
Joined: Fri Jun 12, 2009 9:28 pm
Location: Ital... ehm...

load() function?

Post by napco »

Hi! Why should i use the "load()" function instead of simply writing my include and load stuff on the top of the main script? And what kind of code should i put inside that function?
User avatar
TechnoCat
Inner party member
Posts: 1611
Joined: Thu Jul 30, 2009 12:31 am
Location: Denver, CO
Contact:

Re: load() function?

Post by TechnoCat »

load( ) callback documentation
This function is called once.
I'm not sure what the benefit of using load() instead of putting it outside a function is though. It might just be for reasons of scope.
User avatar
napco
Party member
Posts: 129
Joined: Fri Jun 12, 2009 9:28 pm
Location: Ital... ehm...

Re: load() function?

Post by napco »

Thanks! Maybe i'll include and declare constants outside it and put the rest into the load() function...
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: load() function?

Post by osgeld »

I too questioned load()

then farting around one night i noticed that some of loves stuff wasn't running outside of load() so i use it :x

it is redundant tho
User avatar
Pliskin09
Citizen
Posts: 89
Joined: Fri Jul 24, 2009 8:30 am

Re: load() function?

Post by Pliskin09 »

i just like load because of how it helps organize your game. its especially useful if your new to Lua and dont know what your doing just yet. looking back now (well, im only looking back like a week or so) i found it helpful for understanding how the game was structured but at the moment i dont need it.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: load() function?

Post by Robin »

Pliskin09 wrote:i just like load because of how it helps organize your game.
It does help structuring the game. If all the initializing stuff is littered all over top-level, it's easy to loose oversight. Generally, the only things I place outside of load() or other functions are require()s.
Help us help you: attach a .love.
User avatar
napco
Party member
Posts: 129
Joined: Fri Jun 12, 2009 9:28 pm
Location: Ital... ehm...

Re: load() function?

Post by napco »

Ah, and here comes the second question... What's the difference between love.filesystem.include() and love.filesystem.require()? I'm using include because it reminds me C...
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: load() function?

Post by Robin »

include() can load the file multiple file, and require() makes sure it is only loaded once.
For example:
In libtest.lua:

Code: Select all

a=6
print(a)
In main.lua:

Code: Select all

love.filesystem.include("libtest.lua")
love.filesystem.include("libtest.lua")
love.filesystem.include("libtest.lua")
Prints:

Code: Select all

6
6
6
If we replace the code in main.lua with:

Code: Select all

love.filesystem.require("libtest.lua")
love.filesystem.require("libtest.lua")
love.filesystem.require("libtest.lua")
It prints

Code: Select all

6
Using require() is especially useful if you have multiple files, and you want them all to require() some library.
Help us help you: attach a .love.
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: load() function?

Post by rude »

That love.filesystem.require/include rubbish will be removed, by the way. Lua's standard require function will be love.filesystem-aware from now on.

Also considering removing load(), as it is indeed redundant. Then again, people who don't want to use load() can just ignore it and put load-time stuff in the main chunk.
User avatar
napco
Party member
Posts: 129
Joined: Fri Jun 12, 2009 9:28 pm
Location: Ital... ehm...

Re: load() function?

Post by napco »

I see... love.filesystem.include() == dofile() and love.filesystem.load() = loadfile()
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 67 guests