Detect player team change

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

Detect player team change

Post by ShaiHulud »

Just wanted to share something I observed recently while working on a Nexgen plugin. Nexgen (and other mutators that I've peered into) keeps track of player team changes by testing the playerReplicationInfo.team value against a stored team value in a timer event with a short fuse.

If these two numbers diverge, Nexgen issues a playerTeamChanged() call to plugins. In the vast majority of instances this works just fine. But for reasons that would be too long-winded to go into, I needed something that had a direct link back to the line that caused the team change to happen in the first place - in other words, I didn't want additional game ticks to take place before I had a chance to intercept and deal with them.

So poking around, and tracing through the class hierarchy, I eventually found the AddToTeam() function inside of TeamGamePlus (so, obviously, this will only be applicable to TeamGamePlus derived game types).

Near the end of the AddToTeam() function, a call to BroadcastLocalizedMessage() is issued, and this can be intercepted by a mutatorBroadcastLocalizedMessage() function in a mutator to watch for the relevant LocalMessage class, e.g.,

Code: Select all

function bool mutatorBroadcastLocalizedMessage(actor sender, pawn receiver, out class<LocalMessage> message, out optional int switch, out optional playerReplicationInfo relatedPRI_1, out optional playerReplicationInfo relatedPRI_2, out optional object optionalObject)
{
  local teamInfo TI;

  if (message == class'botpack.deathMatchMessage' && sender.isA('CTFGame') && switch == 3)
  {
    TI = teamInfo(optionalObject);
    // TI.teamIndex contains the new team number
    // relatedPRI_1 is the playerReplicationInfo for the player who changed teams
    if (TI != none)
      LOG(relatedPRI_1.playerName $ " SWITCHED TO TEAM " $ TI.teamIndex);
  }

  return true;
}
Last edited by ShaiHulud on Sat Oct 15, 2022 10:47 pm, edited 1 time in total.
User avatar
EvilGrins
Godlike
Posts: 9731
Joined: Thu Jun 30, 2011 8:12 pm
Personal rank: God of Fudge
Location: Palo Alto, CA
Contact:

Re: Detect player team change

Post by EvilGrins »

In my experience, when playing if anyone changes teams a message pops on the screen and states it.

How is this better than that?
http://unreal-games.livejournal.com/
Image
medor wrote:Replace Skaarj with EvilGrins :mrgreen:
Smilies · viewtopic.php?f=8&t=13758
ShaiHulud
Adept
Posts: 459
Joined: Sat Dec 22, 2012 6:37 am

Re: Detect player team change

Post by ShaiHulud »

It isn't, it's not intended as a replacement of any kind, I just added the code snippet here for anyone who might be writing a mutator, and who wants a way to detect player team changes in their script.
User avatar
Sp0ngeb0b
Adept
Posts: 376
Joined: Wed Feb 13, 2008 9:16 pm
Location: Cologne
Contact:

Re: Detect player team change

Post by Sp0ngeb0b »

Thanks for sharing that hook Shai! Out of curiousity, how do other teamgame gametypes setup that message? Referring to the "sender.isA('CTFGame') && switch == 3" conditions - is above code snippet generally applicable?
Website, Forum & UTStats

Image
******************************************************************************
Nexgen Server Controller || My plugins & mods on GitHub
******************************************************************************
Dennis
Average
Posts: 72
Joined: Tue Jan 12, 2021 9:18 pm

Re: Detect player team change

Post by Dennis »

You can also just add the playerReplicationInfo.team check in a function Tick() to detect it just as fast?
Fraggers hangout place: http://fraggers.online/
ShaiHulud
Adept
Posts: 459
Joined: Sat Dec 22, 2012 6:37 am

Re: Detect player team change

Post by ShaiHulud »

Sp0ngeb0b wrote: Sun Oct 16, 2022 9:47 am Thanks for sharing that hook Shai! Out of curiousity, how do other teamgame gametypes setup that message? Referring to the "sender.isA('CTFGame') && switch == 3" conditions - is above code snippet generally applicable?
Good question, Sponge, I was being totally single-minded about figuring out a solution for my situation, so I haven't investigated other scenarios so far!
Dennis wrote: Sun Oct 16, 2022 10:12 am You can also just add the playerReplicationInfo.team check in a function Tick() to detect it just as fast?
Very true, but as I mentioned, in this very particular case I didn't want additional game ticks to occur between the team change and my ability to respond to it, so this technique meant I was able to satisfy that requirement.
Dennis
Average
Posts: 72
Joined: Tue Jan 12, 2021 9:18 pm

Re: Detect player team change

Post by Dennis »

ShaiHulud wrote: Sun Oct 16, 2022 12:39 pm Very true, but as I mentioned, in this very particular case I didn't want additional game ticks to occur between the team change and my ability to respond to it, so this technique meant I was able to satisfy that requirement.
But if you hook a message then you will also be on next tick() as one tick() has to occur before input is processed into message? So we are on the same Tick() regardless what method is used correct?
Fraggers hangout place: http://fraggers.online/
User avatar
sektor2111
Godlike
Posts: 6412
Joined: Sun May 09, 2010 6:15 pm
Location: On the roof.

Re: Detect player team change

Post by sektor2111 »

Sp0ngeb0b wrote: Sun Oct 16, 2022 9:47 am Referring to the "sender.isA('CTFGame') && switch == 3" conditions - is above code snippet generally applicable?
if Sender is Pawn then... I'm really scared...
I don't get if a Sender can be a game-type. Tell me that I'm wrong.

Resuming.
If I want to detect a team change, I think my methods are not really bad. Once again, assign an actor tracking players with vars "OldTeam" "CurrentTeam" working in a SLOW timer NOT TICK (not 80 times/second or such ticks) - we don't have to exhaust resources for something simple. When OldTeam != CurrentTeam this means that player has changed team - here you can increment something "Changes++". If a limit is set, do what is supposed to be done (eat his legs, arms, etc.)
User avatar
snowguy
Novice
Posts: 8
Joined: Sun Feb 17, 2019 9:07 am

Re: Detect player team change

Post by snowguy »

Thanks for writing about this Shai. Just had occasion to use it and was happy to not have to put additional checks in a tick or timer.

A couple things that might be worth mentioning that I learned when using it. It seems the function is called for each pawn (or is it playerpawn?) on the server so the log would be repeated multiple times depending on the number of players and spectators on the server. I changed the TI check to this to make it only log once:

Code: Select all

if (TI != none && receiver.PlayerReplicationInfo!= none && receiver.PlayerReplicationInfo == relatedPRI_1)
Also, I broke other mutators in the chain that used this function when using the code as is so I changed the 'return true' to the following:

Code: Select all

return Super.MutatorBroadcastLocalizedMessage( Sender, Receiver, Message, Switch, RelatedPRI_1, RelatedPRI_2, OptionalObject );
The mutator I was working with had already registered itself as a message mutator so I'm not sure if that is required for the use of this function or if it had any bearing on my findings listed above. If anyone has more info about this I would like to know. :)
Post Reply