From bf8cfce50e3f32b84c7fccf470601986d6f7913f Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Sat, 2 Jun 2012 15:47:36 +0300 Subject: [PATCH] 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 --- builtin/misc.lua | 2 +- doc/lua_api.txt | 3 ++- src/scriptapi.cpp | 11 ++++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/builtin/misc.lua b/builtin/misc.lua index b3b1ec6e..e018aff8 100644 --- a/builtin/misc.lua +++ b/builtin/misc.lua @@ -44,7 +44,7 @@ function minetest.get_connected_players() -- This could be optimized a bit, but leave that for later local list = {} 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) end end diff --git a/doc/lua_api.txt b/doc/lua_api.txt index 6de255c0..ddab4726 100644 --- a/doc/lua_api.txt +++ b/doc/lua_api.txt @@ -913,7 +913,8 @@ LuaEntitySAO-only: (no-op for other objects) - get_entity_name() (DEPRECATED: Will be removed in a future version) - get_luaentity() 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_pitch(): pitch in radians - get_look_yaw(): yaw in radians (wraps around pretty randomly as of now) diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 320a45ff..9744aaa3 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -2683,6 +2683,15 @@ private: } /* 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) static int l_get_player_name(lua_State *L) @@ -2690,7 +2699,7 @@ private: ObjectRef *ref = checkobject(L, 1); Player *player = getplayer(ref); if(player == NULL){ - lua_pushnil(L); + lua_pushlstring(L, "", 0); return 1; } // Do it -- 2.30.2