AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Hooking player actions (+reload, +attack2, etc) (https://forums.alliedmods.net/showthread.php?t=238292)

epicoder 04-07-2014 14:51

Hooking player actions (+reload, +attack2, etc)
 
I'm trying to write a plugin that involves giving a player noclip while attack2 is held. Here is the gist of what I have:

Code:

public OnPluginStart() {
    AddCommandListener(Lst_Noclip, "+attack2");
    AddCommandListener(Lst_Noclip, "-attack2");
}

public Action:Lst_Noclip(client, const String:command[], argc) {
    if (command[0] == '+') {
        SetEntityMoveType(client, MOVETYPE_NOCLIP);
    } else {
        SetEntityMoveType(client, MOVETYPE_WALK);
    }
    return Plugin_Continue;
}

(in the actual code, I have all the check that player is alive/in-game/etc. This is a simplified version)

As far as I can tell, the listener is not even getting called at all. I've tried putting PrintToChat debug lines in there, typing +attack2 in console rather than pressing the button, just about anything I could think of.

I came up with an alternate version that involves checking each usercmd for attack2 using OnPlayerRunCmd, but that seemed a bit expensive and I was hoping to avoid it. Is what I'm trying to do possible with the method I'm using?

wyd3x 04-07-2014 14:54

Re: Hooking player actions (+reload, +attack2, etc)
 
try that :) :

PHP Code:

public Action:OnPlayerRunCmd(client, &buttons)
{
    if (
buttons & +attack2)
    {
        
// do that
    
} else if (buttons & -attack2) {
       
// do that



captaindeterprimary 04-07-2014 14:55

Re: Hooking player actions (+reload, +attack2, etc)
 
Quote:

Originally Posted by wyd3x (Post 2121576)
try that :) :

PHP Code:

public Action:OnPlayerRunCmd(client, &buttons)
{
    if (
buttons & +attack2)
    {
        
// do that
    
} else if (buttons & -attack2) {
       
// do that



Why not do a case switch?

wyd3x 04-07-2014 14:57

Re: Hooking player actions (+reload, +attack2, etc)
 
My mistake. try that:
PHP Code:

public Action:OnPlayerRunCmd(client, &buttons)
{
    if (
buttons IN_ATTACK)
    {
        
// do that
    
} else if (buttons IN_ATTACK2) {
       
// do that



Quote:

Originally Posted by vman315 (Post 2121578)
Why not do a case switch?

hmmm.. I don`t know XD

epicoder 04-07-2014 15:00

Re: Hooking player actions (+reload, +attack2, etc)
 
While I appreciate your response, I already have that code... Like I said, I was hoping to avoid OnPlayerRunCmd. It seems a little expensive for what I want to do. What I want to know is whether I will have to use OnPlayerRunCmd or not.

Powerlord 04-07-2014 15:13

Re: Hooking player actions (+reload, +attack2, etc)
 
Quote:

Originally Posted by vman315 (Post 2121578)
Why not do a case switch?

Because buttons is a bitfield not an enum.

Incidentally, to track when a person presses or lets go of IN_ATTACK2, you're going to have to track what it was on the previous OnPlayerRunCmd as it fires continually as long as the player is doing things.

Mathias. 04-07-2014 15:56

Re: Hooking player actions (+reload, +attack2, etc)
 
Quote:

Originally Posted by epicoder (Post 2121583)
While I appreciate your response, I already have that code... Like I said, I was hoping to avoid OnPlayerRunCmd. It seems a little expensive for what I want to do. What I want to know is whether I will have to use OnPlayerRunCmd or not.

Well nvm you idea than. It the way to track players action, people are afraid of calling codes in gameframes. It only bad if you do something crazy like accessing a keyvalue/create temporary entity/etc.. If you plan to declare variables use static and your all set.

Mitchell 04-07-2014 16:38

Re: Hooking player actions (+reload, +attack2, etc)
 
I dont under stand how it's expensive to use OnPlayerRunCmd?
something like this to make a toggle command:
Code:

public Action:OnPlayerRunCmd(iClient, &buttons, &impulse, Float:vel[3], Float:angles[3], &weapon)
{
        if (!IsClientConnected(iClient) || !IsClientInGame(iClient) || !IsPlayerAlive(iClient)) return Plugin_Continue;
        static bool:bIsInAttack[MAXPLAYERS+1];
        if (!bIsInAttack[iClient] && buttons & IN_ATTACK)
        {
                bIsInAttack[iClient] = true;
                SomeOtherFunction(iClient);
        }
        else
        {
                bIsInAttack[iClient] = false;
        }
        return Plugin_Continue;
}

bit tired so it might be wrong

captaindeterprimary 04-07-2014 17:23

Re: Hooking player actions (+reload, +attack2, etc)
 
Quote:

Originally Posted by Powerlord (Post 2121589)
Because buttons is a bitfield not an enum.

Incidentally, to track when a person presses or lets go of IN_ATTACK2, you're going to have to track what it was on the previous OnPlayerRunCmd as it fires continually as long as the player is doing things.

Huh, I didn't know this.

epicoder 04-07-2014 20:55

Re: Hooking player actions (+reload, +attack2, etc)
 
Quote:

Originally Posted by Black-Rabbit (Post 2121626)
Well nvm you idea than. It the way to track players action, people are afraid of calling codes in gameframes. It only bad if you do something crazy like accessing a keyvalue/create temporary entity/etc.. If you plan to declare variables use static and your all set.

Ok, I will keep this in mind.

Quote:

Originally Posted by Mitchell (Post 2121666)
I dont under stand how it's expensive to use OnPlayerRunCmd?

I run my server on budget hardware that has barely enough memory to run srcds + sm core along with the other software stacks I run on it, and the CPU is not exactly a beast either. As such, I try to consider the impact of everything I am doing... Checking each and every usercmd when only one client will be allowed to do this in the end just seemed a bit excessive to me. But if that's the only way to do it, that's the only way to do it. Thanks for the information.


All times are GMT -4. The time now is 13:10.

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