Page 1 of 1
How to use multiple lua files?
Posted: Mon May 20, 2013 9:06 pm
by cybercircuit
How do you use multiple lua files in a game? I mean, I know how to create them but I dont know how to use them.
P.S Is something like a class possible? And if so is it possible to use polymorphisim, and how?
Re: How to use multiple lua files?
Posted: Mon May 20, 2013 9:15 pm
by Eamonn
As far as I'm aware, classes do not exist in Lua. There are, however, objects(or table
). For LÖVE, you do not need classes anyway. Lua is a true OOP language, using only objects for things. Like for example:
Code: Select all
love.graphics.print("Example.", 50, 50)
does the following:
1) Go into the love object
2) Look for the graphics object
3) Use the print() function in the graphics object which is inside the love object
Hope this all makes sense
To use a file after you've created it, you do
or
Hope this helps!
Re: How to use multiple lua files?
Posted: Mon May 20, 2013 9:44 pm
by bartbes
Even though classes aren't built into lua, they can easily be implemented with tables, and there's quite a few class libraries for lua floating around on the internet.
Eamonn wrote:
Code: Select all
require("filename")
require "filename"
I'd like to note those are completely equivalent. (And require isn't special, this is a syntax feature available for every function.)
Apart from that, require doesn't take file names, it takes module names, which are closely related to file names, generally, to go from a file name to a module name you follow these steps:
- Get the filename, for example: a/b/c.lua
- Remove the extension: a/b/c
- Replace slashes with dots: a.b.c
Therefore, to load a file at a/b/c.lua you'd run require("a.b.c").
Re: How to use multiple lua files?
Posted: Mon May 20, 2013 9:45 pm
by cybercircuit
Thanks but that dident explain to me exactly how to use multiple lua files
Re: How to use multiple lua files?
Posted: Mon May 20, 2013 9:46 pm
by cybercircuit
bartbes wrote:Even though classes aren't built into lua, they can easily be implemented with tables, and there's quite a few class libraries for lua floating around on the internet.
Eamonn wrote:
Code: Select all
require("filename")
require "filename"
I'd like to note those are completely equivalent. (And require isn't special, this is a syntax feature available for every function.)
Apart from that, require doesn't take file names, it takes module names, which are closely related to file names, generally, to go from a file name to a module name you follow these steps:
- Get the filename, for example: a/b/c.lua
- Remove the extension: a/b/c
- Replace slashes with dots: a.b.c
Therefore, to load a file at a/b/c.lua you'd run require("a.b.c").
oh thx
Re: How to use multiple lua files?
Posted: Mon May 20, 2013 9:47 pm
by veethree
What exactly don't you understand?
Re: How to use multiple lua files?
Posted: Mon May 20, 2013 9:47 pm
by cybercircuit
How to call functions and variables from other files
Re: How to use multiple lua files?
Posted: Mon May 20, 2013 9:57 pm
by Larsii30
Let's say you have a directory on your desktop with this in it :
- main.lua
- other.lua
The "other.lua" only contains the variable text = "Hello World".
now to load the "other.lua" you simple use require( "other" ).
This should be done on top of your main.lua or inside the love.load()
now you can access the var "text" from your main.
Code: Select all
require( "other" )
function love.draw()
love.graphics.print( text, 10, 10 )
end
The same for functions. If you got this in your other.lua :
Code: Select all
function doMath( v1, v2 )
return v1 + v2
end
you can call it from your main .. like this :
Code: Select all
require( "other" )
function love.draw()
love.graphics.print( text, 10, 10 ) -- "Hello World"
love.graphics.print( doMath( 1, 1 ), 10, 30 ) -- display "2"
end
Re: How to use multiple lua files?
Posted: Mon May 20, 2013 10:08 pm
by cybercircuit
Thanks a ton! Now I can make a game!
Re: How to use multiple lua files?
Posted: Mon May 20, 2013 11:30 pm
by veethree
Also worth noting if in other.lua you put local in front of the text variable it would no longer be accessible in main.lua. Same goes for functions.
Code: Select all
local function doMath( v1, v2 )
return v1 + v2
end
If a variable or function isn't used outside of the file it's in, It's considered good practice to make it local.