TF2 - Event when team empty.

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Omniwolf
Junior Member
Posts: 10
Joined: Tue Aug 09, 2016 9:45 pm

TF2 - Event when team empty.

Postby Omniwolf » Fri Aug 26, 2016 5:17 pm

I've looked for ages for a way to detect when RED team is empty, but to no avail. Is there any way I could do this, maybe through an event?
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: TF2 - Event when team empty.

Postby iPlayer » Fri Aug 26, 2016 5:43 pm

Try a combination of 2 events:
1) player_team - because a team might become empty due to somebody switching to another team
2) player_disconnect - because a team might become empty due to somebody disconnecting

Syntax: Select all

from events import Event
from filters.players import PlayerIter


def check_teams():
if len(list(PlayerIter('red'))) == 0:
print("RED team is empty!!!")


@Event('player_disconnect')
def on_player_disconnect(game_event):
check_teams()


@Event('player_team')
def on_player_team(game_event):
check_teams()

This is the simpliest I could think of. Can be optimized by getting team len from team manager instead of iterating over PlayerIter. Also this guarantees to happen when team becomes empty, but can also happen when team is already empty!
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: TF2 - Event when team empty.

Postby satoon101 » Fri Aug 26, 2016 6:12 pm

If I remember correctly, player_team is fired when a player disconnects. In fact, there should be a 'disconnect' boolean event variable in player_team.
Image
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: TF2 - Event when team empty.

Postby iPlayer » Fri Aug 26, 2016 6:17 pm

Yes, I guess you're right. Then it all simplifies to

Syntax: Select all

from events import Event
from filters.players import PlayerIter


@Event('player_team')
def on_player_team(game_event):
if len(list(PlayerIter('red'))) == 0:
print("RED team is empty!!!")


Or, if you only need to fire it once,

Syntax: Select all

from events import Event
from filters.players import PlayerIter


_was_non_zero = True


@Event('player_team')
def on_player_team(game_event):
global _was_non_zero

if len(list(PlayerIter('red'))) == 0:
if _was_non_zero:
print("RED team is empty!!!")
_was_non_zero = False
else:
_was_non_zero = True
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
Omniwolf
Junior Member
Posts: 10
Joined: Tue Aug 09, 2016 9:45 pm

Re: TF2 - Event when team empty.

Postby Omniwolf » Fri Aug 26, 2016 6:34 pm

thnx guys, this'll be helpful!
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: TF2 - Event when team empty.

Postby L'In20Cible » Sat Aug 27, 2016 11:48 am

Instead of counting the player everytime a switch occurs, we could simply make sure the player is leaving the RED team (which optimize quite a bit, imo):

Syntax: Select all

from events import Event

@Event('player_team')
def player_team(game_event):
# If the player is not coming from the red team, that means the count
# didn't change...
if game_event.get_int('oldteam') != 2:
return

# Make sure he was the last player into the RED team...
if len(PlayerIter('red')):
return

print('RED team is empty!!!')


However, I'm quite unsure if that event is called before or after m_iTeamNum has been updated for the player so this code may not works.

As a side note, you don't have to cast PlayerIter as a list in order to get its length since this is handled internally into _IterObject.__len__. So, len(PlayerIter('red')) should works just fine.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: TF2 - Event when team empty.

Postby iPlayer » Sat Aug 27, 2016 11:57 am

As a side note, you don't have to cast PlayerIter as a list in order to get its length since this is handled internally into _IterObject.__len__

Yeah, wasn't sure if __len__ was implemented, so used list() to make sure.

Also, there's an entity with a classname team_manager, it might occur to be faster than counting players manually.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: TF2 - Event when team empty.

Postby L'In20Cible » Sat Aug 27, 2016 12:00 pm

Yes, I know about the cs_/tf_team_manager (CTeam instances) entities. But the offset where the length is stored is not mapped into the ServerClass nor the DataMap and I still prefer users to use a bit more CPU than having to constantly update the data files to keep it updated. :tongue:
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: TF2 - Event when team empty.

Postby L'In20Cible » Sat Aug 27, 2016 12:05 pm

Actually, the players are mapped into an array which is networkable, wrapping it may give us the ability to count the element. However, our implementation doesn't allow for this currently and last time I checked, the bits/flags/etc of the SendProp needed to be verified as a case-by-case basis. I will put that on my to-do list, as we definitely have to get array implemented for entities (not just for temp entities).

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 31 guests