Possible to add a string in SetTimer?
#1

Well as the tittle states is it possible to add a string to SetTimer in the time param? Heres a command i have been making and i am deffinatly doing it wrong. Just need a little assistance.
pawn Код:
if(!strcmp(cmdtext, "/rs", true, 3))
    {
        if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,Color_Red, "Only admins can use this command");
        if(!cmdtext[3])return SendClientMessage(playerid, 0xFF0000FF, "USAGE: /reload [minutes]");
        new str[128];
        format(str, sizeof(str), "%s", cmdtext[5]);
        Timer1 = SetTimer("Timer_Restart_Kick", 1000 * 60* str, false);
        return 1;
    }
And if it is possible how would i make it so if some one does "/rs bla bla" instead of "/rs 20" it says something along the lines of this "Please Only use numbers"?

Thanks
Nick
Reply
#2

I would recommend sscanf for checking parameters.

And Use SetTimerEx for setting timers with parameters.
Reply
#3

Well i need some help, i have yet to use sccanf and SetTimerEx and use it proporly and i need help(this command is alot of fail)....
pawn Код:
if(strcmp(cmdtext, "/restart", true, 4))
    {
        new Msg[128];
        new timelimit[4];
        if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid,Color_Red, "Only admins can use this command");
        if (unformat(cmdtext, "iiii", timelimit)) return SendClientMessage(playerid, 0xFF0000AA, "Usage: /rs <Time in minutes>");
        //SetTimerEx("Timer_Restart_Kick", timelimit, false, "iiii", timelimit);

        format(Msg, 128, "%i ", cmdtext);
        SendClientMessage(playerid, 0x00FF00FF, Msg);
        return 1;
    }
I commented off the timer part just to see if i could figure out the unformat part but i cant get it to work, what ever i do it just says "Usage: /rs <Time in minutes>" . And as for the timer i dont get how to add the number string to that, just wondering if someone could explain to me how to do this correctly. I dont get the tuts on how to do them.
Thanks
Nick
Reply
#4

What do you want the command to do?
Reply
#5

This is using sscanf2.5. This tool is especially useful for commands such as this.
pawn Код:
if(!strcmp(cmdtext,"/reload",true))
{
    if(!IsPlayerAdmin(playerid)) // If they are not Admin then it sends the message
    {
        return SendClientMessage(playerid,-1,"You are not authorised to use this command.");
    }
    new gmxdelay; // A new variable that the player will type after typing "/reload"
    // This is the line that checks if they didn't type the command correctly.
    if(sscanf(params,"d",gmxdelay))
    {
        // So if they didn't type it correctly then tell them how to use it.
        return SendClientMessage(playerid,-1,"Syntax: /reload <minutes>");
    }
    if(gmxdelay == 0) // If they typed 0
    {
        return SendClientMessage(playerid,-1,"It must be at least one minute.");
    }
    else
    {
        // The "gmxdelay*1000*60" is what converts it to minutes.
        // gmxdelay is multiplied by 1000, which is one second
        // that product is then multiplied by 60 to equal a minute
        SetTimer("gmxtimer",gmxdelay*1000*60,false);
        new
            string[128]; // A new optional string to format
        format(string,sizeof(string),"The server will restart in %d minute.",gmxdelay);
        SendClientMessageToAll(-1,string);
    }
    return 1;
}
forward gmxtimer();
public gmxtimer() // So once the timer is up, then it....
{
    // sends the gmx command to RCON
    SendRconCommand("gmx");
}
Here is the same example with ZCMD.
pawn Код:
CMD:reload(playerid,params[])
{
    if(!IsPlayerAdmin(playerid)) // If they are not Admin then it sends the message
    {
        return SendClientMessage(playerid,-1,"You are not authorised to use this command.");
    }
    new gmxdelay; // A new variable that the player will type after typing "/reload"
    // This is the line that checks if they didn't type the command correctly.
    if(sscanf(params,"d",gmxdelay))
    {
        // So if they didn't type it correctly then tell them how to use it.
        return SendClientMessage(playerid,-1,"Syntax: /reload <minutes>");
    }
    if(gmxdelay == 0) // If they typed 0
    {
        return SendClientMessage(playerid,-1,"It must be at least one minute.");
    }
    else
    {
        // The "gmxdelay*1000*60" is what converts it to minutes.
        // gmxdelay is multiplied by 1000, which is one second
        // that product is then multiplied by 60 to equal a minute
        SetTimer("gmxtimer",gmxdelay*1000*60,false);
        new
            string[128]; // A new optional string to format
        format(string,sizeof(string),"The server will restart in %d minute.",gmxdelay);
        SendClientMessageToAll(-1,string);
    }
    return 1;
}
forward gmxtimer();
public gmxtimer() // So once the timer is up, then it....
{
    // sends the gmx command to RCON
    SendRconCommand("gmx");
}
Hope I was of any help.
Reply
#6

