Login x
User Name:
Password:
Social Links Facebook Twitter YouTube Steam RSS News Feeds

Members Online

»
0 Active | 86 Guests
Online:

LATEST FORUM THREADS

»
CoD: Battle Royale
CoD+UO Map + Mod Releases
Damaged .pk3's
CoD Mapping
heli to attack ai
CoD4 SP Mapping

Forums

»

Welcome to the MODSonline.com forums. Looking for Frequently Asked Questions? Check out our FAQs section or search it out using the SEARCH link below. If you are new here, you may want to check out our rules and this great user's guide to the forums and the website.
For more mapping and modding information, see our Wiki: MODSonWiki.com

Jump To:
Forum: All Forums : Call of Duty 2
Category: CoD2 Scripting
Scripting and coding with Call of Duty 2.
Moderators: foyleman, Foxhound, Mystic, StrYdeR, batistablr, Welshy, DrBiggzz, supersword, playername
Latest Posts
Subscribed Posts
Search
Restricted Access Topic is Locked
Page
Previous Page Next Page
subscribe
Author Topic: Random Player
Pedro699
General Member
Since: Jun 19, 2006
Posts: 781
Last: Dec 18, 2010
[view latest posts]
Level 7
Category: CoD2 Scripting
Posted: Tuesday, Nov. 9, 2010 06:03 pm
You code won't work liltc as there is a syntax error and the appearance of a level array from somewhere.

I would solve the original specification with two functions:

First get a random player on a team:

Code:
getRandomPlayer(team)
{
   teamPlayers = [];

   players = getentarray("player", "classname");

   for(i=0; i<players.size; i++)
   {
      plr = players[i];

      if(isDefined(plr.pers["team"]) && plr.pers["team"] == team)
         teamPlayers[teamPlayers.size] = plr;
   }


   if(teamPlayers.size > 0)
      return teamPlayers[randomInt(teamPlayers.size)];
   else
      return undefined;
}


In your existing code you would then have:

Code:
randPlayer = getRandomPlayer("axis");


depending on the team you were looking for.


For the exclusion try:

Code:
getRandomExcludePlayer(exPlayer)
{
   rPlayers = [];

   players = getentarray("player", "classname");

   for(i=0; i<players.size; i++)
   {
      plr = players[i];
         
      if(plr != exPlayer)
         rPlayers[rPlayers.size] = plr;
   }


   if(rPlayers.size > 0)
      return rPlayers[randomInt(rPlayers.size)];
   else
      return undefined;
}


Where you would call:

Code:
randPlayer = getRandomExcludePlayer(player);


where 'player' is a reference to the player you want to exclude.
Share |
Leal
General Member
Since: Feb 5, 2010
Posts: 196
Last: Aug 18, 2013
[view latest posts]
Level 4
Category: CoD2 Scripting
Posted: Tuesday, Nov. 9, 2010 07:24 pm
Can I use "randPlayer" as "level.randPlayer"? It's for making a iprintln of the "randPlayer.name" later in an other part of the script.

And what do this "returns" in this part?

Code:
	if(teamPlayers.size > 0)
		return teamPlayers[randomInt(teamPlayers.size)];
	else
		return undefined;


edited on Nov. 9, 2010 02:28 pm by Leal
Share |
liltc64
General Member
Since: Feb 12, 2007
Posts: 906
Last: Oct 22, 2012
[view latest posts]
Level 7
Im a fan of MODSonair
Category: CoD2 Scripting
Posted: Tuesday, Nov. 9, 2010 10:12 pm
yeah my bad arrays are hard for me but i still try [tongue]
Share |
Pedro699
General Member
Since: Jun 19, 2006
Posts: 781
Last: Dec 18, 2010
[view latest posts]
Level 7
Category: CoD2 Scripting
Posted: Wednesday, Nov. 10, 2010 02:49 pm
Leal writes...
Quote:
Can I use "randPlayer" as "level.randPlayer"? It's for making a iprintln of the "randPlayer.name" later in an other part of the script.

