Clock using timer?
#1

Well I was fooling around with ways to make my clock sync to real time and not get thrown off. Then I tried using a timer

Код:
#include <a_samp>

forward clock(playerid);

#if defined FILTERSCRIPT

public OnFilterScriptInit()
{
	print("\n--------------------------------------");
	print(" Clock By Yury");
	print("--------------------------------------\n");
	return 1;
}

public OnFilterScriptExit()
{
	return 1;
}

#else

main()
{
	print("\n----------------------------------");
	print("");
	print("----------------------------------\n");
}

#endif

public OnGameModeInit()
{
	SetTimer("clock",1000,true);

	return 1;
}

public clock(playerid)
{
	new Hour, Minute, Second;
	gettime(Hour, Minute, Second);
	SetPlayerTime(playerid,Hour,Minute);
}
It works/compiles but when I start up the time keeps switching between the time now (21:59) and the previous time (21:5 and it makes the sky look glitchy due to the time changing.

NOTE I have TogglePlayerClock(playerid,1); in the main gamemode script

Is there a way to fix it or will it just not work?
Reply
#2

If this a gamemode or a filterscript?

You should really remove the #if defined FILTERSCRIPT stuff..
Reply
#3

ingame clock moves a minute once per second. so you'd have to update quite frequently since it will change a lot. It's best to just make you're own using textdraws

pawn Код:
new Text:WorldTime;
new Hour,Minute,Second;
forward UpdateWorldTime();
CalculateTime(&hour,&minute,&second)
{
  label:
  if(second>=60){second-=60;minute++;goto label;}
  if(second<=-1){second+=60;minute--;goto label;}
  if(minute>=60){minute-=60;hour++;goto label;}
  if(minute<=-1){minute+=60;hour--;goto label;}
  if(hour>=13){hour-=12;goto label;}
  if(hour<=0){hour+=12;goto label;}
}
//ongamemodeinit or onfilterscriptinit
WorldTime=TextDrawCreate(10,450,"BACON!"); // change the location and the text doesn't really matter
gettime(Hour,Minute,Second);
Second--;
UpdateWorldTime();
SetTimer("UpdateWorldTime",1000,1);
//anywhere outside of a callback
public UpdateWorldTime()
{
  Second++;
  CalculateTime(Hour,Minute,Second);
  new tmpstr[10];
  format(tmpstr,sizeof(tmpstr),"%02d:%02d:%02d",Hour,Minute,Second);
  TextDrawSetString(WorldTime,tmpstr);
}
That should allow you to choose to stay with the server's time, or make you're own by changing the Hour,Minute, and Second variables in a command

Don't forget to TextDrawShowForPlayer(playerid,WorldTime); under OnPlayerConnect


EDIT* You may have noticed about 6 different changes since I first posted this lol, I haven't tested it and I'm correcting / rewriting
EDIT** editted again lol, had a infinite loop
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)