Last active
January 2, 2016 13:29
-
-
Save tuttlem/8310746 to your computer and use it in GitHub Desktop.
Lua varaible reader
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 <iostream> | |
#include <lua.hpp> | |
int main(int argc, char *argv[]) { | |
// create a new lua context to work with | |
lua_State *L = luaL_newstate(); | |
// open any library we may use | |
luaL_openlibs(L); | |
// read the Lua script off disk and execute it | |
if ((luaL_dofile(L, "test.lua")) != 0) { | |
// handle any errors | |
std::cout << "unable to load test.lua" << std::endl; | |
return 1; | |
} | |
// put the value of X at the top of the stack | |
lua_getglobal(L, "x"); | |
// interpret the value at the top of the stack | |
// as an integer and put it in the variable "val" | |
int val = (int)lua_tointeger(L, -1); | |
// pop the value of X off the stack | |
lua_pop(L, 1); | |
// write the value out | |
std::cout << "Value of X: " << val << std::endl; | |
// finish up with the Lua context | |
lua_close(L); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment