Advertisement

Lua: creating globals inside a script

Started by August 04, 2004 11:40 AM
7 comments, last by Fragmo 20 years, 3 months ago
I'm pretty new to lua and was wondering how you would create a global variable or function when you run a script using luaL_loadfile. I'm guessing I need to set a table entry for the variable, but how do I obtain the global table within the script? And with the function, the chunk that the file is loaded into is discarded after it's executed isn't it? I need both the variables and functions to be accessible from any other script file I load later. thanks, dave
Each lua_State has its own globals table. Any chunk running in that lua_State has access to the globals table, either explicitly using the _G variable (used if you want to iterate over all global variables, for example) or implicitly (any variable not created as local will default to being an entry in the globals table).

If you load a chunk, and run it, and it adds a function to the globals table (which will happen if you just declare the function normally, without the 'local' specifier), that function will remain there until its value is reset to 'nil' (even if the chunk itself gets destroyed).

Does that answer your question? Or were you looking for something else..?

John B
The best thing about the internet is the way people with no experience or qualifications can pretend to be completely superior to other people who have no experience or qualifications.
Advertisement
Here's what I've tried to do. this script is loaded when my app starts

--script1:

function test(a, b)
return a + b
end

and from a different file I attempted to call the function like so:

--script2:

x = test(10, 5)

when I ran the second script I got an error saying that I tried to call global 'test' a nil value. Did I mess up the syntax? The documentation I read said that the whole chunk is pushed onto the stack as a function, if I declare several functions inside a script will that add all of them to the globals table and if not how would I accomplish that?

thanks,
dave
You must be recreating your lua state somewhere in your program.
Can you write a little sample program for us to look at? What you did there would create a global function called test.
I've set breakpoints in the function that creates the lua_State and it's only called once so I don't think that's a problem. After the luaL_loadfile do I need to do anything like a lua_setglobal to retain the functions that were declared in the file?

thanks,
dave
Nope, call dofile and if there are any function definitions it will register them, if there is any code outside function definitions it will execute it.

So again I'll ask, please post your code to show what you are doing, and maybe we can help. Without seeing exactly what you are doing all we can do is guess.

You should be able to rip out your main function, including whatever lua initializing you do and the lua scripts and post them.
Advertisement
Make sure that you are using the same lua_State var for each lua_dofile. And also check to make sure the spelling of test is correct. Like maybe you defined it as test() and you are calling Test() (capital T). also, make sure that you are not closing lua and opening it again in between dofile's. Try this:


#include
#include
#include
#include

int main()
{
luaState *Script;

Script = lua_open();
luaopen_base();

lua_dofile( Script, "File1.lua" );
lua_dofile( Script, "File2.lua" );

lua_close( Script );
Script = NULL;

return 0;
}

(Untested code, but looks ok)
ok, those 4 includes are suppose to be:

#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

Something weird happened when I tried to post and it showed up as Anonymous Poster. Sorry.
I got it, I called lua_dofile instead of luaL_loadfile and it worked. Thanks guys

upon further review it would've helped if I had called lua_pcall after the loadfile, DOH!

dave

This topic is closed to new replies.

Advertisement