forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Splitting a script into several files?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development
View previous topic :: View next topic  
Author Message
thomas_fogh



Joined: 20 Jul 2007
Posts: 21
Location: Denmark

PostPosted: Thu Oct 04, 2007 5:30 am    Post subject: Splitting a script into several files? Reply with quote

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
View user's profile Send private message
chaos^^



Joined: 16 Sep 2007
Posts: 2

PostPosted: Sun Oct 07, 2007 2:28 am    Post subject: Reply with quote

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
View user's profile Send private message MSN Messenger
Anti-QJ



Joined: 03 May 2007
Posts: 16

PostPosted: Sun Oct 07, 2007 4:53 am    Post subject: Reply with quote

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
View user's profile Send private message
PiCkDaT



Joined: 04 Oct 2007
Posts: 69

PostPosted: Tue Oct 09, 2007 2:47 am    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Lua Player Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group