This works...
if ( tileType == "water" or
( otherObj and otherObj:GetType() == "IceBlock" )) then
self:SetNoClip( true )
else
self:SetNoClip( false )
end
- These don't...
self:SetNoClip( tileType == "water" or
( otherObj and otherObj:GetType() == "IceBlock" ))
//----------------------------------------------------------
local noClip = ( tileType == "water" or
( otherObj and otherObj:GetType == "IceBlock" ))
self:SetNoClip( noClip )
The otherObj
test just evaluates to whether otherObj is nil
or not. The variables given are retrieved in a previous line. The error I get when the application runs is:
unprotected error to call in Lua API(script path...: Did not pass boolean to SetNoClip).
SetNoClip is a function in the application that grabs the argument pushed onto the lua stack via lua_toboolean
.
So why does the first work and the second and third return errors?
EDIT:
SetNoClip had this definition.
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;
}
The problem is that lua_isboolean
doesn't do any implicit type conversion (but lua_toboolean
does) and will only return true for literal boolean values. So if it sees nil, it will return that a boolean wasn't passed. I just removed the error check for a boolean literal, since people (including me) commonly rely on arguments that aren't boolean literals being treated correctly as booleans.