Jump to content
Official BF Editor Forums
Sign in to follow this  
PiratePlunder

Pick A Random Player From A Certain Team

Recommended Posts

I couldn't find a simple way of doing it, compared to just picking a random player irrespective of team, this seems much harder.

Thought that this would do it, basically create an empty array, go through all players and add the index number of any players that are alive and in the correct team to the array. Then just pull a random index out of that array and grab the corresponding player. The random bits work as i've copied them from another part of our gamemode, i also know through debugging that its working through to the if index >0 and its entering there, so the player indexes are being added to the array (although i've not tested with more than 1 player). So im thinking its falling over when pulling the playerbyindex, maybe because of the array?

                               peglegPlayerList = {}
                               index = 0
                               #go through all alive players and grab pegleg players
                               for p in bf2.playerManager.getPlayers():
                                       if p.getTeam() == 1: #we only want players from team1
                                               if p.isAlive() and not p.isManDown():
                                                       peglegPlayerList[index] = p.index
                                                       index += 1
                               #pick a random player from the array
                               if index > 0:
                                       peglegPlayerIndex = random.randint(-1, index-1)
                                       peglegPlayer = bf2.playerManager.getPlayerByIndex(peglegPlayerList[peglegPlayerIndex])
                                       prnt("Player Picked" + str(peglegPlayer.getName()) )

Share this post


Link to post
Share on other sites

one line solution (expand to make more readable at your leisure ;) )

randomplayer = random.choice([p for p in bf2.playerManager.getPlayers() if p.getTeam() == 1 and p.isAlive() and not p.isManDown()])

whoops, fixed

Edited by [FH]ctz

Share this post


Link to post
Share on other sites

Are you sure we cant slide into FH2's harbor one night, steal their booty, put hickeys on their fair damsels, and pressgang ctz into service for our mod? That would be more pirately than all this typing. LOL

Share this post


Link to post
Share on other sites

LOL 1/2 if only we could. Im guessing that, like other mods, their list of fair damsels will be quite a short one. Besides, we've got in trouble for doing that sort of thing before - of course before either of us was on board though.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...