Author |
Topic: Random Player |
|
Category: CoD2 Scripting Posted: Monday, Nov. 8, 2010 11:54 am |
|
Hey all
I want to choose a random player. For that there is this function:
Code: random_player = players[randomint(players.size)];
But, I would like to choose a random player of a specific team or a random player except a specific player!
How would that work? Does anyone have an idea or knows how?
Thanks in anticipation. |
|
|
|
IzNoGoD |
General Member Since: Nov 29, 2008 Posts: 694 Last: Nov 10, 2012 [view latest posts] |
|
|
Category: CoD2 Scripting Posted: Monday, Nov. 8, 2010 01:21 pm |
|
make
Code:
team="allies";
teamplayers=[];
for(i=0;i<players.size;i++)
{
if(players[i].pers["team"]==team)
teamplayers[teamplayers.size]=players[i];
}
randomplayer=teamplayers[randomint(players.size)];
or
Code:
exceptplayer=self;
otherplayers=[];
for(i=0;i<players.size;i++)
{
if(players[i]!=exceptplayer)
otherplayers[otherplayers.size]=players[i]
}
randomplayer=otherplayers[randomint(otherplayers.size)];
|
|
|
|
|
|
|
|
|
Category: CoD2 Scripting Posted: Tuesday, Nov. 9, 2010 04:18 pm |
|
But before the "for" we need to declare players:
Code: players = getentarray("player", "classname"); |
|
|
|
|
Category: CoD2 Scripting Posted: Tuesday, Nov. 9, 2010 04:44 pm |
|
I'm getting confused now :S.
Code: if(players[i].pers["team"]==team)
teamplayers[teamplayers.size]=players[i];
}
randomplayer=teamplayers[randomint(players.size)];
Does this mean, that it will only choose a random player, if all the players in the server are in the spectator team?
I'm not really sure about what i'm asking, but I have the next problem:
If there is 1 axis player and 3 spectators, "sometimes" (:S) the 3 spectators become the random players. Why is this happening? |
|
|
|
IzNoGoD |
General Member Since: Nov 29, 2008 Posts: 694 Last: Nov 10, 2012 [view latest posts] |
|
|
Category: CoD2 Scripting Posted: Tuesday, Nov. 9, 2010 05:10 pm |
|
Thats because you most likely set a random team, and you need to set
team="allies";
correct
Code:
if(randomint(1)==0)
team="allies";
else
team="axis";
or something like that |
|
|
|
liltc64 |
General Member Since: Feb 12, 2007 Posts: 906 Last: Oct 22, 2012 [view latest posts] |
|
|
|
Category: CoD2 Scripting Posted: Tuesday, Nov. 9, 2010 05:13 pm |
|
try this
Code: teamPlayers = [];
teamPlayer["allies"] = [];
players = getentarray("player", "classname");
for(i=0; i<players.size; i++)
{
plr = players[i];
if(isDefined(plr.pers["team"]) && isDefined(level.teamPlayers[plr.pers["team"]]))
level.teamPlayers[plr.pers["team"]][level.teamPlayers[plr.pers["team"]].size] = plr;
}
randomPlayer = [];
if(teamPlayer["allies"].size > 0)
{
randomPlayer["allies"] = teamPlayers["allies"][randomInt[teamPlayers["allies"].size
} |
|
|
|
|
Category: CoD2 Scripting Posted: Tuesday, Nov. 9, 2010 05:39 pm |
|
IzNoGoD writes...Quote: Thats because you most likely set a random team, and you need to set
team="allies";
correct
Code:
if(randomint(1)==0)
team="allies";
else
team="axis";
or something like that
Oh sorry, I copied wrong, I wanted to ask it about this code:
Code: teamplayers=[];
players = getentarray("player", "classname");
for(i = 0 ; i < players.size ; i++)
{
if(players[i].pers["team"] == "spectator")
teamplayers[teamplayers.size] = players[i];
}
level.randomAxis = teamplayers[randomint(players.size)];
Do I need to apply what you said anyway? |
|
|
|
|
Category: CoD2 Scripting Posted: Tuesday, Nov. 9, 2010 05:42 pm |
|
liltc64 writes...Quote: try this
Code: teamPlayers = [];
teamPlayer["allies"] = [];
players = getentarray("player", "classname");
for(i=0; i<players.size; i++)
{
plr = players[i];
if(isDefined(plr.pers["team"]) && isDefined(level.teamPlayers[plr.pers["team"]]))
level.teamPlayers[plr.pers["team"]][level.teamPlayers[plr.pers["team"]].size] = plr;
}
randomPlayer = [];
if(teamPlayer["allies"].size > 0)
{
randomPlayer["allies"] = teamPlayers["allies"][randomInt[teamPlayers["allies"].size
}
Okay, I will try it, the bad thing is that like I said it only happens "sometimes" so I need to try it a lot of times to be sure that it works. |
|
|
|