Function to run on each hour
#1

Hi all,

I have a function running every hour, that's fine, using a timer. However, it's not quite what I want. I want the function to execute on each hour (E.g. 14:00, 15:00, 16:00, 17:00, etc.). However, at the moment it runs hours but from the time that the server is started.

How would I go about getting the result I want from this?

Thanks
Reply
#2

Create a timer that checks once a second (or use an existing one) save current hour to a variable with gettime() then call your function if the hour is changed.
Reply
#3

Something like that?

public SyncTime()
{
new tmphour;
new tmpminute;
new tmpsecond;
gettime(tmphour, tmpminute, tmpsecond);
FixHour(tmphour);
tmphour = shifthour;
if ((tmphour > ghour) || (tmphour == 0 && ghour == 23))
{
ghour = tmphour;
if (realtime)
{
SetWorldTime(tmphour);
}
}
return 1;
}

And use SyncTime(); to another timer.
Reply
#4

Quote:
Originally Posted by blackeagle1122
Посмотреть сообщение
Create a timer that checks once a second (or use an existing one) save current hour to a variable with gettime() then call your function if the hour is changed.
I don't particularly want to have a timer that's running a function every second in all honesty, it's only going to slow things down.

Quote:
Originally Posted by Nin9r
Посмотреть сообщение
Something like that?

public SyncTime()
{
new tmphour;
new tmpminute;
new tmpsecond;
gettime(tmphour, tmpminute, tmpsecond);
FixHour(tmphour);
tmphour = shifthour;
if ((tmphour > ghour) || (tmphour == 0 && ghour == 23))
{
ghour = tmphour;
if (realtime)
{
SetWorldTime(tmphour);
}
}
return 1;
}

And use SyncTime(); to another timer.
Sorry but that doesn't really explain much to me. Where is ghour coming from? WHere did FixHour() come from? Where did shift hour come from?

Thanks
Reply
#5

Quote:
Originally Posted by Jonesy96
Посмотреть сообщение
I don't particularly want to have a timer that's running a function every second in all honesty, it's only going to slow things down.
If your only syncing time it wont slow things down. There are no complex calculations or slow code involved. Easiest way would be to use a timer, set it to a time you want your accuracy to be within. EG. If you need it to be accurate to within 1 second you will need to use a maximum 1 second timer.

Only other alternative i can think of is OnPlayerUpdate (which will be worse performance-wise than using a timer).

Timer duration depends on level of accuracy you need.

You could make a system that checks every 5 min or so, then when it gets to within 5 min start a 1 second timer (not worth the effort). I wouldn't bother though because calling gettime() once per second will be completely unnoticeable.
Reply
#6

You could also convert the amount of seconds until the end of the hour into milliseconds and use it as a timer value. Just note that timers can be inaccurate.
Reply
#7

Create a timer to run every minute and check if the minutes are equal to 0. If minutes are equal to 0, that means a new hour began.

Код:
SetTimer("Function1",60000,true);

public Function1()
{
	new hours,minutes,seconds;
	gettime(hours,minutes,seconds);
	if(minutes==0)
	{
	    // New hour began
	}
	return 1;
}
Reply
#8

Thanks very much for the input guys. Running a function every minute, comparing whether minutes = 0, seems probably the best solution for me.

Thanks a lot.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)