12.01.2013, 13:35
- Entry words
- System base
- Using
- A few arguments.
ENTRY WORDS
Hi. I heard from some people that using SetTimerEx is not the best solution to make an action with delay(i'll happy if you can explain why SetTimerEx is not good in big gamemodes).
So I decided to make an alternative personal timer system. I'll describe the system without using any sided filterscript, include or something else. You can go deeper and optimize the code as you want.
SYSTEM BASE
Ok, first thing we will do is to declarate two arrays:
Code:
pActionTimer[MAX_PLAYERS]; pAction[MAX_PLAYERS];
Code:
pubic OnPlayerConnect(playerid) { pActionTimer[playerid] = -1; //... code }
Code:
SetPlayerTimerAction(playerid, actionid, seconds) { pActionTimer[playerid] = seconds; pAction[playerid] = actionid; }
Code:
public globalActionTimer() { for (new i = 0; i < MAX_PLAYERS; i++) if (pActionTimer[i] != -1) { pActionTimer[i]--; if (!pActionTimer[i]) { runPlayerAction(i); pActionTimer[i] = -1; } } return 1; }
We need now one more function to run some action.
Code:
#define ACTION_KILLPLAYER 0 #define ACTION_SPAWNPLAYER 1 #define ACTION_SOMEACTION 2 runPlayerAction(playerid) { switch (pAction[playerid]) { case ACTION_KILLPLAYER: SetPlayerHealth(playerid, 0.0); case ACTION_SPAWNPLAYER: SpawnPlayer(playerid); case ACTION_SOMEACTION: myFunction(playerid); } }
Code:
public OnGameModeInit() { ... SetTimer("globalActionTimer", 1000, false); return 1; }
USING
Code:
SetTimerEx("SetPlayerHealth", 3000, false, "if", playerid, 0.0);
Code:
SetPlayerTimerAction(playerid, ACTION_KILLPLAYER, 3000);
A FEW ARGUMENTS
What if you want to set player position after some delay?
Code:
... new Float: x, Float: y, Float: z; GetPlayerPos(playerid,x,y,z); SetPVarInt(playerid, "pPosX", x); SetPVarInt(playerid, "pPosY", y); SetPVarInt(playerid, "pPosZ", z); SetPlayerTimerAction(playerid, ACTION_SETPLAYERPOS, 3000);
Code:
... #define ACTION_SETPLAYERPOS 100 runPlayerAction(playerid) { switch (pAction[playerid]) { ... case ACTION_SETPLAYERPOS: timerSetPlayerPos(playerid); } }
Code:
timerSetPlayerPos(playerid) { new Float: x, Float: y, Float: z; x = GetPVarInt(playerid, "pPosX"); y = GetPVarInt(playerid, "pPosY"); z = GetPVarInt(playerid, "pPosZ"); DeletePVar(playerid, "pPosX"); DeletePVar(playerid, "pPosY"); DeletePVar(playerid, "pPosZ"); SetPlayerPos(playerid, x, y, z); }