• Function that does the same thing as Entity() but takes a userid?
    10 replies, posted
Entity() takes an entindex. But, for the gameevent code I'm using, there isn't any entindex for that game event(player_spawn), only the userid of the spawner player. Is there a command similar to Entity() that takes a userid? If not, is there a simple way to create one?
loop through players check if their userid is equal to target userid if so, return the player, if not, continue looping
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/Player]Global.Player[/url]?
[lua] function FindByUserID(id) for k, v in ipairs(player.GetAll()) do if (v:UserID() == id) then return v end end end [/lua] That should work, though I'm not really sure why you'd not just use the PlayerSpawn hook.
[QUOTE=StonedPenguin;47984299][lua] function FindByUserID(id) for k, v in ipairs(player.GetAll()) do if (v:UserID() == id) then return v end end end [/lua] That should work, though I'm not really sure why you'd not just use the PlayerSpawn hook.[/QUOTE] Why ipairs? It isn't faster than pairs. (source [url]http://facepunch.com/showthread.php?t=875909[/url])
[QUOTE=RonanZer0;47984600]Why ipairs? It isn't faster than pairs. (source [url]http://facepunch.com/showthread.php?t=875909[/url])[/QUOTE] I doubt there are going to be more than 100 players on at a time, in which case ipairs is faster according to the source you provided.
[QUOTE=RonanZer0;47984600]Why ipairs? It isn't faster than pairs. (source [url]http://facepunch.com/showthread.php?t=875909[/url])[/QUOTE] ipairs is faster with lua jit
[QUOTE=James xX;47985190]I doubt there are going to be more than 100 players on at a time, in which case ipairs is faster according to the source you provided.[/QUOTE] If you go much further down you can see he says that pairs is actually always faster than ipairs.
I think you should be worrying about functionality before micro optimisations.
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/player/GetByID]player.GetByID[/url]?
Seriously its Player(userid), that easy. [url]http://wiki.garrysmod.com/page/Global/Player[/url]
Sorry, you need to Log In to post a reply to this thread.