AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Take Player +attack (https://forums.alliedmods.net/showthread.php?t=338608)

XSlayer 07-16-2022 20:42

Take Player +attack
 
Hi, i wanted to know how to hook player primary attack,first of all, discard the ham forward, because it only runs when it fires itself, when you do something else it doesn't run, I just want to get if the player is pressing the primary attack, second, I read somewhere that CMD_Start is very bad for the CPU, so what options do I have to get it?( register_clcmd( "+attack", is blocked or something by the way )

damage220 07-16-2022 21:09

Re: Take Player +attack
 
Hi. There are IN_ATTACK and IN_ATTACK2 flags.
PHP Code:

public client_PreThink(cid)
{
    new 
btn;

    if(!
is_user_alive(cid))
        return;
    
btn get_user_button(cid);
    if(!(
btn IN_ATTACK))
        return;
    
/* do some things */


client_PreThink is also resource intensive, though.

XSlayer 07-16-2022 21:09

Re: Take Player +attack
 
Quote:

Originally Posted by damage220 (Post 2783939)
Hi. There are IN_ATTACK and IN_ATTACK2 flags.
PHP Code:

public client_PreThink(cid)
{
    new 
btn;

    if(!
is_user_alive(cid))
        return;
    
btn get_user_button(cid);
    if(!(
btn IN_ATTACK))
        return;
    
/* do some things */




PreThink is more effective than cmd start? i mean for the cpu

PHP Code:

public @ClientCommand_InstantClientHandle )
{
      if(!
is_user_aliveClient ))
      {
           return 
1;
      }
      else
      {
         if(!
__int_Transformation[Client][5]) // Cant make an instant transformation
         

                 return 
1;
         }
         else
         {        
            if(
__int_Transformation[Client][0]) // Is in a transformation
            
{
                    return 
1;
            }
            else
            {            
               if(
get_user_weaponClient ) != 1// Not holding the correct weapon
               
{
                         return 
1;
               }   
               else
               { 
                      if(!
get_pdata_intClient198 )) // Not blocking
                      
{
                               return 
1;
                      }
                      else
                      {
                               static 
ControlControl get_uc(HandleUC_Buttons);

                               if((
Control == IN_ATTACK))
                               {
                                      
fxChargeInstantClient );
                               }
                      }
                       
               }
            }
         }
      }
      return 
0;


im doing this in CMD_Start

damage220 07-17-2022 10:44

Re: Take Player +attack
 
I have no idea which method is better, but you may try both. I would recommend to count how many times a method is called.
PHP Code:

public client_PreThink(cid)
{
    static 
count 0;

    
client_print(cidprint_chat"client_PreThink call #%d", ++count);
}

public 
ClientCommand_Instant(cidhandle)
{
    static 
count 0;

    
client_print(cidprint_chat"ClientCommand_Instant call #%d", ++count);


And choose the one that has less number.
PS: try to avoid nested "if" whenever possible.

Natsheh 07-18-2022 03:42

Re: Take Player +attack
 
Use cmd start its much better because its called when a player uses a key or hold a key.

damage220 07-18-2022 20:45

Re: Take Player +attack
 
Quote:

Originally Posted by Natsheh (Post 2784050)
Use cmd start its much better because its called when a player uses a key or hold a key.

FM_CmdStart as well as client_PreThink or client_cmdStart is called on every frame, not a key press, and can be used to catch client state. You can make sure of it by changing fps_max value.
https://forums.alliedmods.net/showpo...03&postcount=5

Natsheh 07-19-2022 04:34

Re: Take Player +attack
 
Quote:

Originally Posted by damage220 (Post 2784119)
FM_CmdStart as well as client_PreThink or client_cmdStart is called on every frame, not a key press, and can be used to catch client state. You can make sure of it by changing fps_max value.
https://forums.alliedmods.net/showpo...03&postcount=5

Cmdstart actually expect when a +action to be used then its called every frame otherwise when there is no action been used it will be stopped getting called.

damage220 07-19-2022 11:45

Re: Take Player +attack
 
Quote:

Originally Posted by Natsheh (Post 2784142)
Cmdstart actually expect when a +action to be used then its called every frame otherwise when there is no action been used it will be stopped getting called.

Could you provide an example program? I did some tests and found out that any of the mentioned events are called on every frame no matter if there is +cmd or not.

wilian159 07-24-2022 22:16

Re: Take Player +attack
 
this ?:

PHP Code:

register_forward(FM_CmdStart"xCmdStart"false)

public 
xCmdStart(iduc_handlerandseed)
{
    if(!
is_user_alive(id))
        return 
FMRES_IGNORED
    
    
static xBtnxOldBtn
    xBtn 
get_uc(uc_handleUC_Buttons)
    
xOldBtn pev(idpev_oldbuttons)

    if((
xBtn IN_ATTACK) && !(xOldBtn IN_ATTACK))
    {
        const 
m_flNextAttack 83
        
const Float:nextTime 9999.0

        set_pdata_float
(idm_flNextAttacknextTime)

        
client_print(id3"-> attack block")
    }

    return 
FMRES_IGNORED



damage220 07-25-2022 03:51

Re: Take Player +attack
 
Quote:

Originally Posted by wilian159 (Post 2784687)
this ?:

PHP Code:

register_forward(FM_CmdStart"xCmdStart"false)

public 
xCmdStart(iduc_handlerandseed)
{
    if(!
is_user_alive(id))
        return 
FMRES_IGNORED
    
    
static xBtnxOldBtn
    xBtn 
get_uc(uc_handleUC_Buttons)
    
xOldBtn pev(idpev_oldbuttons)

    if((
xBtn IN_ATTACK) && !(xOldBtn IN_ATTACK))
    {
        const 
m_flNextAttack 83
        
const Float:nextTime 9999.0

        set_pdata_float
(idm_flNextAttacknextTime)

        
client_print(id3"-> attack block")
    }

    return 
FMRES_IGNORED



No. It is called every frame even when player idle (i.e. do nothing). I was expecting that the handler will be called when I jump or shoot, or whatever but not when I am moving or idle.


All times are GMT -4. The time now is 15:40.

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