Timers
#1

Alright, I've been going through with timers a couple of times now the past two days and I do not understand them fully. I have read about the timers on samp wiki many times and I never fully understand them so please do not link me to them, I know where I can find them.

So my problem is, I am making an ajail command. Wouldn't be so hard, right? Well for me it was easy until I got to the last part where I had to put a timer. I have noticed that you may only save a time variable (interval in a timer) only after or before the timer is going on. Once it is going, you cannot pause it and save the interval sadly. So I downloaded a couple of gamemodes from the section on these forums and either they didn't have a timer or they made me even more confused.

Basically my questions or problems are:
1. Can you pause a SetTimer or SetTimerEx, so that you can save the interval into a variable. Why saving them? Because if a player disconnects I want the player to continue his ajail once he spawns again.

Right now my command saves the time from /ajail command into a ini file BEFORE the timer starts. Therefore if the player disconnects he has to complete the whole ajail again even though he spent 90% of the time.

2. The difference between SetTimerEx and SetTimer. I know that some people have answered me that question before but then I have a follow up question. What can you do with the two last parameters in SetTimerEx? Example:
Code:
SetTimerEx("mafunction", 5000, false, "i", x);
What can you do with the variable "x"? Like what's the purpose of x?


Really helpful for those helping me, also don't give me the code of /ajail. I wanna understand the problems so I don't have to ask the same thing in 2 weeks when I do something else with SetTimer or SetTimerEx...
Reply
#2

1."i" - I stands for integer (whole number). We are passing an integer (a player ID) to the function.
2. playerid - The value to pass. This is the integer specified in the previous parameter.
You can put playerid instead of x..
and you can make under
pawn Code:
public OnPlayerDisconnect...
      KillTimer(details)
and as i have scripted a gm from scratch and it's just it's not necessary to put timer you just will put like:
to put the playerid in jail,time....
Reply
#3

Yes but how could I possibly make the script know that time is the time being spent in the jail and then after that I just release him? The script wouldn't know that with time I meant actual minutes or seconds. It's all just confusing man. But thanks for explaining the things above at least.

Let's say that I don't have a timer then, what else can I use to count actual seconds or well miliseconds?
Reply
#4

It's really not that hard.

There are 2 ways you can use timers here:
Global timer OR Player timer.

Global timer = SetTimer(...)
Player timer = SetTimerEx(parameters)

In a global timer check if player is jailed (if variable is not zero), if yes -> re jail him until the values goes to zero or less

In a player timer, do the same thing (check if not zero or less) if yes, kill his private timer so no need to check again

1) All you need to have is a variable that holds the minutes/second he has to be in jail.
(I'll be using seconds here and a private timer)
pawn Code:
new jailSeconds[MAX_PLAYERS], jailTimer[MAX_PLAYERS];

forward PlayerJailTimer(playerid); // you must forward the timer

// @Command - target is the guy you want to jail
jailSeconds[target] = 60_000; /* -> 1 full minute */
JailPlayerId(playerid); /* -> creates a timer for this specific playerid */

// @OnPlayerDisconnect - make sure you saved the variable to a file/database
public OnPlayerDisconnect(playerid)
{
     SavePlayer(playerid);
     UnjailPlayerId(playerid); // kill the timer so wont mix with another player
     return 1;
}

// @OnPlayerConnect - after connecting to the server and getting the player variable -> passing login dialog
public OnPlayerDisconnect(playerid)
{
     // jailSeconds[playerid] = value saved into the file/database
     if(IsPlayerJailed(playerid))
     {
          JailPlayerId(playerid); /* -> creates a timer for this specific playerid */
          SendClientMessage(playerid, -1, "Returning to jail!");
     }
     return 1;
}

JailPlayerId(playerid)
     jailTimer[playerid] = SetTimerEx("PlayerJailTimer", 1000, true, "i", playerid); /* -> creates a timer for this specific playerid */

UnjailPlayerId(playerid)
     return KillTimer(jailTimer[playerid]);

IsPlayerJailed(playerid)
     return (jailSeconds[playerid] > 0);

public PlayerJailTimer(playerid)
{
     if(IsPlayerJailed(playerid))
     {
          SetPlayerPos(...);
          // ... etc
          jailSeconds[playerid] --;
     }
     else
     {
          UnjailPlayerId(playerid)
     }
     return 1;
}
2) SetTimerEx takes parameters with specific values
SetTimer doesn't take any parameters -> global timer to check stuff

All of that might be a little off, I hope you get the general idea however.
Reply
#5

A couple of side-questions, what does these mean?
jailSeconds[playerid] --; (What does the -- do?)
jailSeconds[target] = 60_000; (What does it mean to have 60 underscore 000?)

And, first you make jailSeconds to an array. But then you change it to a minute? That is what I don't understand. :S
Reply
#6

1) Decrease variable by 1
same as jailSeconds[playerid] = jailSeconds[playerid] - 1;

2) 60000 = 60_000 -> i was getting 60 seconds > 1000 * 60 (timer repeats itself every 1 sec -> 1000 miliseconds)

jailSeconds simply holds the seconds so I convert the seconds to minutes to use that (1000 miliseconds (1 sec) * 60 = 1 minute)
Reply
#7

Yes got it working! Thank you a lot Loot for taking the time to answer! And you too iBots for just replying and explaining briefly!

I fully understand now how you have to think when it comes to jailing someone. Basically make a timer loop and decrease by 1 second and kill the timer if the time reaches zero basically.

Thanks guys! Giving you a cookie as fast as I get 24 hours back (reputation wait time).
Reply
#8

No problem
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)