Page 2 of 2
Re: Opening .lua files
Posted: Mon Mar 05, 2012 4:46 am
by sanjiv
another question:
Let's say I start a main.lua with require 'needed file.' Now let's go to that required file. There, I've gotten into the habit of creating a need.Load(), a need.Draw(), and a need.update(), and I load them in the obvious place in main.lua.
My question is about the need.Load(). What should I include in it? And what can I leave out of it? Function definitions?
Re: Opening .lua files
Posted: Mon Mar 05, 2012 4:55 am
by MarekkPie
This will be more individual preference, but at the very least put anything that is only important to that file inside that load() function. When you "require" a file, Lua/LOVE runs through the file and creates variables for everything that is exposed to the global scope. So if I had this file:
Code: Select all
-- bunch of images
function load()
--other stuff
end
As soon as I require that file, all the images will be loaded, even if I do not need them until I call load(). So, you'd want to put those images in the load function, so that they will only get loaded when load() is called. Beyond that, I usually don't bother putting things like other requires inside a function.
Re: Opening .lua files
Posted: Mon Mar 05, 2012 5:03 am
by sanjiv
So if I don't put things specifically into load() (or some other, callable function), then they'll exist around forever as global variables, cluttering up memory?
Re: Opening .lua files
Posted: Mon Mar 05, 2012 5:07 am
by MarekkPie
Pretty much. There's probably more to it, but that's the extent of my understanding.
Re: Opening .lua files
Posted: Mon Mar 05, 2012 5:40 am
by tentus
Building off of Marekk's point, it's good practice to never have anything defined that isn't inside a function. Yes, you can accurately predict when stuff will get loaded if you're paying attention, but in six months when you want to make a small change, walking through your code again just to find variables you didn't put into functions... it can be a pain. Better to use love.load as it is meant to be used, and then follow a similar pattern for your requires.
Re: Opening .lua files
Posted: Mon Mar 12, 2012 1:03 am
by sanjiv
Robin wrote:You should use periods as separators. It's just that LÖVE is currently pretty lenient in what it allows, but that might change in the future.
update: I'm exploring how to call pictures within folders, and
hop0=love.graphics.newImage("pics/hop0.png")
works, but
hop0=love.graphics.newImage("pics.hop0.png")
does not.
When I look into folders, should I always use '/', or only do so within certain circumstances?
Re: Opening .lua files
Posted: Mon Mar 12, 2012 2:14 am
by jradich
sanjiv wrote:Robin wrote:You should use periods as separators. It's just that LÖVE is currently pretty lenient in what it allows, but that might change in the future.
update: I'm exploring how to call pictures within folders, and
hop0=love.graphics.newImage("pics/hop0.png")
works, but
hop0=love.graphics.newImage("pics.hop0.png")
does not.
When I look into folders, should I always use '/', or only do so within certain circumstances?
I always use '/'. It seems to work all the time. Use that.
Re: Opening .lua files
Posted: Mon Mar 12, 2012 5:40 am
by tentus
sanjiv wrote:Robin wrote:You should use periods as separators. It's just that LÖVE is currently pretty lenient in what it allows, but that might change in the future.
update: I'm exploring how to call pictures within folders, and
hop0=love.graphics.newImage("pics/hop0.png")
works, but
hop0=love.graphics.newImage("pics.hop0.png")
does not.
When I look into folders, should I always use '/', or only do so within certain circumstances?
newImage is dealing with a specific file, so use / as your separator. require deals with modules, so use periods. I know that the two appear similar but they're not quite the same thing.
Re: Opening .lua files
Posted: Mon Mar 12, 2012 4:27 pm
by sanjiv
Thanks! So slashes for specific files, and periods for folders? Or periods for .lua files as well?
Re: Opening .lua files
Posted: Mon Mar 12, 2012 4:42 pm
by slime
Slashes for everything except the require function, which takes a 'module' as an argument rather than a folder or a file (the implementation of the module argument is a folder or a file, but without the file extension and using . instead of / to represent directory structure).