What is a Lua State?

15,334

Solution 1

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

Solution 2

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.

Share:
15,334
Sam H
Author by

Sam H

Thanks to the Stack Overflow community: https://sourceforge.net/projects/ilua/

Updated on June 23, 2022

Comments

  • Sam H
    Sam H almost 2 years

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

  • Stefan Falk
    Stefan Falk over 7 years
    Can you tell, why there might be multiple lua_State objects in a program? I am currently trying to hook into the Lua initialization process and I now discovered that there are at least 3 different lua_State objects. What I don't understand - and this has to do with my question - is why L->l_G->_defaultmeta.value.gc is always NULL. All three structs get by and none of them have a value set at that time. But this can't be, because the original Lua code looks like
  • Stefan Falk
    Stefan Falk over 7 years
    as if they should have set a value at this stage of the initialization process. Any help would be great!
  • Stefan Falk
    Stefan Falk over 7 years
    Do you know why there might be multiple lua_State objects in a program? I need help with this question here. As I stated below, I think that there should be a value set for L->l_G->_defaultmeta.value.gc at the time I steal the pointer to the lua_State struct, but for some reason non of the stolen structs does have it set.