Lua调用C++的变量

#include <iostream>
#include "lua.hpp"
int main(void)
{
lua_State *L = luaL_newstate();
luaL_loadfile(L,"2.lua");
luaL_openlibs(L);
lua_pcall(L,0,0,0);


int iNum = 100;
char szStr[] = "World";
lua_pushnumber(L,iNum);
lua_setglobal(L,"g_Num");
lua_pushstring(L,szStr);
lua_setglobal(L,"g_Str");
lua_pushnumber(L,iNum);
lua_setglobal(L,"cpp_Num");
lua_pushstring(L,szStr);
lua_setglobal(L,"cpp_Str");
lua_getglobal(L,"TestFun");
lua_pcall(L,0,0,-1);
 
lua_close(L);
getchar();
return 0;

}



/*************************************2.lua*************************************/

g_Num = 0;
g_Str = "Hello";
function TestFun()
print(g_Num);
print(g_Str);
print(cpp_Num);
print(cpp_Str);
end



你可能感兴趣的:(Lua调用C++的变量)