我试图反复从lua
调用该函数,该函数将值返回给C ++。我通常会输入double
类型,但有时它会发送与double
相同的string
值。例如=> 30.5为“ 30.5”。
lua
为什么用错误的方式解释它?
我尝试使用lua
从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:
。