Created
November 26, 2015 19:03
-
-
Save kpmiller/dbe38da0ae43b65cf94a to your computer and use it in GitHub Desktop.
This is a simple example of calling a lua function that returns a table and retrieving the values out of the table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <assert.h> | |
#include "lua.h" | |
#include "lualib.h" | |
#include "lauxlib.h" | |
char *luacode = | |
"function f ( ) \n" | |
" print(\"hello\\n\") \n" | |
" return { a=1.0, b=3.0 } \n" | |
"end \n" | |
; | |
void CheckLuaErr(lua_State *L, int err, int line) | |
{ | |
if (err != 0) | |
{ | |
printf("line %d: lua error %d\n", line, err); | |
printf("%s\n", lua_tostring(L, -1)); | |
lua_pop(L, 1); // pop error message from the stack | |
} | |
} | |
int main(int argc, char *argv[]) | |
{ | |
lua_State *L = luaL_newstate(); | |
luaL_openlibs(L); | |
int err = luaL_loadbuffer(L, luacode, strlen(luacode), "testscript"); | |
CheckLuaErr(L, err, __LINE__); | |
//Have to run the script once so the global variables (including the function | |
// names) are loaded | |
//ref: http://www.troubleshooters.com/codecorn/lua/lua_c_calls_lua.htm | |
err = lua_pcall(L, 0, 0, 0); | |
CheckLuaErr(L, err, __LINE__); | |
lua_getglobal(L, "f"); //Call the function f(), expecting 1 table returned | |
err = lua_pcall(L, 0, 1, 0); | |
CheckLuaErr(L, err, __LINE__); | |
assert ( lua_istable(L, -1) ); | |
//-1 is now a reference to the table | |
//Use gettable | |
lua_pushstring(L, "a"); | |
lua_gettable(L, -2); | |
float a = lua_tonumber(L, -1); | |
lua_pop(L, 1); | |
//Or getfield | |
lua_getfield(L, -1, "b"); | |
float b = lua_tonumber(L, -1); | |
lua_pop(L, 1); // pop table from the stack | |
printf("a = %f, b= %f\n",a,b); | |
lua_close (L); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment