Just an alternative way of detecting player joins.

Discussions about Coding and Scripting
Post Reply
ShaiHulud
Adept
Posts: 459
Joined: Sat Dec 22, 2012 6:37 am

Just an alternative way of detecting player joins.

Post by ShaiHulud »

I'm not going to go into detail about my use-case this time, simply sharing this snippet here in case it ever proves useful to anyone else.

In GameInfo.uc, in the Login() function, BroadcastMessage is called with "EnteredMessage" - this occurs when a player joins a server, and while the playerPawn object is being initialized. However, at the point in Login() when the call to BroadcastMessage() occurs, the playerPawn object is fully available, including its playerReplicationInfo. Thus (in your Mutator class - remembering to use "level.game.registerMessageMutator(self)" at some point in your Mutator's start-up script):

Code: Select all

function bool mutatorBroadcastMessage(actor sender, pawn receiver, out coerce string msg, optional bool bBeep, out optional name type)
{
  local int i;
  local string enterMsg, actualMsg, playerName;
  local playerPawn PP;

  enterMsg = class'gameinfo'.default.enteredMessage;
  i = inStr(msg, enterMsg);

  if (sender == level.game && receiver == level.pawnList && i > 0)
  {
    actualMsg = right(msg, len(msg) - i);
    if (actualMsg == enterMsg)
    {
      playerName = left(msg, i);
      PP = findPlayerByName(playerName);
      if (PP != none && PP.playerReplicationInfo.playerID == level.game.currentID - 1)
      {
        // Do something with the newly connected player
      }
    }
  }

  // Pass the baton to the next message mutator
  if (nextMessageMutator != none)
		return nextMessageMutator.mutatorBroadcastMessage(sender, receiver, msg, bBeep, type);

	return true;
}

//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
function playerPawn findPlayerByName(string playerName)
{
  local pawn p;
  local tournamentPlayer TP;

  for (p = level.pawnList; p != none; p = p.nextPawn)
  {
    if (tournamentPlayer(p) != none)
    {
      TP = tournamentPlayer(p);
      if (TP.playerReplicationInfo.playerName == playerName)
        return TP;
    }
  }
  return none;
}
Post Reply