View previous topic :: View next topic |
Author |
Message |
thomas_fogh
Joined: 20 Jul 2007 Posts: 21 Location: Denmark
|
Posted: Thu Oct 04, 2007 5:30 am Post subject: Splitting a script into several files? |
|
|
Hi,
My lua script is getting a bit long and I was wondering if it was possible to split it into several files?
And will my "global" variables still be accessible to all functions?
Code: |
file1.lua:
function func1()
..
function func1_1()
..
file2.lua:
function func2()
..
function func2_1()
..
main.lua:
include file1.lua
include file2.lua
function2_1(...)
function1(...)
...
|
Thanks!
BR, Thomas Fogh[/code] |
|
Back to top |
|
|
chaos^^
Joined: 16 Sep 2007 Posts: 2
|
Posted: Sun Oct 07, 2007 2:28 am Post subject: |
|
|
Hi, I solved this question using the "dofile()" method, like this:
Code: |
# file1.lua # (or whatever extension you like)
function Funct1()
return 2
end
variable = 1
array = {}
# main.lua #
dofile("file1.lua") -- file1.lua must be in the same directory of main.lua
array[1] = variable
array[2] = Funct1()
|
This method is actually running under Lua-Player 0.20. At real, you don't call the file as an header but the effect is the same.
Up to Luaplayer 0.16 you may use the method "require(filename)" that runs like "include" in C/C++ code. Using LuaP 0.20 I didn't find a way to make it running correctly, so I've used the first kind of splitting codes.
Hope you find this usefull, bye!
GiuSeppe _________________ Dopo la forza non c'č nulla di pių alto del suo dominio! |
|
Back to top |
|
|
Anti-QJ
Joined: 03 May 2007 Posts: 16
|
Posted: Sun Oct 07, 2007 4:53 am Post subject: |
|
|
functions.lua
Code: | function movePlayer()
...
end |
index.lua
Code: | dofile("functions.lua"
blah blah
while true do
movePlayer()
...
end |
Thats what I do. I put all my functions in a separate file then call them when needed. |
|
Back to top |
|
|
PiCkDaT
Joined: 04 Oct 2007 Posts: 69
|
Posted: Tue Oct 09, 2007 2:47 am Post subject: |
|
|
chaos^^ wrote: |
Code: |
# main.lua #
dofile("file1.lua") -- file1.lua must be in the same directory of main.lua
|
|
with dofile() the file doesnt have to be in the same directory 'in case' you didnt know, if you use
Code: | dofile("folder/file1.lua") |
or you can even use
Code: | System.currentDirectory("..") -- goes up one directory |
heres a link to all the functions http://wiki.ps2dev.org/psp:lua_player:functions _________________ Enlighten me, Reveal my fate -- Follow - Breaking Benjamin |
|
Back to top |
|
|
|