Lua使用lua_type(lua_state,lua_gettop(lua_state)将“ string”返回到C ++,而不是“ double”,这可能是什么问题?

时间:2019-01-22 13:47:46

标签: c++ lua

我试图反复从lua调用该函数,该函数将值返回给C ++。我通常会输入double类型,但有时它会发送与double相同的string值。例如=> 30.5为“ 30.5”。

lua为什么用错误的方式解释它?

我尝试使用luatonumber(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:

0 个答案:

没有答案