How to use multiple 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: 16
- Joined: Mon May 20, 2013 7:39 pm
How to use multiple lua files?
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?
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?
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:
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!
Code: Select all
love.graphics.print("Example.", 50, 50)
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
Code: Select all
require("filename")
Code: Select all
require "filename"
"In those quiet moments, you come into my mind" - Liam Reilly
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: How to use multiple lua files?
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.
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:
I'd like to note those are completely equivalent. (And require isn't special, this is a syntax feature available for every function.)Eamonn wrote:Code: Select all
require("filename") require "filename"
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
-
- Prole
- Posts: 16
- Joined: Mon May 20, 2013 7:39 pm
Re: How to use multiple lua files?
Thanks but that dident explain to me exactly how to use multiple lua files
-
- Prole
- Posts: 16
- Joined: Mon May 20, 2013 7:39 pm
Re: How to use multiple lua files?
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.
I'd like to note those are completely equivalent. (And require isn't special, this is a syntax feature available for every function.)Eamonn wrote:Code: Select all
require("filename") require "filename"
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:Therefore, to load a file at a/b/c.lua you'd run require("a.b.c").
- Get the filename, for example: a/b/c.lua
- Remove the extension: a/b/c
- Replace slashes with dots: a.b.c
oh thx
-
- Prole
- Posts: 16
- Joined: Mon May 20, 2013 7:39 pm
Re: How to use multiple lua files?
How to call functions and variables from other files
Re: How to use multiple lua files?
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.
The same for functions. If you got this in your other.lua :
you can call it from your main .. like this :
- 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
Code: Select all
function doMath( v1, v2 )
return v1 + v2
end
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
-
- Prole
- Posts: 16
- Joined: Mon May 20, 2013 7:39 pm
Re: How to use multiple lua files?
Thanks a ton! Now I can make a game!
Re: How to use multiple lua files?
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.
If a variable or function isn't used outside of the file it's in, It's considered good practice to make it local.
Code: Select all
local text = "Hello World".
Code: Select all
local function doMath( v1, v2 )
return v1 + v2
end
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 10 guests