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?
Opening .lua files
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Opening .lua files
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:
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.
Code: Select all
-- bunch of images
function load()
--other stuff
end
Re: Opening .lua files
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
Pretty much. There's probably more to it, but that's the extent of my understanding.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Opening .lua files
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.
Kurosuke needs beta testers
Re: Opening .lua files
update: I'm exploring how to call pictures within folders, andRobin 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.
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
I always use '/'. It seems to work all the time. Use that.sanjiv wrote:update: I'm exploring how to call pictures within folders, andRobin 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.
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?
Losing a friend's trust is the fastest way to lose a friend, forever. FOREVER!
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Opening .lua files
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.sanjiv wrote:update: I'm exploring how to call pictures within folders, andRobin 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.
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?
Kurosuke needs beta testers
Re: Opening .lua files
Thanks! So slashes for specific files, and periods for folders? Or periods for .lua files as well?
- slime
- Solid Snayke
- Posts: 3170
- Joined: Mon Aug 23, 2010 6:45 am
- Location: Nova Scotia, Canada
- Contact:
Re: Opening .lua files
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).
Who is online
Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot] and 4 guests