Keys State Help
#1

Hi!

How to detect a player when he/she used "KEY_JUMP" for 3 times? and apply an animation for it?

Thank you for your response!
Reply
#2

https://sampwiki.blast.hk/wiki/GetPlayerKeys
Reply
#3

PHP код:
new Used[MAX_PLAYERS];
if(
PRESSED(KEY_JUMP))
{
         
Used[playerid] += 1;
         if(
Used[playerid] >= 3)
         {
             new 
string[100];
             
format(stringsizeof(string), "*** %s (ID:%d) has used the JUMP KEY more than 3 times"GetName(playerid), playerid);
             
SendClientMessageToAll(-1string);
         }

You should have the stock GetName

Код:
stock GetName(playerid)
{
	new Name[MAX_PLAYER_NAME];
	GetPlayerName(playerid,Name,MAX_PLAYER_NAME);
	return Name;
}
Reply
#4

Quote:
Originally Posted by Loinal
Посмотреть сообщение
PHP код:
new Used[MAX_PLAYERS];
if(
PRESSED(KEY_JUMP))
{
         
Used[playerid] += 1;
         if(
Used[playerid] >= 3)
         {
             new 
string[100];
             
format(stringsizeof(string), "*** %s (ID:%d) has used the JUMP KEY more than 3 times"GetName(playerid), playerid);
             
SendClientMessageToAll(-1string);
         }

You should have the stock GetName

Код:
stock GetName(playerid)
{
	new Name[MAX_PLAYER_NAME];
	GetPlayerName(playerid,Name,MAX_PLAYER_NAME);
	return Name;
}
How do you expect that work correctly without checking the times between presses? No resetting of the variable either.

The best thing to do would be to create your own system that allows defining custom key combo detection.
Reply
#5

Quote:
Originally Posted by Pottus
Посмотреть сообщение
How do you expect that work correctly without checking the times between presses? No resetting of the variable either.

The best thing to do would be to create your own system that allows defining custom key combo detection.
PHP код:
#include <a_samp>
#define PRESSED(%0) \
    
(((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
new 
timesJump[MAX_PLAYERS];
public 
OnPlayerKeyStateChange(playeridnewkeysoldkeys)
{
    if(!
IsPlayerInAnyVehicle(playerid))
    {
        new 
firstjumpTime;
    
        if(
PRESSED(KEY_JUMP))
        {
    
            
timesJump[playerid]++;
    
            if(
timesJump[playerid] == 1)
            {
                
firstjumpTime gettime()+4500;
                return 
1;
            }
    
            if(
timesJump[playerid] > 3)
            {
                
timesJump[playerid] = 0;
    
                if(
firstjumpTime gettime())
                {
                    
OnPlayerJumpThreeTimes(playerid);
                }
            }
    
            return 
1;
        }
    }
    return 
1;
}
forward public OnPlayerJumpThreeTimes(playerid);
public 
OnPlayerJumpThreeTimes(playerid)
{
    
SendClientMessage(playerid, -1"detected jumping 3 times within timespan of 4.5 seconds");
    
// the code when the player jumps three times
    
ApplyAnimation(playerid"GYMNASIUM""gym_tread_falloff"4.1011015001);
    
timesJump[playerid] = 0;
    return 
1;

here's what i've come up with.


to the op:

it uses timestamps. it gets the current time + 4.5 seconds so for example if the time is 12:50:30 (hh mm ss) it will store 12:50:34 in the variable.

if the player has jumped 3 times while the time is less than 12:50:34 (time span of 4 seconds) it will detect and reset the variable for the jump counter.

(technically timestamp is in seconds so 199999 would be 200003 if 4 seconds were added to it).
this code may be bad practice, but it is an idea of what you want.

i can confirm that my method works.


alternatively, you could use ziggi's GetTickDiff function and check the interval in ms of each jump, so this way, when people spam the jump key and they do it within below a specific value, you can apply an animation to make them trip or what not.
Reply
#6

Quote:
Originally Posted by git
Посмотреть сообщение
PHP код:
#include <a_samp>
#define PRESSED(%0) \
    
(((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
new 
timesJump[MAX_PLAYERS];
public 
OnPlayerKeyStateChange(playeridnewkeysoldkeys)
{
    if(!
IsPlayerInAnyVehicle(playerid))
    {
        new 
firstjumpTime;
    
        if(
PRESSED(KEY_JUMP))
        {
    
            
timesJump[playerid]++;
    
            if(
timesJump[playerid] == 1)
            {
                
firstjumpTime gettime()+4500;
                return 
1;
            }
    
            if(
timesJump[playerid] > 3)
            {
                
timesJump[playerid] = 0;
    
                if(
firstjumpTime gettime())
                {
                    
OnPlayerJumpThreeTimes(playerid);
                }
            }
    
            return 
1;
        }
    }
    return 
1;
}
forward public OnPlayerJumpThreeTimes(playerid);
public 
OnPlayerJumpThreeTimes(playerid)
{
    
SendClientMessage(playerid, -1"detected jumping 3 times within timespan of 4.5 seconds");
    
// the code when the player jumps three times
    
ApplyAnimation(playerid"GYMNASIUM""gym_tread_falloff"4.1011015001);
    
timesJump[playerid] = 0;
    return 
1;

Thank you for helping although this doesn't work for me. I want something like on sampdm.
Reply
#7

Keep in mind that the sniper zoom uses the same key. If you are creating some kind of anti bunnyhop this is a no-no.
Reply
#8

Quote:
Originally Posted by Istrator
Посмотреть сообщение
Thank you for helping although this doesn't work for me. I want something like on sampdm.
You've just stated you wanted if a player jumps 3 times, they will get an animation played? What from where I have made for you, doesn't work for you?

I've just researched this sampdm server and my code does exactly what sampdm does.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)