Opening .lua files

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.
brozenadenzen
Prole
Posts: 14
Joined: Tue Feb 14, 2012 5:04 pm

Opening .lua files

Post 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.
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: Opening .lua files

Post 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.
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: Opening .lua files

Post 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
User avatar
Golan2781
Prole
Posts: 8
Joined: Sat Jan 07, 2012 9:13 pm

Re: Opening .lua files

Post 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.
Last edited by Golan2781 on Sat Mar 03, 2012 12:34 pm, edited 1 time in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Opening .lua files

Post by Robin »

You made a few typos, by writing require = in several places instead of require.

EDIT: also, functions need to start with function.
Help us help you: attach a .love.
User avatar
sanjiv
Citizen
Posts: 88
Joined: Mon Feb 27, 2012 5:11 am

Re: Opening .lua files

Post 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)
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: Opening .lua files

Post by Jasoco »

Code: Select all

require 'folder.file'
No extension and no slashes. Instead of slashes you use a period.
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Opening .lua files

Post 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
Last edited by MarekkPie on Sun Mar 04, 2012 3:57 pm, edited 1 time in total.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Opening .lua files

Post 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"
Kurosuke needs beta testers
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Opening .lua files

Post 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.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot], Google [Bot], Semrush [Bot] and 3 guests