How to use multiple 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.
Post Reply
cybercircuit
Prole
Posts: 16
Joined: Mon May 20, 2013 7:39 pm

How to use multiple lua files?

Post 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?
User avatar
Eamonn
Party member
Posts: 550
Joined: Sat May 04, 2013 1:29 pm
Location: Ireland

Re: How to use multiple lua files?

Post by Eamonn »

As far as I'm aware, classes do not exist in Lua. There are, however, objects(or table :P ). 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

Code: Select all

require("filename")
or

Code: Select all

require "filename"
Hope this helps!
"In those quiet moments, you come into my mind" - Liam Reilly
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: How to use multiple lua files?

Post 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:
  1. Get the filename, for example: a/b/c.lua
  2. Remove the extension: a/b/c
  3. Replace slashes with dots: a.b.c
Therefore, to load a file at a/b/c.lua you'd run require("a.b.c").
cybercircuit
Prole
Posts: 16
Joined: Mon May 20, 2013 7:39 pm

Re: How to use multiple lua files?

Post by cybercircuit »

Thanks but that dident explain to me exactly how to use multiple lua files
cybercircuit
Prole
Posts: 16
Joined: Mon May 20, 2013 7:39 pm

Re: How to use multiple lua files?

Post 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:
  1. Get the filename, for example: a/b/c.lua
  2. Remove the extension: a/b/c
  3. 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
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: How to use multiple lua files?

Post by veethree »

What exactly don't you understand?
cybercircuit
Prole
Posts: 16
Joined: Mon May 20, 2013 7:39 pm

Re: How to use multiple lua files?

Post by cybercircuit »

How to call functions and variables from other files
User avatar
Larsii30
Party member
Posts: 267
Joined: Sun Sep 11, 2011 9:36 am
Location: Germany

Re: How to use multiple lua files?

Post 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
cybercircuit
Prole
Posts: 16
Joined: Mon May 20, 2013 7:39 pm

Re: How to use multiple lua files?

Post by cybercircuit »

Thanks a ton! Now I can make a game!
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: How to use multiple lua files?

Post 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 text = "Hello World".

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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 6 guests