And what do this "returns" in this part?


The code is a standalone function and functions return an answer - in this case a random player. In case there are not enough players on the server it returns "undefined" - i.e. no random player found.

For your original purpose you can use:

Code:

randPlayer = getRandomPlayer("axis");

// some more code

if(isDefined(randPlayer))
   iprintln(randPlayer.name);
Share |
IzNoGoD
General Member
Since: Nov 29, 2008
Posts: 694
Last: Nov 10, 2012
[view latest posts]
Level 6
Category: CoD2 Scripting
Posted: Wednesday, Nov. 10, 2010 07:39 pm
Code:
if(players[i].pers["team"] == "spectator")


Well, if you dont want spectators to be the random players, dont use this line :P
Else, define team.
And: If(teamplayers.size==0) there are no players on a team.
Add a check for it somewhere
Share |
Leal
General Member
Since: Feb 5, 2010
Posts: 196
Last: Aug 18, 2013
[view latest posts]
Level 4
Category: CoD2 Scripting
Posted: Thursday, Nov. 11, 2010 04:43 pm
liltc64 writes...
Quote:
yeah my bad arrays are hard for me but i still try [tongue]


Mh what?
Share |
Leal
General Member
Since: Feb 5, 2010
Posts: 196
Last: Aug 18, 2013
[view latest posts]
Level 4
Category: CoD2 Scripting
Posted: Thursday, Nov. 11, 2010 04:44 pm
Pedro699 writes...
Quote:
Leal writes...
Quote:
Can I use "randPlayer" as "level.randPlayer"? It's for making a iprintln of the "randPlayer.name" later in an other part of the script.

And what do this "returns" in this part?


The code is a standalone function and functions return an answer - in this case a random player. In case there are not enough players on the server it returns "undefined" - i.e. no random player found.

For your original purpose you can use:

Code:

randPlayer = getRandomPlayer("axis");

// some more code

if(isDefined(randPlayer))
   iprintln(randPlayer.name);


But if I want to use the random player in an other thread? I used it as a level. variable and it seems to work fine.
Share |
Leal
General Member
Since: Feb 5, 2010
Posts: 196
Last: Aug 18, 2013
[view latest posts]
Level 4
Category: CoD2 Scripting
Posted: Thursday, Nov. 11, 2010 04:45 pm
IzNoGoD writes...
Quote:
Code:
if(players[i].pers["team"] == "spectator")


Well, if you dont want spectators to be the random players, dont use this line :P
Else, define team.
And: If(teamplayers.size==0) there are no players on a team.
Add a check for it somewhere


Okay thank you!
Share |
Pedro699
General Member
Since: Jun 19, 2006
Posts: 781
Last: Dec 18, 2010
[view latest posts]
Level 7
Category: CoD2 Scripting
Posted: Thursday, Nov. 11, 2010 06:51 pm
Leal writes...
Quote:


But if I want to use the random player in an other thread? I used it as a level. variable and it seems to work fine.


So use:

Code:
level.randPlayer = getRandomPlayer("axis");


Then level.randPlayer is available everywhere.
Share |
Leal
General Member
Since: Feb 5, 2010
Posts: 196
Last: Aug 18, 2013
[view latest posts]
Level 4
Category: CoD2 Scripting
Posted: Friday, Nov. 12, 2010 08:28 pm
Okay, thank you very much liltc and IzNoGoD, specialy Pedro :D. It's working fine :).
Share |
Restricted Access Topic is Locked
Page
Previous Page Next Page
subscribe
MODSonline.com Forums : Call of Duty 2 : CoD2 Scripting

Latest Syndicated News

»
Codutility.com up and runn...
Nice, and there still using the logo and template for the screenshots, which...
Codutility.com up and runn...
dundy writes...Quote:Call of Duty modding and mapping is barly alive only a ...
Codutility.com up and runn...
Mystic writes...Quote:It seems to me the like the site is completely dead? ...
Codutility.com up and runn...
It seems to me the like the site is completely dead?

Partners & Friends

»