lua代码
a = 0
str = "hello world"
function SetA(param)
a = param
end
function SetStr()
str = "hello wolrd ni hao"
end
c++使用前需要包含头文件
#include <lua/lua.hpp>
#include <luabind/luabind.hpp>
#include <luabind/raw_policy.hpp>
lua_State* m_L = lua_open();
luaL_openlibs(m_L);
luabind::open(m_L);
try
{
//获取lua中的全局变量a
luabind::object luaRet = luabind::globals(m_L)["a"];
int a = luabind::object_cast<int>(luaRet);
//修改lua中的全局变量a的值为10
luabind::globals(m_L)["a"] = 10;
//获取全局变量str
luaRet = luabind::globals(m_L)["str"];
std::string b= luabind::object_cast<std::string>(str);
//修改lua中的全局变量str的值为”ni hao"
luabind::globals(m_L)["str"] = "ni hao";
//调用lua中的全局函数SetA()
luabind::call_function<int>(m_L,"SetA",10);
//调用lua中的全局函数SetStr()
luabind::call_function<void>(m_L,"SetStr");
}
catch (std::exception& e)
{
theLog.error("Lua runtime error: %s", e.what());
}