Page 1 of 2
How to connect the 2 lua files?
Posted: Sun Jan 13, 2013 7:10 pm
by plz_m0ve
Hi,
Is there a command for how to 'blend' 2 lua files together? I have a main.lua with some stuff and the player code. But how do i like 'connect' them?
I tried with this comment dofile "player.lua", but i just get an error : "Can't find player.lua"... But it is in the same folder?
Someone please help! ;S
Re: How to connect the 2 lua files?
Posted: Sun Jan 13, 2013 8:51 pm
by master both
Well... you can use the
require funtion.
it loads that lua file into you main file.
example:
require "player"
It load the player.lua file that should be in the same folder as main.lua.
also its recommended to write it at the start of the main.lua file.
i hope this help you, also sorry for my bad english
Re: How to connect the 2 lua files?
Posted: Mon Jan 14, 2013 10:20 am
by Kjell Granlund
With the require function it can be in a different folder too. I do this alot in my code like this: require("playercode/player") where playercode is the folder and player is the player.lua that has your code in it. Put the require in your main in the load fuction: load() require("playercode/player") end. I hope this helps. Sorry for not using the insert code. My phone is not allowing me to do that. Good luck
Re: How to connect the 2 lua files?
Posted: Mon Jan 14, 2013 10:52 am
by bartbes
I should note that the proper directory separator for require is a ., not /.
Re: How to connect the 2 lua files?
Posted: Mon Jan 14, 2013 11:50 am
by Kjell Granlund
Ah thanks for that bit bartbes. Now when you say proper does that mean "/" still works? I only ask bc I have been using that. And why "." Is there a certain reason for this? Im still very new and want to understand the logic
Re: How to connect the 2 lua files?
Posted: Mon Jan 14, 2013 12:49 pm
by kikito
Kjell Granlund wrote:Ah thanks for that bit bartbes. Now when you say proper does that mean "/" still works? I only ask bc I have been using that. And why "." Is there a certain reason for this? Im still very new and want to understand the logic
It works, but by coincidence. It's not guaranteed to work in the future, so you might as well change to "." today.
Re: How to connect the 2 lua files?
Posted: Mon Jan 14, 2013 1:53 pm
by Boolsheet
I would call it implementation specific behaviour. It works because Lua passes the unmodified string to the loader which will use a platform specific function. All modern operating systems recognize forward slashes as directory separators. Still, there's no rule that requires them to do so and Lua, being ultra portable, has to account for that. Windows officially uses backslashes and I think Mac OS used to have colons. The syntax with the period creates an interface that should work the same over all platforms. Behind the scene, it just replaces the period with the appropriate directory separator.
There's even more to it once you start loading C modules.
Re: How to connect the 2 lua files?
Posted: Mon Jan 14, 2013 3:33 pm
by plz_m0ve
Now im a little confused.
So i write this
Code: Select all
function love.load(require player.lua)
Got a little confused with all the different advanced talk you guys did there.
But thanks for the answers :-)
Re: How to connect the 2 lua files?
Posted: Mon Jan 14, 2013 6:00 pm
by Kjell Granlund
Do not add the extension. Just "player" not "play.lua"
Re: How to connect the 2 lua files?
Posted: Mon Jan 14, 2013 6:45 pm
by plz_m0ve
Ok, thanks for the answer :-)