AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved simplify (https://forums.alliedmods.net/showthread.php?t=337329)

kww 04-14-2022 09:05

simplify
 
Is it possible to simplify this code? It must have 2 conditions.
PHP Code:

public Ham_Knife_Deploy_Post(iEnt)
{
    static 
idid get_pdata_cbase(iEntm_pPlayer4)
    
    if(!
g_bBot[id] && g_bConnected[id]) // i don't know why connect checking needed. (when i see any ham deploy hooking there are connection checks)
    
{
        
refreshKnifeModel(id// if user isn't a bot
    
}
    else if(
g_bBot[id] && g_iBotsWithRandomKnives)
    {
        
refreshKnifeModel(id// if user is a bot and bots can have random knives
    
}
    
    return 
HAM_IGNORED



zXCaptainXz 04-14-2022 12:20

Re: simplify
 
PHP Code:

public Ham_Knife_Deploy_Post(iEnt)
{
    static 
idid get_pdata_cbase(iEntm_pPlayer4)
    
    if(!
g_bBot[id]))
    {
        if(!
g_bConnected[id])
        {
            return 
HAM_IGNORED;
        }         
    }
    else if(!
g_iBotsWithRandomKnives)
    {    
        return 
HAM_IGNORED
    
}
    
    
refreshKnifeModel(id)
    
    return 
HAM_IGNORED



CrazY. 04-14-2022 12:33

Re: simplify
 
You don't need to check if the player is connected on Ham_Item_Deploy hook.

Code:
public Ham_Knife_Deploy_Post(iEnt) {     new id = get_pdata_cbase(iEnt, m_pPlayer, 4)         if(g_bBot[id] && g_iBotsWithRandomKnives)     {         return     }     refreshKnifeModel(id) }

kww 04-14-2022 13:05

Re: simplify
 
Final look:
PHP Code:

public Ham_Knife_Deploy_Post(iEnt)
{
    static 
idid get_pdata_cbase(iEntm_pPlayer4)
    
    if(!
g_bBot[id])
    {
        
refreshKnifeModel(id// if user isn't a bot
    
}
    else if(
g_iBotsWithRandomKnives)
    {
        
refreshKnifeModel(id// if user is a bot and bots can have random knives
    
}
    
    return 
HAM_IGNORED



Bugsy 04-14-2022 13:36

Re: simplify
 
The id being within range may not be needed but it's cheap insurance to throw it in there.
PHP Code:

public Ham_Knife_Deploy_Post(iEnt)
{
    static 
idid get_pdata_cbase(iEntm_pPlayer4)
    
    if ( ( 
<= id <= MAX_PLAYERS ) && ( !g_bBot[id] || g_iBotsWithRandomKnives ) )
    {
        
refreshKnifeModel(id)
    }
    
    return 
HAM_IGNORED



kww 04-14-2022 14:17

Re: simplify
 
Quote:

Originally Posted by Bugsy (Post 2776793)
The id being within range may not be needed but it's cheap insurance to throw it in there.
PHP Code:

public Ham_Knife_Deploy_Post(iEnt)
{
    static 
idid get_pdata_cbase(iEntm_pPlayer4)
    
    if ( ( 
<= id <= MAX_PLAYERS ) && ( !g_bBot[id] || g_iBotsWithRandomKnives ) )
    {
        
refreshKnifeModel(id)
    }
    
    return 
HAM_IGNORED



any reason to check if it is a player?

Natsheh 04-14-2022 14:20

Re: simplify
 
Quote:

Originally Posted by kww (Post 2776795)
any reason to check if it is a player?

Actually its not needed, entity is certained to have an owner.

Bugsy 04-14-2022 15:14

Re: simplify
 
In normal circumstances, but I suppose other plugins/mods could trigger deploy on knife by a non-player entity. It's not making your server CPU sweat by having it, and there's a good chance you'd not get an error without it. I did mention this in my post, that you can likely get away without it.


All times are GMT -4. The time now is 21:22.

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