Detect player team change

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

Detect player team change

Post by ShaiHulud » Sat Oct 15, 2022 12:25 pm

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: 8956
Joined: Thu Jun 30, 2011 8:12 pm
Personal rank: God of Fudge
Location: Palo Alto, CA

Re: Detect player team change

Post by EvilGrins » Sat Oct 15, 2022 10:09 pm

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: 446
Joined: Sat Dec 22, 2012 6:37 am

Re: Detect player team change

Post by ShaiHulud » Sat Oct 15, 2022 10:46 pm

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

Re: Detect player team change

Post by Sp0ngeb0b » 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?
Website, Forum & UTStats

Image
******************************************************************************
Nexgen Server Controller || My plugins & mods on GitHub
******************************************************************************

Dennis
Average
Posts: 63
Joined: Tue Jan 12, 2021 9:18 pm

Re: Detect player team change

Post by Dennis » 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?
Fraggers hangout place: http://fraggers.online/

ShaiHulud
Adept
Posts: 446
Joined: Sat Dec 22, 2012 6:37 am

Re: Detect player team change

Post by ShaiHulud » Sun Oct 16, 2022 12:39 pm

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: 63
Joined: Tue Jan 12, 2021 9:18 pm

Re: Detect player team change

Post by Dennis » Mon Oct 17, 2022 5:46 pm

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: 6031
Joined: Sun May 09, 2010 6:15 pm
Location: On the roof.

Re: Detect player team change

Post by sektor2111 » Mon Oct 17, 2022 7:50 pm

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.)