I'm using Lua as scripting language in my (C++) game. In one call (from lua to c++) I check what type is on the top of the stack:
if(lua_type(L, (0)) == LUA_TSTRING)
But sometimes lua_type(L, (0)) returns 9.
I can't seem to find any references to this (return values should be between -1 and 8 or LUA_TNONE, LUA_TNIL, ... LUA_TTHREAD).
What's happening?
So, the complete round trip is: Start the lua interpreter Register the C functions you want lua to use Call a lua function from within C, either by a call to luaL_dofile, or by calling a lua function directly The lua function that you called from C can access the C function
Lua - Data Types. Lua is a dynamically typed language, so the variables don't have types, only the values have types. Values can be stored in variables, passed as parameters and returned as results.
Values can be stored in variables, passed as parameters and returned as results. In Lua, though we don't have variable data types, but we have types for the values. The list of data types for values are given below.
C/C++ functions that are called from lua need to take as an input parameter the lua state. The parameters can be examined on the call stack using the functions lua_tointeger, lua_tonumber, etc (described above). The first parameter is at index 1, the second parameter is at index 2, and so on.
The top of the stack is at index -1, not 0.
0 can never be used as an index for accessing the stack:
(Note that 0 is never an acceptable index.)
in §4.3 – Valid and Acceptable Indices in the reference manual.
The C API in Lua does not hold the programmer's hand:
As in most C libraries, the Lua API functions do not check their arguments for validity or consistency. However, you can change this behavior by compiling Lua with the macro LUA_USE_APICHECK defined. [§4]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With