UE1 - UT Need help choosing a random pawn

  • Two Factor Authentication is now available on BeyondUnreal Forums. To configure it, visit your Profile and look for the "Two Step Verification" option on the left side. We can send codes via email (may be slower) or you can set up any TOTP Authenticator app on your phone (Authy, Google Authenticator, etc) to deliver codes. It is highly recommended that you configure this to keep your account safe.

Rajada

Member
Jan 21, 2008
213
0
16
rajada.tumblr.com
I can't seem to find anything that uses this already for this engine, but I need help making code that chooses a pawn at random in a mutator (probably just playerpawns). Any suggestions?
 

Rajada

Member
Jan 21, 2008
213
0
16
rajada.tumblr.com
Nevermind, I found something similar enough in my game's (NAB) source code to adapt for this use. I doubt I'll ever use all 64 player slots, and ended up with this:

Code:
PlayerPawn PP[64];
var byte NumPPS;
var PlayerPawn P;

function PostBeginPlay()
{
	SetTimer(15,True);
	Super.PostBeginPlay();
}
simulated function timer() 
{
	foreach AllActors( class 'PlayerPawn', P )
		{

		PP[NumPPS++] = P;
		if( NumPPS==ArrayCount(PP) )
			break;
		}

	P = PP[Rand(NumPPS)];
}
 

gopostal

Active Member
Jan 19, 2006
848
47
28
Try this function on:

Code:
function Pawn GetRandomPlayer()
{
  local Pawn N;
  local PlayerPawn Dest;
  local Pawn Candidate[32];
  local int num;

  for (N=Level.PawnList; N!=None; N=N.NextPawn)
  {
    Dest=PlayerPawn(N);
    if (Dest!=None && Dest.bIsPlayer && !Dest.PlayerReplicationInfo.bIsSpectator)
    {
      if (num<32) Candidate[num] = Dest;
      else if (Rand(num) < 32) Candidate[Rand(32)] = Dest;
      num++;
    }
  }

  if(num==0) return None;
  return Candidate[Rand(Min(32,num))];
}
 

gopostal

Active Member
Jan 19, 2006
848
47
28
You should avoid ForEach AllActors every time it is possible. Iterating the pawn list like I showed you is much faster and uses less system resources by far.
 

Rajada

Member
Jan 21, 2008
213
0
16
rajada.tumblr.com
I'll keep that in mind because I'm going to be remaking it as a larger mutator. This version is just a proof of concept, thanks for the help.