better 'minetest.get_connected_players'

randomproof
Member
 
Posts: 214
Joined: Thu Nov 17, 2011 06:31

better 'minetest.get_connected_players'

by randomproof » Wed Apr 04, 2012 23:21

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
 

User avatar
sfan5
Member
 
Posts: 3636
Joined: Wed Aug 24, 2011 09:44
GitHub: sfan5
IRC: sfan5

by sfan5 » Thu Apr 05, 2012 05:21

+1
Mods: Mesecons | WorldEdit | Nuke
Minetest builds for Windows (32-bit & 64-bit)
 

jn
Member
 
Posts: 106
Joined: Tue Jan 03, 2012 19:15

by jn » Thu Apr 05, 2012 13:35

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.
 

User avatar
sfan5
Member
 
Posts: 3636
Joined: Wed Aug 24, 2011 09:44
GitHub: sfan5
IRC: sfan5

by sfan5 » Thu Apr 05, 2012 17:23

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
Mods: Mesecons | WorldEdit | Nuke
Minetest builds for Windows (32-bit & 64-bit)
 


Return to Minetest Engine

Who is online

Users browsing this forum: No registered users and 15 guests