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

Solved Do two different client checks but run same code block


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PatriotGames
AlliedModders Donor
Join Date: Feb 2012
Location: root@irs:/# rm -rf /
Old 01-14-2019 , 18:48   Do two different client checks but run same code block
Reply With Quote #1

This is probably easy for most of you, but the logic has me scratching my head.

A function exec the same nfty code for any one of the special infected (no tank) or the witch , with each having their own classId (1-6 for SI, 7 for witch).

How can we do a different valid client check for any classId and if true, run the same nifty code without writing it twice?
Code:
//client check for SI (classId 1-6):

if (class != 7 && IsClientInGame(client) && IsPlayerAlive(client) && GetClientTeam(client) == 3)  // Note: is it correct that GetClientTeam() does not exist for the Witch?
{
	nifty code;
}

// client check for Witch (classId 7):

if (class == 7 && IsValidWitch(witch))
{
	nifty code;
}

IsValidWitch(witch)
{
	if (witch != INVALID_ENT_REFERENCE && IsValidEdict(witch) && IsValidEntity(witch))
	{
		decl String:classname[32];
		GetEdictClassname(witch, classname, sizeof(classname));
		if(StrEqual(classname, "witch"))
		{
			return true;
		}
	}
	return false;
}
Thank you,
pg

Last edited by PatriotGames; 01-15-2019 at 18:08. Reason: Solved
PatriotGames is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 01-15-2019 , 04:00   Re: Do two different client checks but run same code block
Reply With Quote #2

Probably not the best way but here you go:

PHP Code:
void MyFunction(int client)
{
    if (
bIsSpecialInfected(client) || bIsWitch(client))
    {
        
// nifty code
    
}
}

stock bool bIsSpecialInfected(int client)
{
    
// Invalid client = no
    
if (>= client || client MaxClients || !IsClientInGame(client) || !IsPlayerAlive(client))
    {
        return 
false;
    }

    
// Zombie class is 0, 7, or greater than 7 = no
    
int iClass GetEntProp(clientProp_Send"m_zombieClass");
    if (
>= iClass || iClass >= 7)
    {
        return 
false;
    }

    
// Client team is not infected = no
    
if (GetClientTeam(client) != 3)
    {
        return 
false;
    }

    
// If none of the above return false (no), then return true (yes)
    
return true;
}

stock bool bIsWitch(int witch)
{
    
// Entity must be valid
    
if (witch != INVALID_ENT_REFERENCE && IsValidEntity(witch))
    {
        
char sClassname[32];
        
GetEntityClassname(witchsClassnamesizeof(sClassname));
        if (
StrEqual(sClassname"witch"))
        {
            
// Witch class = yes
            
return true;
        }
    }

    
// If the above does not return true (yes), then return false (no)
    
return false;

__________________
Psyk0tik is offline
PatriotGames
AlliedModders Donor
Join Date: Feb 2012
Location: root@irs:/# rm -rf /
Old 01-15-2019 , 17:07   Re: Do two different client checks but run same code block
Reply With Quote #3

Hi Crasher,

Thanks for your suggestion, it seems like a very reasonable solution.

Any reason why we couldn't simplify it further with this small change:
Code:
Original:

    // Zombie class is 0, 7, or greater than 7 = no
    int iClass = GetEntProp(client, Prop_Send, "m_zombieClass");
    if (0 >= iClass || iClass >= 7)
    {
        return false;
    }

    // Client team is not infected = no
    if (GetClientTeam(client) != 3)
    {
        return false;
    }

    // If none of the above return false (no), then return true (yes)
    return true;

Change:

stock bool bIsSpecialInfected(int client)
{
    // Invalid client = no
    if (0 >= client || client > MaxClients || !IsClientInGame(client) || !IsPlayerAlive(client))
    {
        return false;
    }

    // Zombie class is 0, 7, or greater than 7 = no  or is not on infected team
    int iClass = GetEntProp(client, Prop_Send, "m_zombieClass");
    if (0 >= iClass || iClass >= 7 || GetClientTeam(client) != 3)
    {
        return false;
    }

    // If none of the above return false (no), then return true (yes)
    return true;
Thank you,
pg
PatriotGames is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 01-15-2019 , 17:09   Re: Do two different client checks but run same code block
Reply With Quote #4

No particular reason. I just wanted to show an extra example of returning false from an if statement. You can actually check the team first before the zombie class since the zombie class would be 0 anyway if the client is a survivor or spectator.
__________________
Psyk0tik is offline
PatriotGames
AlliedModders Donor
Join Date: Feb 2012
Location: root@irs:/# rm -rf /
Old 01-15-2019 , 18:08   Re: Do two different client checks but run same code block
Reply With Quote #5

I appreciate your examples and the way you commented what was happening at each step, really helpful! Thanks for taking the time to do this.

pg
PatriotGames is offline
Psyk0tik
Veteran Member
Join Date: May 2012
Location: Homeless
Old 01-15-2019 , 18:35   Re: Do two different client checks but run same code block
Reply With Quote #6

No problem; glad to help!
__________________
Psyk0tik 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 19:20.


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