Skip to content

Commit 2170c99

Browse files
fairiestoykahrl
fairiestoy
authored andcommittedOct 1, 2013
Optimized minetest.get_connected_players()
Instead of collecting all objects within a huge radius (which could be a big value), just register each player that connects and give back the current hold list.
1 parent 2134316 commit 2170c99

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed
 

‎builtin/misc.lua

+14-7
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,22 @@ function minetest.check_player_privs(name, privs)
4040
return true, ""
4141
end
4242

43+
local player_list = {}
44+
45+
minetest.register_on_joinplayer(function(player)
46+
player_list[player:get_player_name()] = player
47+
end)
48+
49+
minetest.register_on_leaveplayer(function(player)
50+
player_list[player:get_player_name()] = nil
51+
end)
52+
4353
function minetest.get_connected_players()
44-
-- This could be optimized a bit, but leave that for later
45-
local list = {}
46-
for _, obj in pairs(minetest.get_objects_inside_radius({x=0,y=0,z=0}, 1000000)) do
47-
if obj:is_player() then
48-
table.insert(list, obj)
49-
end
54+
local temp_table = {}
55+
for index, value in pairs(player_list) do
56+
table.insert(temp_table, value)
5057
end
51-
return list
58+
return temp_table
5259
end
5360

5461
function minetest.hash_node_position(pos)

0 commit comments

Comments
 (0)
Please sign in to comment.