Raised This Month: $51 Target: $400
 12% 

[CS:GO] How to get team count ? CT/T


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Audite
AlliedModders Donor
Join Date: Sep 2021
Location: Sweden
Old 08-09-2022 , 11:41   [CS:GO] How to get team count ? CT/T
Reply With Quote #1

Hi! I'm trying to create my own beacon plugin that checks how many T are left and if only 1 left then add beacon on the last T client.

Code:
public Action Event_PlayerDeath(Event event, const char[] name, bool dontBroadcast){
	for (int i = 1; i <= MaxClients; i++)
	{
		int iTeam;

		// Skip bots or non connected indexes.
		if (!IsClientConnected(i) || IsFakeClient(i))
			continue;

		iTeam = GetClientTeam(i);

		if(iTeam == CS_TEAM_T){
			//CHECK HOW MANY ALIVE, IF 1 LEFT PUT BEACON ON THE CLIENT
		}

	}
}
I'm using death hook for reason that if there is 1 player in T and 1 in CT it does not activate.

I'm completely new to sourcepawn and trying to figure stuff out Thanks in advance.
Audite is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 08-09-2022 , 17:39   Re: [CS:GO] How to get team count ? CT/T
Reply With Quote #2

Didn't test but would be something like this.
PHP Code:
    int aliveCTCount;
    
int aliveTCount;
    
int lastTClient;
    
    for (
int client 1client <= MaxClientsclient++)
    {
        if (!
IsClientInGame(client))
            continue;
        
        if (
IsFakeClient(client))
            continue;
        
        if (!
IsPlayerAlive(client))
            continue;
        
        if (
GetClientTeam(client) == CS_TEAM_CT)
        {
            
aliveCTCount++;
        }
        else if (
GetClientTeam(client) == CS_TEAM_T)
        {
            
aliveTCount++;
            
lastTClient client;
        }
    }
    
    if (
aliveCTCount == && aliveTCount == 1)
    {
        
PerformBeacon(lastTClient//Your logic here
    

__________________

Last edited by Marttt; 08-09-2022 at 17:42.
Marttt is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 08-10-2022 , 05:40   Re: [CS:GO] How to get team count ? CT/T
Reply With Quote #3

For readable code, above post is good example.



This example is just... with trick.
PHP Code:

public void OnPluginStart()
{
    
HookEvent("player_death"player_death);
}

public 
void player_death(Event event, const char[] namebool dontBroadcast)
{
    
int lastT = -1;
    
int CTcount 0;
    
int team;

    for(
int i 1<= MaxClientsi++)
    {
        if(!
IsClientInGame(i))
            continue;
        
        
team GetClientTeam(i);
        
        if(
team || !IsPlayerAlive(i))
            continue;



        if(
team == 2)
        {
            if(
lastT == -1)
            {
                
// We store player index in variable, first time.
                
lastT i;
            }
            else
            {
                
// When there are more than one T alive in team, this variable will get reset (-2)
                
lastT = -2;
            }
        }
        else
        {
            
CTcount++;
        }
    }

    if(
lastT && CTcount 1)
    {
        
ServerCommand("sm_beacon #%i"GetClientUserId(lastT));
    }

__________________
Do not Private Message @me
Bacardi is offline
Grey83
Veteran Member
Join Date: Dec 2014
Location: Ukraine
Old 08-10-2022 , 13:01   Re: [CS:GO] How to get team count ? CT/T
Reply With Quote #4

Bacardi, we can improve the algorithm a little more:
PHP Code:
public void OnPluginStart()
{
    
HookEvent("player_death"Event_DeathEventHookMode_PostNoCopy);
}

public 
void Event_Death(Event event, const char[] namebool dontBroadcast)
{
    
int lastTCTcount;
    for(
int i 1team<= MaxClientsi++) if(IsClientInGame(i) && (team GetClientTeam(i)) > && IsPlayerAlive(i))
    {
        if(
team == 2)
        {
            if(!
lastTlastT i;    // We store player index in variable, first time.
            
else if(lastT) return;    // More than one alive terrorist. No more need checks.
        
}
        else 
CTcount++;
    }

    if(
CTcount 1ServerCommand("sm_beacon #%i"GetClientUserId(lastT));

__________________
Grey83 is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 08-10-2022 , 13:32   Re: [CS:GO] How to get team count ? CT/T
Reply With Quote #5

That for loop and if statement is wierdest thing I have seen in web
Bacardi is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 08-10-2022 , 14:35   Re: [CS:GO] How to get team count ? CT/T
Reply With Quote #6

Micro-optimizations like this don't bring many advantages.
Readability and a clean code, IMO - based IRL daily experience - should be a priority instead of the code lines amount, especially in a death hook event.
(unless you are in a big and repeatable loop)
But, EventHookMode_PostNoCopy was a good catch.
Anyway, all snippets are good examples of how to achieve what OP wants.
__________________

Last edited by Marttt; 08-10-2022 at 14:36.
Marttt is offline
Audite
AlliedModders Donor
Join Date: Sep 2021
Location: Sweden
Old 08-20-2022 , 21:15   Re: [CS:GO] How to get team count ? CT/T
Reply With Quote #7

Thank you everyone. I got to see all the possible ways to do it! Thanks!
Audite is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:19.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode