Search code examples
capiluaboolean-logicluac

Lua error did not pass boolean


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.


Solution

  • The and operator returns its first argument if that value is something considered non-true, and its second argument otherwise.

    The or operator returns its first argument if it is something considered true, and its second argument otherwise.

    Thus, A or (B and C) can theoretically return any of the following:

    • A if A is a value considered true
    • B if B is a value considered false and A is considered false
    • C if neither of the above is the case

    Note that A, B, and C are not required to be actual boolean values - only things that can be interpreted as booleans. Since nil is considered a false value, it's possible that the second case is occurring for you, and the argument being passed to SetNoClip is nil instead of true or false.

    One option to fix this would be to explicitly compare with nil, instead of just using the object:

    ( otherObj ~= nil and otherObj:GetType() == "IceBlock" )
    

    since the ~= operator is guaranteed to return a boolean.