Help with Timer -
GeorgeMcReary - 07.03.2017
Hello all. i am trying to make a system where a player can get free gifts at gifts each 24 hours. i added a variable at enum named, pGiftTime. so when a player uses /gift, pGiftTime will be 86400 sec( ie 24 Hours). now how do i reduce each seconds so that when when 24 hours has passed, a player gets message and then he can use /gift again. if he is offline, timer should stop.
Please help me, thanks
Re: Help with Timer -
Toroi - 07.03.2017
Create a simple function to reduce the value of pGiftTime for 1, and create a player timer to reproduce it every second. You can also use the same function to give the gift to the player.
Re: Help with Timer -
GeorgeMcReary - 07.03.2017
Can you please give me an example?
Thanks.
Re: Help with Timer -
DRIFT_HUNTER - 07.03.2017
why dont you use tick count for these? Basically just check if player can use gift when he uses command.
pawn Код:
pGiftTime[playerid] = gettime();//When he gets a gift
if(gettime() > (pGiftTime[playerid] + 86400))
{
//He can use gift, time has passed...
}
//you can also use these to get time remaining (in seconds):
((pGiftTime[playerid] + 86400) - gettime())
Re: Help with Timer -
Toroi - 07.03.2017
Sure:
Код:
forward GiftTaimer(playerid);
public GiftTaimer(playerid)
{
if(PlayerInfo[playerid][pGiftTime] > 0) PlayerInfo[playerid][pGiftTime] --;
else SetPVarInt(playerid,"Claimyourpresent",1);
return 1;
}
public OnPlayerConnect(playerid)
{
SetTimerEx("GiftTaimer",1000,true,"i",playerid);
return 1;
}
Re: Help with Timer -
GeorgeMcReary - 07.03.2017
Quote:
Originally Posted by DRIFT_HUNTER
why dont you use tick count for these? Basically just check if player can use gift when he uses command.
pawn Код:
pGiftTime[playerid] = gettime();//When he gets a gift
if(gettime() > (pGiftTime[playerid] + 86400)) { //He can use gift, time has passed... }
//you can also use these to get time remaining (in seconds): ((pGiftTime[playerid] + 86400) - gettime())
|
Quote:
Originally Posted by Troydere
Sure:
Код:
forward GiftTaimer(playerid);
public GiftTaimer(playerid)
{
if(PlayerInfo[playerid][pGiftTime] > 0) PlayerInfo[playerid][pGiftTime] --;
else SetPVarInt(playerid,"Claimyourpresent",1);
return 1;
}
public OnPlayerConnect(playerid)
{
SetTimerEx("GiftTaimer",1000,true,"i",playerid);
return 1;
}
|
Thanks. also i have one doubt, dont you think server will be laggy if i use timer for every second for every player?
also in /gift cmd, i will use like,
Код:
if(GetPVarInt(playerid, "Claimyourpresent") != 1) return SCM(COLOR_RED, "You cant use /gift yet.");
is that ok?
Thanks
Re: Help with Timer -
Vince - 07.03.2017
This is ridiculous and simply not viable. Since the timers aren't being killed you will create lag that gets progressively worse until the server comes to a complete standstill. At which point the CPU usage will reach 100% and your server will probably crash.
It is also incorrect because playerids are volatile. If a player disconnects another player can take the slot. And then what? Reset the variable? Discarding the stored value for the previous player in the process?
Re: Help with Timer -
Toroi - 07.03.2017
Quote:
Originally Posted by Vince
This is ridiculous and simply not viable. Since the timers aren't being killed you will create lag that gets progressively worse until the server comes to a complete standstill. At which point the CPU usage will reach 100% and your server will probably crash.
It is also incorrect because playerids are volatile. If a player disconnects another player can take the slot. And then what? Reset the variable? Discarding the stored value for the previous player in the process?
|
Quote:
Can you please give me an example?
|
example != write the entire code for me
--
As for OP, I SUPPOSE you're saving the value in your database just like the rest of the player's data, aren't u?
Re: Help with Timer -
GeorgeMcReary - 08.03.2017
Yeah i am saving data in pGiftTime. but if i continue the whole loop for each player untill he is loggged in, dont you think server will lag? how do i prevent it?
Thanks for help
Re: Help with Timer -
GTLS - 08.03.2017
Try Adding Condition
PHP код:
if(PlayerInfo[playerid][pGiftTime] > 0 )
{
SetTimerEx("GiftTaimer",1000,true,"i",playerid);
}
inside OnPlayerConnect or any other call back you want to use.