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.
-
- Prole
- Posts: 14
- Joined: Tue Feb 14, 2012 5:04 pm
Opening .lua files
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
I do this:
The global table and the two functions are in different files, but you get the idea.
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
Re: Opening .lua files
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:
pissant.lua:
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
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
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
if that's not what you meant, sorry for wasting your time. lol
Re: Opening .lua files
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.
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.
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.
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.
€dit
Thanks for spotting the typos and wrong formating, Robin.
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()
...
Code: Select all
-- myLib.Lua
require "myOtherLib"
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
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}
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.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Opening .lua files
You made a few typos, by writing require = in several places instead of require.
EDIT: also, functions need to start with function.
EDIT: also, functions need to start with function.
Help us help you: attach a .love.
Re: Opening .lua files
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)
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)
- Jasoco
- Inner party member
- Posts: 3727
- Joined: Mon Jun 22, 2009 9:35 am
- Location: Pennsylvania, USA
- Contact:
Re: Opening .lua files
Code: Select all
require 'folder.file'
Re: Opening .lua files
(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/:
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
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
Edit: Fixed typo
Last edited by MarekkPie on Sun Mar 04, 2012 3:57 pm, edited 1 time in total.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Opening .lua files
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: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.)
Code: Select all
require "foo.bar"
Kurosuke needs beta testers
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Opening .lua files
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.
Who is online
Users browsing this forum: Amazon [Bot], Bing [Bot] and 6 guests