|
Post by sixtogus on Feb 9, 2006 8:53:11 GMT -5
Hello guys
I have one question about lunar programing. I have been trying to call a file(A) within another file to use X functions from A on X. Kind like when you use #Include<FileName>. on C++. How do i do that on lunar or Lua.
Thank you very much for your help guys.
|
|
|
Post by X-Drop on Feb 9, 2006 9:22:27 GMT -5
Lookup loadstring and loadfile.
|
|
|
Post by sixtogus on Feb 9, 2006 11:39:15 GMT -5
so i will just type: loadFile(x.lua) And i will be able to call any funtion inside that file ? can you give some Example code?
Thank you a lot for the help.
|
|
|
Post by X-Drop on Feb 9, 2006 12:08:54 GMT -5
lua-users wikiYou can call the whole file as a function using loadfile or you can call a specific chunk of a file as a function using loadstring.
|
|
sdphantom
Full Member
Savior and Destroyer
Posts: 230
|
Post by sdphantom on Feb 9, 2006 13:32:09 GMT -5
dofile() Includes and runs a Lua script file.
loadfile() Load a file as a Lua function.
loadstring() Converts a string into a Lua function.
dofile() works in the exact same way as #include from C/C++ Example:dofile("luascript.lua")
|
|
|
Post by sixtogus on Feb 13, 2006 9:19:58 GMT -5
Hello me again. i still have a problem i dosent work. this is how my code is. Example:fileAdofile("fileB.lua") testFunc()Example:fileBfunction testFunc() -- code here--
endit gives me a global thing error. this what i want to do i want to make the Lunar fuctions shorter and be able to call them with a simpler function from any other scripts I make. in a simpley way
|
|
|
Post by X-Drop on Feb 13, 2006 12:34:55 GMT -5
Hey I see what you wanna do and I want to redirect you to loadstring and loadfile since you are looking to reuse the function many times.
For instance, if you use loadfile you will be reading the entire file as a Lua chunk and it will return the chunk as a function. This will work fine if you want the whole file to act like a function.
An example,
my_function = loadfile("fileA.lua")
I think you need to define the new function before you can run it,
my_function()
Now you may call the new function anytime in your script and even pass your arguments here,
my_function(arg1, arg2, arg3, ...)
Loadstring is similar, but you need to parse the file for the chunk you wish to use as a function. This is very handy too.
|
|
sdphantom
Full Member
Savior and Destroyer
Posts: 230
|
Post by sdphantom on Feb 14, 2006 2:11:02 GMT -5
Hello me again. i still have a problem i dosent work. this is how my code is. Example:fileAdofile("fileB.lua") testFunc()Example:fileBfunction testFunc() -- code here--
endit gives me a global thing error. this what i want to do i want to make the Lunar fuctions shorter and be able to call them with a simpler function from any other scripts I make. in a simpley way Is the error pointing to the dofile() function or the one you made in the file? There's a problem with all Lua core functions. When you specify a file path, it looks for the file within the FFXI directory, not in the script directory. This affects dofile(), loadfile(), and io.open(). To correct this, define a function in the main script: function getPath() local filePath=string.gsub(debug.getinfo(1).source, "@", "", 1) return string.sub(filePath,1,string.len(filePath)-string.len(Windower.script_GetArg(1))) end
Then call the function to return the directory as part of the argurment to dofile(): dofile(getPath().."fileB.lua")
|
|
|
Post by sixtogus on Feb 15, 2006 8:26:47 GMT -5
Thanks a lot X-Drop and SDphantom finaly i got it to work whit the last post.
function getPath() local filePath=string.gsub(debug.getinfo(1).source, "@", "", 1) return string.sub(filePath,1,string.len(filePath)-string.len(Windower.script_GetArg(1))) end
now im gonna be working in a file to minimize the code we have to use whit Lunar this may help to some nobs on this like me. ;D i will post the code later.
Thanks a lot guys.
|
|