Time delay on commands
#1

Hi there,

I was just wondering if this is the best way to put a time delay on a command:

Код:
forward AllowCommand(playerid);
new StopCommand[MAX_PLAYERS]; //at the top of the script

public OnPlayerCommandText(playerid, cmdtext[])
{
	if (strcmp("/mycommand", cmdtext, true) == 0)
	{
        if(StopCommand != 1)
        {
		     // Do something here

             StopCommand[playerid] = 1;
             SetTimerEx("AllowCommand", 1000, 0, "i", playerid);
             return 1;
        }
	}
	return 0;
}

public AllowCommand(playerid)
{
    StopCommand[playerid] = 0;
}
Or is there a better way? If not then what's the best way to go about putting a time delay on multiple commands using this method without declaring an array (StopCommand[MAX_PLAYERS] in my example) for every single command you have

Many thanks.
Reply
#2

Could remove the timers and use GetTickCount.

pawn Код:
// Global Variable
new CommandTimeStamp[ MAX_PLAYERS ];

// OnPlayerConnect
CommandTimeStamp[ MAX_PLAYERS ] = 0;

// OnPlayerCommandText
if ( !strcmp( "/mycommand", cmdtext, true ) )
{
  if ( ( GetTickCount() - CommandTimeStamp[ playerid ] ) < 10000 ) // If difference in time is less than 10000 ms (10 seconds)
    return SendClientMessage( playerid, "You must wait 10 seconds before using this command again." );
  else
  {
    CommandTimeStamp[ playerid ] = GetTickCount( );
   
    // Your code!
  }
}
Reply
#3

Thanks for the reply. That's actually how I was doing it before I found out that you could pass parameters using SetTimerEx. Even with this method you need to declare several arrays for your different commands, and I'm just not sure if that's a good method... I also read that GetTickCount()'s not very stable and tickcount() expires after 24 days of non-stop playback (although that's still a hell of a long time!).
Reply
#4

Then use gettime instead.
Reply
#5

Hmm. I'm not really short of methods to put the delay in. I just want to know if anyone can think of a way to do it without declaring an array for every command that I want to put a delay in for...

Edit: On a side note: gettime actually looks very useful, thanks for that.
Reply
#6

You don't really have the choice, but you could use a 2d array.

Also hi Simon, a mistake in your code, where it say
pawn Код:
// OnPlayerConnect
CommandTimeStamp[ MAX_PLAYERS ] = 0;
should be playerid
Reply
#7

I don't know a lot about 2nd arrays but I will read up on them. Just one more question: are 2nd arrays more efficient than normal arrays? So would it be better to declare a 2nd array instead of several normal arrays? Thanks.
Reply
#8

No it's not more efficient. But you still can do with only one array, like in this example:
pawn Код:
enum
{
  CMD_MYCOMMAND,
  CMD_OTHERCMD,
   
  SIZEOF_DELAYS_ENUM
};

new PlayerDelays[MAX_PLAYERS * SIZEOF_DELAYS_ENUM];
#define PlayerDelay(%0,%1) PlayerDelays[%0+(%1*MAX_PLAYERS)]
Then you can do
pawn Код:
PlayerDelay(playerid, CMD_MYCOMMAND) = gettime();
or whatever.


A more efficient way than constantly using gettime(), is to increment a global variable in a repeating timer of 1 second and use this variable instead.
Reply
#9

Ah that's great. Thanks a lot for the help. About this bit:

Quote:

increment a global variable in a repeating timer of 1 second and use this variable instead.

I'm not 100% sure what you mean here...something like this?

Код:
new TickCount;
forward RaiseCount();

public OnGameModeInIt()
{
    SetTimer("RaiseCount", 1000, 1);
}

public RaiseCount()
{
    TickCount++;
}
And then use the TickCount variable in my commands?
Reply
#10

Yes exactly
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)