Quote:
Originally Posted by VincentDunn
Посмотреть сообщение
This is using sscanf2.5. This tool is especially useful for commands such as this.
pawn Код:
if(!strcmp(cmdtext,"/reload",true))
{
    if(!IsPlayerAdmin(playerid)) // If they are not Admin then it sends the message
    {
        return SendClientMessage(playerid,-1,"You are not authorised to use this command.");
    }
    new gmxdelay; // A new variable that the player will type after typing "/reload"
    // This is the line that checks if they didn't type the command correctly.
    if(sscanf(params,"d",gmxdelay))
    {
        // So if they didn't type it correctly then tell them how to use it.
        return SendClientMessage(playerid,-1,"Syntax: /reload <minutes>");
    }
    if(gmxdelay == 0) // If they typed 0
    {
        return SendClientMessage(playerid,-1,"It must be at least one minute.");
    }
    else
    {
        // The "gmxdelay*1000*60" is what converts it to minutes.
        // gmxdelay is multiplied by 1000, which is one second
        // that product is then multiplied by 60 to equal a minute
        SetTimer("gmxtimer",gmxdelay*1000*60,false);
        new
            string[128]; // A new optional string to format
        format(string,sizeof(string),"The server will restart in %d minute.",gmxdelay);
        SendClientMessageToAll(-1,string);
    }
    return 1;
}
forward gmxtimer();
public gmxtimer() // So once the timer is up, then it....
{
    // sends the gmx command to RCON
    SendRconCommand("gmx");
}
Here is the same example with ZCMD.
pawn Код:
CMD:reload(playerid,params[])
{
    if(!IsPlayerAdmin(playerid)) // If they are not Admin then it sends the message
    {
        return SendClientMessage(playerid,-1,"You are not authorised to use this command.");
    }
    new gmxdelay; // A new variable that the player will type after typing "/reload"
    // This is the line that checks if they didn't type the command correctly.
    if(sscanf(params,"d",gmxdelay))
    {
        // So if they didn't type it correctly then tell them how to use it.
        return SendClientMessage(playerid,-1,"Syntax: /reload <minutes>");
    }
    if(gmxdelay == 0) // If they typed 0
    {
        return SendClientMessage(playerid,-1,"It must be at least one minute.");
    }
    else
    {
        // The "gmxdelay*1000*60" is what converts it to minutes.
        // gmxdelay is multiplied by 1000, which is one second
        // that product is then multiplied by 60 to equal a minute
        SetTimer("gmxtimer",gmxdelay*1000*60,false);
        new
            string[128]; // A new optional string to format
        format(string,sizeof(string),"The server will restart in %d minute.",gmxdelay);
        SendClientMessageToAll(-1,string);
    }
    return 1;
}
forward gmxtimer();
public gmxtimer() // So once the timer is up, then it....
{
    // sends the gmx command to RCON
    SendRconCommand("gmx");
}
Hope I was of any help.
Oh wow, didnt expect help that fast. And Thank you this is alot of help and i kinda get how to use sscanf now. And i was deffinaly doing it wrong lol.

Thanks
Nick
Reply
#7

Just glad I could help.
Reply
#8

Well i am having a issue with the command, when i type /restart with no time it does the "/restart <minutes>" like its supposed to but when i put in a time its a unknown command.

pawn Код:
if(!strcmp(cmdtext,"/restart",true))
    {
        if(!IsPlayerAdmin(playerid))
        {
            return SendClientMessage(playerid, Color_Red,"Only admins can use this command.");
        }
        new gmxdelay;
        if(sscanf(cmdtext,"d",gmxdelay))
        {  
            return SendClientMessage(playerid, Color_Red,"Syntax: /restart <minutes>");
        }
        if(gmxdelay == 0)
        {
            return SendClientMessage(playerid, Color_Blue,"It must be at least one minute.");
        }
        else
        {        
            SetTimer("Timer_Restart_Kick",gmxdelay*1000*60,false);
            new string[128];
            format(string,sizeof(string),"The server will restart in %d minute.",gmxdelay);
            SendClientMessageToAll( Color_Blue,string);
        }
        return 1;
    }
Thats what i have. same as what you have except i changed the timer and the command.
Reply
#9

edit: sorry read that wrong
Reply
#10

Oh my, I see what I did wrong.
I had to put all the code after the else statement.
pawn Код:
if(!strcmp(cmdtext,"/restart",true))
    {
        if(!IsPlayerAdmin(playerid))
        {
            return SendClientMessage(playerid, Color_Red,"Only admins can use this command.");
        }
        new gmxdelay;
        if(sscanf(cmdtext,"d",gmxdelay))
        {
            return SendClientMessage(playerid, Color_Red,"Syntax: /restart <minutes>");
        }
        else
        {
            if(gmxdelay == 0)
            {
                return SendClientMessage(playerid, Color_Blue,"It must be at least one minute.");
            }
            SetTimer("Timer_Restart_Kick",gmxdelay*1000*60,false);
            new string[128];
            format(string,sizeof(string),"The server will restart in %d minutes.",gmxdelay);
            SendClientMessageToAll( Color_Blue,string);
        }
        return 1;
    }
Reply
#11

I don't know if it's really a string, it looks more as an variable, a string should be between quotes.
pawn Код:
new Example[20];
Example = "Example of a string";
//---
new Variable = 1000;
//As you can see it just saves the miliseconds, nothing else.
And you're using the specifier d with sscanf, it's used for integers, not for strings.

Correct me if I'm wrong, I think strings can't be used in this case.

Best regards!
Reply
#12

No, strings can't be used with timers unless you use one of the timer fix scripts such as Y_timers
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)