c++ - Lua 使用 lua_type(lua_state, lua_gettop(lua_state) 将“string”返回给 C++ 而不是“double”,这可能是什么问题?
问题描述
我正在尝试lua
重复调用该函数,该函数将值返回给 C++。double
我大部分时间都得到 type ,但有时它发送的double
值与string
. 例如=> 30.5 为“30.5”。
为什么lua
解释它是错误的方式?
我试图从lua
using中返回值tonumber(return_value)
,以强制它编号,但string
有时它仍然给我。
lua
侧面:
function getValue()
mValue = 30+ 0.5
return mValue
end
function init()
print(" [lua] Calling getValue")
ret1 = getValue()
return ret1 -- tried tonumber(ret1) also
end
对于 C++ 端:
while (lua_gettop(lua_state) >= 1)
{
str_buf.str(std::string());
str_buf << " ";
switch (lua_type(lua_state, lua_gettop(lua_state)))
{
case LUA_TNUMBER:
{
str_buf << "script returned the number: "
<< lua_tonumber(lua_state, lua_gettop(lua_state));
}
break;
case LUA_TTABLE:
str_buf << "script returned a table";
break;
case LUA_TSTRING:
{
std::string temp = lua_tostring(lua_state, lua_gettop(lua_state));
str_buf << "script returned the string: "
<< temp;
}
break;
case LUA_TBOOLEAN:
{
str_buf << "script returned the boolean: "
<< lua_toboolean(lua_state, lua_gettop(lua_state));
}
break;
default:
str_buf << "script returned an unknown-type value";
}
我一直希望在 C++ 中获得 30.5 并且case LUA_TNUMBER:
应该每次都执行,但LUA_TSTRING:
有时会发生这种情况。
解决方案
推荐阅读
- python - VSCode:keybindings.json 不会生效
- javascript - 使用 javascript 动态设置网格列和行
- python - 为您的课程添加选项的最佳方式是什么?
- python - 在python中使用迭代+堆栈展平数组
- python - 不清楚我的模型的整体结构
- r - 使用日出和日落时间过滤时间序列数据
- javascript - Cypress 中的固件和支持文件有什么区别?哪个更好用?
- java - 设置路径后找不到Javac
- python - 是否通过 multiprocessing.Queue 传递 asyncio.Future 并返回线程安全?
- unity3d - Unity 导入包没有导入正确的版本