Page 1 of 2

Opening .lua files

Posted: Mon Feb 27, 2012 9:38 am
by brozenadenzen
Hi, I might be double posting but I have a question. I have just started creating a simple game, but I would like to make different things in separate files. My question is how do I open and execute .lua files in the same session (without re-opening Love again). Thanks in advance.

Re: Opening .lua files

Posted: Mon Feb 27, 2012 9:44 am
by molul
I do this:

Code: Select all

gReloadableModules = { "bla.lua" , "blabla.lua", "blablabla.lua", }

function love.keypressed(key, unicode)
	if (key == "f5") then reloadModules() end
end

function reloadModules() 
	for _, module in ipairs(gReloadableModules) do
		love.filesystem.load(module)()
	end
end
The global table and the two functions are in different files, but you get the idea.

Re: Opening .lua files

Posted: Tue Feb 28, 2012 6:24 pm
by veethree
If you mean coding various game mechanics in their own lua files then i might have a solution, this is the way i usually do things.


Example:
Main.lua:

Code: Select all

require "pissant" --pissant would be the name of the lua file, require basically loads the file.
function love.load()
	initPissant() --calling the function from the pissant file
	pissOnPissant()
end

function love.update(dt)
	updatePissant(dt) -- same as above
end

function love.draw()
	drawPissant() -- same as above
	love.graphics.print("piss left: "..piss, 10, 10)
end
pissant.lua:

Code: Select all

function initPissant()
	--whatever you do here happens when main.lua is loaded
end

function updatePissant(dt)
	--whatever you do here happens when main.lua is updated
end

function drawPissant()
	--you get the point by now i hope.
end

function pissOnPissant()
	--You can call this function in main.lua if you so desire.
	--Also, All the lua files you require share the global variables, Meaning if you have a variable
	--Here like so:
	piss = 38
	--you could change or refrence it in main.lua aswell.
	--Assuming you've called this function in main.lua that is.
end
Excuse the pissant stuff, That word has been stuck in my head recently..hope this was at least a little tiny bit helpful.

if that's not what you meant, sorry for wasting your time. lol

Re: Opening .lua files

Posted: Sat Mar 03, 2012 11:42 am
by Golan2781
Just to clarify how the system works:
You can load LUA code from outside the main.lua file by using require. It's a function so you probably want to call it at the start of your main file.

Code: Select all

require "myLib"

function love.load()
...
Should you need to, you can also use require in a file already added to your game with a require call connected to main.lua.

Code: Select all

-- myLib.Lua
require "myOtherLib"
You'll most likely want to use separate files for storing functions. When loading a Lua file containing functions, these functions become available in your program but are not executed. You must call them directly or indirectly from the love.load, love.update or love.draw functions or execute them separately at the start of a required file.

Code: Select all

-- main.lua
require "myLib"

function love.load()
	myMainVariable = myLoadFunction(myGlobalVariable ,7)
end

Code: Select all

-- myLib.Lua
myGlobalVariable = myGimmeNumberFunction()
function myLoadFunction(a,b)
	return a+b
end
function myGimmeNumberFunction()
	return 5
end
Functions and variables added in this way are global unless specifically set to local. Keep this in mind to avoid collisions.

In addition, you can use require to run a basic function. This is very useful for loading data from a file by having the function simply return the data.

Code: Select all

-- main.lua
function love.load()
	myMainArray = require "MyStoredArray"
end

Code: Select all

-- MyStoredArray.lua
return {1,b,42,1138}
€dit
Thanks for spotting the typos and wrong formating, Robin.

Re: Opening .lua files

Posted: Sat Mar 03, 2012 12:14 pm
by Robin
You made a few typos, by writing require = in several places instead of require.

EDIT: also, functions need to start with function.

Re: Opening .lua files

Posted: Sun Mar 04, 2012 7:45 am
by sanjiv
Follow up question:
How do I 'require' files that are nested within folders? I've successfully used 'require' to call other lua files outside my main, but then I'm running into problems when I try to organize those files into folders.

For example, I've been able to require ('cyberGrid.lua'), but have had trouble when I move cyberGrid.lua into a 'cyber' folder. Should I be using require('cyber.cyberGrid.lua'), or require('cyber/cyberGrid.lua'), or cyber=somethingSomethingSomething?

Thanks in advance.

(also, I've been looking for information on require for a little too long. It's so simple and so useful, yet there's no mention of it on the WIki. Me thinks we need a hero)

Re: Opening .lua files

Posted: Sun Mar 04, 2012 8:09 am
by Jasoco

Code: Select all

require 'folder.file'
No extension and no slashes. Instead of slashes you use a period.

Re: Opening .lua files

Posted: Sun Mar 04, 2012 3:06 pm
by MarekkPie
(I've always used slashes and its been fine. Maybe LOVE is converting to periods behind the scenes. Or maybe it's the other way around. Whatever.)

You also need to use absolute paths (starting from the directory your main.lua file is in), rather than relative paths. For example, if I am in /dir/subdir/foo.lua, and I need to require a file (bar.lua) in /dir/:

Code: Select all

require "../bar" -- This will not work

require "dir/bar" -- This will work
Some library makers out there have made some regex-like tweaks that allow for relative paths, but the correct way is absolute paths.

Edit: Fixed typo

Re: Opening .lua files

Posted: Sun Mar 04, 2012 3:33 pm
by tentus
MarekkPie wrote:(I've always used slashes and its been fine. Maybe LOVE is converting to periods behind the scenes. Or maybe it's the other way around. Whatever.)
My understanding is that it's the other way- periods get converted to the appropriate directory separator (/ or \). Also, you don't need one at the front of the require:

Code: Select all

require "foo.bar"

Re: Opening .lua files

Posted: Sun Mar 04, 2012 9:58 pm
by Robin
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.