Add ObjRef:is_player() and modify ObjRef:get_player_name() to always return a string to aid better inter-object compatibility of code that assumes objects to be players

This commit is contained in:
Perttu Ahola 2012-06-02 15:47:36 +03:00
parent b235e4d290
commit bf8cfce50e
3 changed files with 13 additions and 3 deletions

@ -44,7 +44,7 @@ function minetest.get_connected_players()
-- This could be optimized a bit, but leave that for later -- This could be optimized a bit, but leave that for later
local list = {} local list = {}
for _, obj in pairs(minetest.env:get_objects_inside_radius({x=0,y=0,z=0}, 1000000)) do for _, obj in pairs(minetest.env:get_objects_inside_radius({x=0,y=0,z=0}, 1000000)) do
if obj:get_player_name() then if obj:is_player() then
table.insert(list, obj) table.insert(list, obj)
end end
end end

@ -913,7 +913,8 @@ LuaEntitySAO-only: (no-op for other objects)
- get_entity_name() (DEPRECATED: Will be removed in a future version) - get_entity_name() (DEPRECATED: Will be removed in a future version)
- get_luaentity() - get_luaentity()
Player-only: (no-op for other objects) Player-only: (no-op for other objects)
- get_player_name(): will return nil if is not a player - is_player(): true for players, false for others
- get_player_name(): returns "" if is not a player
- get_look_dir(): get camera direction as a unit vector - get_look_dir(): get camera direction as a unit vector
- get_look_pitch(): pitch in radians - get_look_pitch(): pitch in radians
- get_look_yaw(): yaw in radians (wraps around pretty randomly as of now) - get_look_yaw(): yaw in radians (wraps around pretty randomly as of now)

@ -2684,13 +2684,22 @@ private:
/* Player-only */ /* Player-only */
// is_player(self)
static int l_is_player(lua_State *L)
{
ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref);
lua_pushboolean(L, (player != NULL));
return 1;
}
// get_player_name(self) // get_player_name(self)
static int l_get_player_name(lua_State *L) static int l_get_player_name(lua_State *L)
{ {
ObjectRef *ref = checkobject(L, 1); ObjectRef *ref = checkobject(L, 1);
Player *player = getplayer(ref); Player *player = getplayer(ref);
if(player == NULL){ if(player == NULL){
lua_pushnil(L); lua_pushlstring(L, "", 0);
return 1; return 1;
} }
// Do it // Do it