Splitting a script into several files?

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
thomas_fogh
Posts: 21
Joined: Fri Jul 20, 2007 10:22 pm
Location: Denmark

Splitting a script into several files?

Post by thomas_fogh »

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: Select all

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]
chaos^^
Posts: 2
Joined: Sun Sep 16, 2007 6:34 am

Post by chaos^^ »

Hi, I solved this question using the "dofile()" method, like this:

Code: Select all

# 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!
Anti-QJ
Posts: 16
Joined: Thu May 03, 2007 2:34 pm

Post by Anti-QJ »

functions.lua

Code: Select all

function movePlayer()
...
end
index.lua

Code: Select all

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.
PiCkDaT
Posts: 69
Joined: Thu Oct 04, 2007 9:49 pm

Post by PiCkDaT »

chaos^^ wrote:

Code: Select all

# 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: Select all

dofile("folder/file1.lua")
or you can even use

Code: Select all

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