Page 1 of 1

better 'minetest.get_connected_players'

PostPosted: Wed Apr 04, 2012 23:21
by randomproof
This should work better than using 'minetest.env:get_objects_inside_radius' though you could have cycled through 'minetest.object_refs' if you wanted all the objects.
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
local connected_players = {}

minetest.register_on_joinplayer(function(obj)
    connected_players[obj:get_player_name()] = obj
end)

minetest.register_on_leaveplayer(function(obj)
    connected_players[obj:get_player_name()] = nil
end)

function minetest.get_connected_players()
    return connected_players
end

PostPosted: Thu Apr 05, 2012 05:21
by sfan5
+1

PostPosted: Thu Apr 05, 2012 13:35
by jn
The api doc says it returns a "list of connected players", does that imply the keys have to be consecutive numbers? Are there mods that iterate the returned table with ipairs? I agree that having the player names as keys is quite nice, but it could break mods that assume a simple array.

PostPosted: Thu Apr 05, 2012 17:23
by sfan5
jn wrote:The api doc says it returns a "list of connected players", does that imply the keys have to be consecutive numbers? Are there mods that iterate the returned table with ipairs? I agree that having the player names as keys is quite nice, but it could break mods that assume a simple array.

If you want to cycle thorugh it use ipairs