Mig*_*l P 3 c++ string variables lua global
在我的脚本中,我有这个全局变量:
name = "Stabilizer"
Run Code Online (Sandbox Code Playgroud)
而且,我正在尝试在c ++中获取此变量,如下所示:
char* CeScript::GetGlobalString(char* pName)
{
luaL_loadstring(L, m_sScript.c_str());
lua_getglobal(L, pName);
return (char*)lua_tostring(L, -1);
}
....
char* _name = pScript->GetGlobalString("name");
Run Code Online (Sandbox Code Playgroud)
但是,lua_tostring返回一个空ptr,表明无法找到全局变量.
可能是什么问题?谢谢.
loadstring只是将字符串编译成Lua块,但要执行块,你必须调用lua_pcall.在loadtring之后执行此操作,或者如果需要重复pcall,则将其分配给global或ref in注册表.
对于代码示例,请查看关键字"luaL_loadstring lua_pcall"的其他SO帖子,例如 LUA_MULTRET无法按预期工作,并且在设置全局变量时出现c ++ lua错误.