这个功能可以正常运作...
if ( tileType == "water" or
( otherObj and otherObj:GetType() == "IceBlock" )) then
self:SetNoClip( true )
else
self:SetNoClip( false )
end
- 这些不会影响您的网站
self:SetNoClip( tileType == "water" or
( otherObj and otherObj:GetType() == "IceBlock" ))
//----------------------------------------------------------
local noClip = ( tileType == "water" or
( otherObj and otherObj:GetType == "IceBlock" ))
self:SetNoClip( noClip )
otherObj
测试只是判断otherObj
是否为nil
。给定的变量是在之前的一行中检索的。当应用程序运行时,我收到以下错误:
unprotected error to call in Lua API(script path...: Did not pass boolean to SetNoClip).
SetNoClip
是应用程序中的一个函数,通过lua_toboolean
从lua堆栈中获取推送的参数。
那么为什么第一个可以正常工作,而第二个和第三个会返回错误?
编辑:
SetNoClip
曾经有这个定义。
int GameObject::LuaSetNoClip( lua_State *L ) {
if ( !lua_isboolean( L, -1 )) {
LogLuaErr( "Did not pass boolean to SetNoClip for GameObject: " + m_type );
return luaL_error( L, "Did not pass boolean to SetNoClip" );
}
m_noClip = lua_toboolean( L, -1 );
return 0;
}
问题在于
lua_isboolean
不进行任何隐式类型转换(但lua_toboolean
会),并且仅对字面布尔值返回true。因此,如果它看到nil,它将返回未传递布尔值的错误。我刚刚删除了对布尔字面量的错误检查,因为人们(包括我)通常依赖于非布尔字面量参数被正确地视为布尔值。
true
或false
进行比较。我会对这个API感到恼火... - Cogwheel