Pressing a key, an amount of times.
#1

Well, i'm creating some kind of a medic script currently, and I need the player to hit a key specific amount of times, for instance, pressing the space bar 20 times. Now, this has to happen in about 20 seconds. Is there any way to get this done? I've been searching around, but I can't really find a solution, if anyone could help me, thanks a lot!
Reply
#2

pawn Код:
#define PRESSED(%0) \
    (((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))
Set a timer for 20 seconds, create a new variable like so...

pawn Код:
new TimesPressed [ MAX_PLAYERS ] = 0, TimerActive [ MAX_PLAYERS ] = 0;
SetTimerEx ( "PressedNumberofTimes", 20000, false, "i", playerid );
pawn Код:
public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    if( PRESSED ( KEY_SPRINT ) )
    {
        if( TimerActive [ playerid ] == 1 )
        {
            TimesPressed [ playerid ] ++;
        }
    }
    return true;
}

forward PressedNumberofTimes ( playerid );
public PressedNumberofTimes ( playerid )
{
    if( TimesPressed [ playerid ] >= 20 )
    {
        // Success!
        TimesPressed [ playerid ] = 0;
    }
    else
    {
        // Failure!
    }
    return true;
}
Reply
#3

Thanks man, Didn't know about the TimerActive thinggy.
Reply
#4

It's just a variable that tells the game you want to use the timer. Without it, you'd just spam space a million times and it would continuously succeed.
Reply
#5

Quote:
Originally Posted by ******
Посмотреть сообщение
Except you don't explain where to start the timer. You don't even need a timer, just GetTickCount.
I assume he wants to show a message to the player saying 'press space 20 times' then do this script, and after 20 seconds wants to show a 'failed' message if they failed. You need a timer for that.

Also, a timer is easier. The most efficient way isn't always the best way, especially for a beginner.
Reply
#6

Quote:
Originally Posted by ******
Посмотреть сообщение
Except you don't explain where to start the timer. You don't even need a timer, just GetTickCount.
I thought that was pretty obvious, and why would I use GetTickCount? I've never even thought of beginning to use it because of this:

Quote:

GetTickCount will cause problems on servers with uptime of over 24 days (SA:MP server, not physical server !!!) as GetTickCount will eventually warp past the integer size constraints

Reply
#7

Quote:
Originally Posted by 2KY
Посмотреть сообщение
I thought that was pretty obvious, and why would I use GetTickCount? I've never even thought of beginning to use it because of this:
In my experience, servers tend to be restarted WAY before approaching the 24 day period.
Reply
#8

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
In my experience, servers tend to be restarted WAY before approaching the 24 day period.
Quote:
Originally Posted by ******
Посмотреть сообщение
I didn't. Anyway, GetTickCount doesn't cause issues unless you do something stupid (and who leaves a server running that long anyway - they are almost always rebooted in that time).
It's the physical server, boxes generally aren't restarted unless there's a problem in my experience at least.

Anyway, just figured a timer would suffice rather than try to use a function which could potentially bug out. There's a line between optimized and OCD, and GetTickCount vs. a 20 second timer is approaching that line.
Reply
#9

Quote:
Originally Posted by 2KY
Посмотреть сообщение
It's the physical server
Mate...

Quote:

SA:MP server, not physical server !!!

Reply
#10

Quote:
Originally Posted by RealCop228
Посмотреть сообщение
Mate...
Quote:
Originally Posted by ******
Посмотреть сообщение
This was quoted in YOUR post:
Man, that's just bad on my part. That's what I get for blindly copy-pasting something because I thought I had read it before. Fair enough, you guys are right, but I still stand behind my decision to use a timer, not like it really matters all that much.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)