Help with Timer
#1

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
Reply
#2

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.
Reply
#3

Can you please give me an example?
Thanks.
Reply
#4

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())
Reply
#5

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;
}
Reply
#6

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
Reply
#7

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?
Reply
#8

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?
Reply
#9

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
Reply
#10

Try Adding Condition
PHP код:
 if(PlayerInfo[playerid][pGiftTime] > )
{
   
SetTimerEx("GiftTaimer",1000,true,"i",playerid);

inside OnPlayerConnect or any other call back you want to use.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)