Lua error failed logical

It works...

if ( tileType == "water" or 
  ( otherObj and otherObj:GetType() == "IceBlock" )) then
  self:SetNoClip( true )
else
  self:SetNoClip( false )
end

- Is not...

self:SetNoClip( tileType == "water" or 
  ( otherObj and otherObj:GetType() == "IceBlock" ))

//----------------------------------------------------------

local noClip = ( tileType == "water" or 
  ( otherObj and otherObj:GetType == "IceBlock" ))
self:SetNoClip( noClip )

The test otherObjsimply evaluates whether otherObj is nilor not. The specified variables are retrieved in the previous line. The error I get when starting the application:

unprotected error to call in Lua API(script path...: Did not pass boolean to SetNoClip).

SetNoClip is a function in an application that grabs an argument pushed onto the lua stack through lua_toboolean.

So why the first job and the second and third error return?

EDIT:

SetNoClip has 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;
}

, lua_isboolean ( lua_toboolean does) true . , nil, , . , ( ) , , booleans.

+3
3

and , .

or , - , .

, A or (B and C) :

  • A, A - ,
  • B, B - , false, A false
  • C,

, A, B C - , . nil , , , , SetNoClip, nil true false.

nil, :

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

~= .

+1

, Lua. - LuaSetNoClip Lua, , . lua_toboolean .

, , , luaL_checkany:

int GameObject::LuaSetNoClip( lua_State *L ) {
  luaL_checkany(L, 1);
  m_noClip = lua_toboolean( L, 1 );
  return 0;
}
+1

, SetNoClip (.. ). , , , and or , true false. ,

foo and bar

will return fooif fooeither nilor falseotherwise, it will return bar.

In your case, the source code is passed trueeither falseto SetNoClip, while in other examples you either pass "water"or the boolean to SetNoClip. SetNoClip may choke on a string.

0
source

Source: https://habr.com/ru/post/1754122/


All Articles