Delay after writing a command
#1

Hey guys, i wondered how to do delay that when you write the command (for example 5 seconds after) the action will happen.
Example:
[CMD:fire(playerid, params[])]
The way it should happen:
when player is writing /fire:
SendClientMessage(Playerid, color_White, "arming the fire"); (5 seconds)

Then its showing the player a GameTextForPlayer: ~~~~~~~~%s Seconds Left~~~~~~~~

and then after 5 seconds, it will say SendClientMessage(Playerid, color_White, "fire is armed you can now fire the missle (( using /firemissle ))");

But he may use /firemissle only after he used /fire

I am breaking my head how to do it. if someone can help me i will be greatful

-BoomShnizel-
Reply
#2

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

Quote:
Originally Posted by sampreader
Посмотреть сообщение
Its still too complicated for me, if you can do it for me that will be great
Reply
#4

It's easy. Read the comments carefully and if something needs more explanation, feel free to ask:

pawn Код:
// To get this to work, you gotta declare a per-player global variable to use it for counting down
new CountDown[MAX_PLAYERS];
// You will also need a variable to hold the player timer, so we can use it on KillTimer()
new PlayerTimer[MAX_PLAYERS];

// The following code should be placed under your command or where the action begins
CountDown[playerid] = 5; // set the count down variable to 5, so it counts down from 5 to 0
PlayerTimer[playerid] = SetTimerEx("ExecuteCommand", 1000, true, "i", playerid); // creating a repeatedly timer with an interval of 1 second, and passing the playerid that we will execute the cmd on
// As SetTimerEx returns the timer id, so we made the PlayerTimer var hold the timerid so we can kill it later.
// -
// Now, let's forward the function that the timer is gonna call
forward ExecuteCommand(playerid);

public ExecuteCommand(playerid)
{
    CountDown[playerid] --; // decreases the CountDown variable by 1
    if(CountDown[playerid] == 0) // (IF) the CountDown variable reached 0
    {
        KillTimer(PlayerTimer[playerid]); // kill the timer, so it won't call this function again.
        // Execute your command here
        // Example: GivePlayerWeapon(playerid, 24, 300);
        return 1;
    }
    else // (ELSE) if the CountDown variable hasn't reached 0 yet.
    {
        // Tell the player how many seconds left
        // Example:
        new str[20];
        format(str, sizeof str, "%i second(s) left", CountDown[playerid]);
        GameTextForPlayer(playerid, str, 1000, 3);
    }
    return 1;
}
Reply
#5

Quote:
Originally Posted by HellSphinX
Посмотреть сообщение
It's easy. Read the comments carefully and if something needs more explanation, feel free to ask:

pawn Код:
// To get this to work, you gotta declare a per-player global variable to use it for counting down
new CountDown[MAX_PLAYERS];
// You will also need a variable to hold the player timer, so we can use it on KillTimer()
new PlayerTimer[MAX_PLAYERS];

// The following code should be placed under your command or where the action begins
CountDown[playerid] = 5; // set the count down variable to 5, so it counts down from 5 to 0
PlayerTimer[playerid] = SetTimerEx("ExecuteCommand", 1000, true, "i", playerid); // creating a repeatedly timer with an interval of 1 second, and passing the playerid that we will execute the cmd on
// As SetTimerEx returns the timer id, so we made the PlayerTimer var hold the timerid so we can kill it later.
// -
// Now, let's forward the function that the timer is gonna call
forward ExecuteCommand(playerid);

public ExecuteCommand(playerid)
{
    CountDown[playerid] --; // decreases the CountDown variable by 1
    if(CountDown[playerid] == 0) // (IF) the CountDown variable reached 0
    {
        KillTimer(PlayerTimer[playerid]); // kill the timer, so it won't call this function again.
        // Execute your command here
        // Example: GivePlayerWeapon(playerid, 24, 300);
        return 1;
    }
    else // (ELSE) if the CountDown variable hasn't reached 0 yet.
    {
        // Tell the player how many seconds left
        // Example:
        new str[20];
        format(str, sizeof str, "%i second(s) left", CountDown[playerid]);
        GameTextForPlayer(playerid, str, 1000, 3);
    }
    return 1;
}
I will check it tomorrow THANKS MAN
Reply
#6

THANK YOU MAN, I OWN YOU

But if can you help me get it into a command that will be really epic :

/fire (arming the missle) (then here goes the timer you wrote)

after /fire is finishing BUT ONLY AFTER IT FINISHS the player will be able to use the command:

/missle (but only once)

can you help me do it? (with ZCMD, like CMD:command(playerid, params[]))
Reply
#7

Do something like this:

pawn Код:
new CountDown[MAX_PLAYERS];
new PlayerTimer[MAX_PLAYERS];
new CanUseMissle[MAX_PLAYERS];
CanUseMissle[MAX_PLAYERS] = 0;
CountDown[playerid] = 5;
PlayerTimer[playerid] = SetTimerEx("ExecuteCommand", 1000, true, "i", playerid);
forward ExecuteCommand(playerid);

public ExecuteCommand(playerid)
{
    CountDown[playerid] --;
    if(CountDown[playerid] == 0)
    {
        KillTimer(PlayerTimer[playerid]);
        CanUseMissle[playerid] = 1;
        return 1;
    }
    else
        new str[20];
        format(str, sizeof str, "%i second(s) left", CountDown[playerid]);
        GameTextForPlayer(playerid, str, 1000, 3);
    }
    return 1;
}
CMD:fire(playerid, params[])
{
    SetTimerEx("ExecuteCommand", 1000, true, "i", playerid);
    SendClientMessage(playerid, -1, "You have started to arm the missle.");
    return 1;
}
CMD:missle(playerid, params[])
{
    if(CanUseMissle[playerid] == 0)
    {
        SendClientMessage(playerid, -1, "You need to use the command /fire first.");
    }
    else if(CanUseMissle[playerid] == 1)
    {
        // Your missle command stuff
        SendClientMessage(playerid, -1, "You have fired a missle!");
        CanUseMissle[playerid] = 0;
    }
    return 1;
}
You have to edit certain things, but that's a basic example.
Reply
#8

PlayerTimer[playerid] = SetTimerEx("ExecuteCommand", 1000, true, "i", playerid);

Abreezy missed it above.
Reply
#9

Код:
C:\Users\talg\Desktop\ачеешйен\NBRP\gamemodes\NGRP_nomysql.pwn(49583) : error 003: declaration of a local variable must appear in a compound block
C:\Users\talg\Desktop\ачеешйен\NBRP\gamemodes\NGRP_nomysql.pwn(49583) : warning 217: loose indentation
C:\Users\talg\Desktop\ачеешйен\NBRP\gamemodes\NGRP_nomysql.pwn(49583) : error 017: undefined symbol "str"
C:\Users\talg\Desktop\ачеешйен\NBRP\gamemodes\NGRP_nomysql.pwn(49583) : warning 215: expression has no effect
C:\Users\talg\Desktop\ачеешйен\NBRP\gamemodes\NGRP_nomysql.pwn(49583) : error 001: expected token: ";", but found "]"
C:\Users\talg\Desktop\ачеешйен\NBRP\gamemodes\NGRP_nomysql.pwn(49583) : fatal error 107: too many error messages on one line
to this:
Код:
new str[20];
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)