Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Lua State?

Tags:

function

lua

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

like image 297
Sam H Avatar asked Nov 17 '10 03:11

Sam H


People also ask

How do you define Lua?

Lua (/ˈluːə/ LOO-ə; from Portuguese: lua [ˈlu. (w)ɐ] meaning moon) is a lightweight, high-level, multi-paradigm programming language designed primarily for embedded use in applications.

What is a Lua Upvalue?

The expression lua_upvalueindex(1) refers to the index of the first upvalue of the function. So, the lua_tonumber in function counter retrieves the current value of the first (and only) upvalue as a number.

What is Userdata Lua?

The type userdata is provided to allow arbitrary C data to be stored in Lua variables. A userdata value is a pointer to a block of raw memory. There are two kinds of userdata: full userdata, where the block of memory is managed by Lua, and light userdata, where the block of memory is managed by the host.

How many functions does Lua have?

This means that all values can be stored in variables, passed as arguments to other functions, and returned as results. There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table.


2 Answers

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.

like image 100
Gemini14 Avatar answered Oct 17 '22 22:10

Gemini14


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
like image 8
Nick Van Brunt Avatar answered Oct 17 '22 21:10

Nick Van Brunt