File includes in Lua

Started by
6 comments, last by fyhuang 19 years, 11 months ago
Is it possible to include files in Lua? The documentation doens''t seem to mention anything about this.
- fyhuang [ site ]
Advertisement
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
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?
- fyhuang [ site ]
there is a builtin function called ''require''
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 functionendfunction bar() -- this line sets the global variable "bar" to be equal to the function body given   print("bar")endfoo() -- 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
What if I wanted to create a function in one file and use it in another? Would I do this:

-- FILE 1function foo()    -- Stuff hereend-- FILE 2two = foo();two();


How would the Lua interpreter know where the function declaration is? I know that I''m thinking in ''C-style'', but...
- fyhuang [ site ]
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 #includedofile "someInclude.lua"-- similar to #defineSOME_DEFINE_SET = true-- similar to #ifdefif SOME_DEFINE_SET then    function foo()        print(123)    endelse    function foo()        print(456)    endend

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
Thanks, your final explanation made it all clear to me!
- fyhuang [ site ]

This topic is closed to new replies.

Advertisement