Archived

This topic is now archived and is closed to further replies.

fyhuang

File includes in Lua

Recommended Posts

Is it possible to include files in Lua? The documentation doens''t seem to mention anything about this.

Share this post


Link to post
Share on other sites
Since "script files" in Lua are simply chunks of code that are executed, rather than a series of declarations and definitions, including a "script" is simply a matter of executing it. The Lua function dofile will happily do this for you.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
I meant ''preprocessor'' includes. If there are no ''declarations'', then does that mean that if I create a function in a seperate file and use it in another, then I compile them together, then Lua won''t complain and it will work?

Share this post


Link to post
Share on other sites
quote:
Original post by fyhuang
I meant ''preprocessor'' includes. If there are no ''declarations'', then does that mean that if I create a function in a seperate file and use it in another, then I compile them together, then Lua won''t complain and it will work?

As long as the line that sets the function is executed before the line using it. Here, watch:


function foo() -- this line sets the global variable "foo" to be equal to the function body given
bar() -- when the function is executed, this line looks up the global variable "bar" and executes it as a function
end

function bar() -- this line sets the global variable "bar" to be equal to the function body given
print("bar")
end

foo() -- this line loos up the global variable "bar" and executes it as a function

As I said before, it''s all just executing code. You "define" functions by setting global variables to your bodies. As long as you set a global before you access it, there''s no problem.




"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
What if I wanted to create a function in one file and use it in another? Would I do this:


-- FILE 1

function foo()
-- Stuff here
end


-- FILE 2

two = foo();

two();




How would the Lua interpreter know where the function declaration is? I know that I''m thinking in ''C-style'', but...

Share this post


Link to post
Share on other sites
quote:
Original post by fyhuang
What if I wanted to create a function in one file and use it in another?

What you have to understand is what a "file" is: a function. "loadfile" loads and compiles the script file as a function. "dofile" does loadfile, then executes the resultant function.

A line like "function foo() print(123) end" is just semantic sugar for "foo = function() print(123) end", which in turn is semantic sugar for "globalsTable.foo = function() print(123) end". See how it works? Declaring a function is an _action_. Because of this, although various people have used preprocessors on Lua in the past, you really tend to not need it. You can, for instance, do something like this:


-- similar to #include
dofile "someInclude.lua"

-- similar to #define
SOME_DEFINE_SET = true

-- similar to #ifdef
if SOME_DEFINE_SET then
function foo()
print(123)
end
else
function foo()
print(456)
end
end

To ultimately answer your question, you can use a function in a file other than that in which it was declared, as long as that function is still sitting around in the globals table. And since the globals table is not cleared after a script file (a function), as long as you''ve already executed the file that puts that global variable in the globals table, it''ll still be there for the other script to use it.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites