AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Detect when player presses two buttons (https://forums.alliedmods.net/showthread.php?t=237425)

wicho 03-23-2014 20:46

Detect when player presses two buttons
 
Hi everybody, well i want to prevent something when a player hold the buttons w and a or w and d buttons simultaneously for two seconds, well my question is how i can detect when the players hold this buttons at the same time for two seconds? i want to set hight gravity when this happen but my quesion is just the other, can someone give me an example? ..thx in advance

fysiks 03-24-2014 01:35

Re: Detect when player presses two buttons
 
It's not possible to detect keys but it is possible to detect the command. So, if we assume the default key binds, you want to detect the movement commands. You need to find plugins that already check for buttons for an example how to determine the "buttons" (aka the commands sent that are made for keyboard keys). Some of the more common ones that I've seen are the IN_SCORE button.

Once you get the basic methodology for determining the 'buttons' that are being pressed, then you will need to keep track of how long the button has been pressed.

Anyways, you should start with the basic detecting the buttons and then go to the 2 seconds thing. Here are some useful posts:

https://forums.alliedmods.net/showthread.php?t=198285
https://forums.alliedmods.net/showthread.php?t=214980
https://forums.alliedmods.net/showthread.php?t=231550

wicho 03-24-2014 16:06

Re: Detect when player presses two buttons
 
Thx this help me a lot, just another question how i can make for example if the player holding down the botton for 2 or more seconds i want activate something, how i can do?

code:

PHP Code:

#include <fakemeta>
#include <fun>
#include <engine>

public plugin_init() 
{
         
     
register_forward(FM_CmdStart"OnCmdStart"false
}

public 
OnCmdStart(idcmd
{
    if(!
is_user_alive(id))
        return 
1;
            
    new 
buttons get_user_button(id) ;    
    new 
oldbuttons get_user_oldbutton(id);
    
    if(
buttons IN_FORWARD && !(oldbuttons IN_FORWARD) || buttons IN_MOVELEFT && !(oldbuttons IN_MOVELEFT)) 
         { 
                  
set_user_gravity(id999999.9)// if the player holding down buttons for 2 or more seconds i want to set hight gravity
         

    else if(
buttons IN_FORWARD && !(oldbuttons IN_FORWARD) || buttons IN_MOVERIGHT && !(oldbuttons IN_MOVERIGHT)) 
         { 
                  
set_user_gravity(id999999.9// if the player holding down buttons for 2 or more seconds i want to set hight gravity
    
else if(oldbuttons IN_FORWARD && !(buttons IN_FORWARD) || oldbuttons IN_MOVELEFT && !(buttons IN_MOVELEFT)) 
         { 
                  
set_user_gravity(id1.0)// if the player released the buttons i want to set normal gravity, i dunno if this is correct..
         
}    
         else if(
oldbuttons IN_FORWARD && !(buttons IN_FORWARD) || oldbuttons IN_MOVERIGHT && !(buttons IN_MOVERIGHT)) 
         { 
                  
set_user_gravity(id1.0)// if the player released the buttons i want to set normal gravity, i dunno if this is correct..
         

     
    return 
1;



Black Rose 03-25-2014 14:30

Re: Detect when player presses two buttons
 
The XY-problem is making it hard to filter the buttons to your likings. I'll give you 3 examples that you can make fit your purpose.

This will be called every call of CmdStart, if you want it only once you'll have to filter it with a bool.
Code:
#include <amxmodx> #include <fakemeta> new Float:gTime[33]; public plugin_init() {         register_forward(FM_CmdStart, "OnCmdStart", false) } public OnCmdStart(id, uc_handle, seed) {         if ( ! is_user_alive(id) )         return;         static buttons;     buttons = get_uc(uc_handle, UC_Buttons);         if ( buttons & IN_FORWARD && ( buttons & IN_MOVELEFT || buttons & IN_MOVERIGHT ) ) {                 if ( ! gTime[id] )             gTime[id] = get_gametime();                 else if ( get_gametime() - gTime[id] > 2.0 )             server_print("User %d has been holding the buttons for more than 2 seconds", id);     }     else         gTime[id] = 0.0;         return; }

This will be called once per holding-down-instance.
Code:
#include <amxmodx> #include <fakemeta> new gBool[33]; public plugin_init() {         register_forward(FM_CmdStart, "OnCmdStart", false) } public OnCmdStart(id, uc_handle, seed) {         if ( ! is_user_alive(id) )         return;         static buttons;     buttons = get_uc(uc_handle, UC_Buttons);         if ( buttons & IN_FORWARD && ( buttons & IN_MOVELEFT || buttons & IN_MOVERIGHT ) ) {         if ( ! gBool[id] ) {             set_task(2.0, "taskFunction", id);             gBool[id] = true;         }     }     else {         remove_task(id);         gBool[id] = false;     }         return; } public taskFunction(id)     server_print("User %d has been holding the buttons for more than 2 seconds", id);

This will only count if the same 2 keys has been hold down for 2 seconds.
Code:
#include <amxmodx> #include <fakemeta> new Float:gTime[33][3]; public plugin_init() {         register_forward(FM_CmdStart, "OnCmdStart", false) } public OnCmdStart(id, uc_handle, seed) {         if ( ! is_user_alive(id) )         return;         static buttons, oldbuttons, Float:gametime;     buttons = get_uc(uc_handle, UC_Buttons);     oldbuttons = pev(id, pev_oldbuttons);     gametime = get_gametime();         if ( buttons & IN_FORWARD ) {         if ( ! ( oldbuttons & IN_FORWARD ) )             gTime[id][0] = gametime;     }     else         gTime[id][0] = 0.0;         if ( buttons & IN_MOVELEFT ) {         if ( ! ( oldbuttons & IN_MOVELEFT ) )             gTime[id][1] = gametime;     }     else         gTime[id][1] = 0.0;         if ( buttons & IN_MOVERIGHT ) {         if ( ! ( oldbuttons & IN_MOVERIGHT ) )             gTime[id][2] = gametime;     }     else         gTime[id][2] = 0.0;         if ( gTime[id][0] && gametime - gTime[id][0] > 2.0 && ( ( gTime[id][1] && gametime - gTime[id][1] > 2.0 ) || ( gTime[id][2] && gametime - gTime[id][2] > 2.0 ) ) )         server_print("User %d has been holding the buttons for more than 2 seconds", id);         return; }

I strongly advice against any of this if there's a better way.

wicho 03-26-2014 01:16

Re: Detect when player presses two buttons
 
wow very thx for the examples, so if i want to set gravity when player hold down two keys then give normal gravity when player release the button, in this form is fine?

PHP Code:

#include <amxmodx>
#include <fakemeta>
#include <fun>

new Float:gTime[33][3];

public 
plugin_init()
{
    
    
register_forward(FM_CmdStart"OnCmdStart"false)
}

public 
OnCmdStart(iduc_handleseed) {
    
    if ( ! 
is_user_alive(id) )
        return;
    
    static 
buttonsoldbuttonsFloat:gametime;
    
buttons get_uc(uc_handleUC_Buttons);
    
oldbuttons pev(idpev_oldbuttons);
    
gametime get_gametime();
    
    if ( 
buttons IN_FORWARD 
    {
        if ( ! ( 
oldbuttons IN_FORWARD ) )
            
gTime[id][0] = gametime;
    }
    else
    {
        
set_user_gravity(id1.0)
        
gTime[id][0] = 0.0;
    }
    
    if ( 
buttons IN_MOVELEFT 
    {
        if ( ! ( 
oldbuttons IN_MOVELEFT ) )
            
gTime[id][1] = gametime;
    }
    else
    {
        
set_user_gravity(id1.0)
        
gTime[id][1] = 0.0;
    }
    
    if ( 
buttons IN_MOVERIGHT 
    {
        if ( ! ( 
oldbuttons IN_MOVERIGHT ) )
            
gTime[id][2] = gametime;
    }
    else
    {   
        
set_user_gravity(id1.0)
        
gTime[id][2] = 0.0;
    }
    
    if ( 
gTime[id][0] && gametime gTime[id][0] > 2.0 && ( ( gTime[id][1] && gametime gTime[id][1] > 2.0 ) || ( gTime[id][2] && gametime gTime[id][2] > 2.0 ) ) )
    {
        
set_user_gravity(id999999.9)
        
server_print("User %d has been holding the buttons for more than 2 seconds"id);
    }
    
    return;
    



hornet 03-26-2014 01:51

Re: Detect when player presses two buttons
 
It's possible that there's another solution for your problem. You might be taking the wrong approach.
Enough of the XY problem, and fully explain why you want this? What is it going to achieve in the end? Does it have something to do with long jumping?

wicho 03-26-2014 02:15

Re: Detect when player presses two buttons
 
Yes is something about jump, for example when you stick to the wall and aim to the wall and you hold the keys w and a or w and d at the same time and you jump a lot of time the player gains more speed and is a bit annoying, that's why I'm trying to do this.

hornet 03-26-2014 02:43

Re: Detect when player presses two buttons
 
So you've just come up with the solution yourself - hook Jump() and check for the buttons or for a certain player speed.

See how much more simple things become when you fully explain your problem?

wicho 03-26-2014 14:02

Re: Detect when player presses two buttons
 
Ok, just i have another question how i can save correctly the gravity of the players? i mean i use this on zombie plague mod and the zombies and humans have different gravity, i make this but dont work..

PHP Code:

#include <amxmodx>
#include <fakemeta>
#include <engine>

new Float:gTime[33][3];
new 
Float:gSaveGravity[33]

public 
plugin_init()
{
    
    
register_forward(FM_CmdStart"OnCmdStart"false)
}

public 
OnCmdStart(iduc_handleseed
{
    
    if ( ! 
is_user_alive(id) )
        return;
    
    static 
buttonsoldbuttonsFloat:gametime
    buttons 
get_uc(uc_handleUC_Buttons);
    
oldbuttons pev(idpev_oldbuttons);
    
gametime get_gametime();
    
    if ( 
buttons IN_FORWARD 
    {
        if ( ! ( 
oldbuttons IN_FORWARD ) )
            
gTime[id][0] = gametime;
    }
    else
    {
        
entity_set_float(idEV_FL_gravitygSaveGravity[id])
        
gTime[id][0] = 0.0;
    }
    
    if ( 
buttons IN_MOVELEFT 
    {
        if ( ! ( 
oldbuttons IN_MOVELEFT ) )
            
gTime[id][1] = gametime;
    }
    else
    {
        
entity_set_float(idEV_FL_gravitygSaveGravity[id])
        
gTime[id][1] = 0.0;
    }
    
    if ( 
buttons IN_MOVERIGHT 
    {
        if ( ! ( 
oldbuttons IN_MOVERIGHT ) )
            
gTime[id][2] = gametime;
    }
    else
    {        
        
entity_set_float(idEV_FL_gravitygSaveGravity[id])        
        
gTime[id][2] = 0.0;
    }
    
    if ( 
gTime[id][0] && gametime gTime[id][0] > 2.0 && ( ( gTime[id][1] && gametime gTime[id][1] > 2.0 ) || ( gTime[id][2] && gametime gTime[id][2] > 2.0 ) ) )
    {
        
        
gSaveGravity[id] = entity_get_float(idEV_FL_gravity)        
        
entity_set_float(idEV_FL_gravity999999.9 )    
        
server_print("User %d has been holding the buttons for more than 2 seconds"id);
    }
      
    return;
    



hornet 03-26-2014 19:10

Re: Detect when player presses two buttons
 
You'vs misunderstood, CmdStart is not required.


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

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