Sam H
Sam H

Reputation: 976

What is a Lua State?

I need to know because I supposedly need to know what it is to make a Lua global using lua_setglobal().

Upvotes: 9

Views: 21069

Answers (2)

Nick Van Brunt
Nick Van Brunt

Reputation: 15484

Brief example which may help...

lua_State* L=lua_open();           // create a Lua state
luaL_openlibs(L);                  // load standard libs 

lua_pushstring(L, "nick");         // push a string on the stack
lua_setglobal(L, "name");          // set the string to the global 'name'

luaL_loadstring(L, "print(name)"); // load a script
lua_pcall(L, 0, 0, 0);             // call the script

Upvotes: 9

Gemini14
Gemini14

Reputation: 6254

You'll want to check out this page in Programming in Lua: A first example To make an analogy, pretend that the C or C++ program is running in a little box and has access to its functions, variables, and so on. The lua_State is basically a way to access what's going on in the Lua "box" during execution of your program and allows you to glue the two languages together.

Upvotes: 12

Related Questions