[Tutorial] How to make a global Timescript
#1

Global Time
Hey! This is my second tutorial.

This time it's about making a simple clock,
i saw a guy who had problems with it, and i guess others have too / will have at some point.
This is how it looks.

upload photo
pawn Code:
#include <a_samp>
new Hour, Minute, timestring[6];//The time converted into a string for the textdraw.
new Text:timetext;//The textdraw
new Ztimer;//Timer ID.
This is in the top of the script.
Hour is for the amount of hours passed, obviously. (Real world Minutes)
Minute is the amount of minutes passed ingame (Real world Seconds)
timetext is the clock textdraw.
Ztimer is the ID for the timer, incase you want to kill the timer you need to assign an ID to it.
-----


pawn Code:
public OnFilterScriptInit()
{
Hour = 12;
Minute = 00;
Ztimer = SetTimer("timeupdate",1000,true);
////////////////////////////////////////////////////////////////////////////////
timetext = TextDrawCreate(547.000000, 23.000000, "00:00");
TextDrawBackgroundColor(timetext, 255);
TextDrawFont(timetext, 3);
TextDrawLetterSize(timetext, 0.599999, 2.100000);
TextDrawColor(timetext, -1);
TextDrawSetOutline(timetext, 1);
TextDrawSetProportional(timetext, 1);
////////////////////////////////////////////////////////////////////////////////
print("\n--------------------------------------");
print(" FzTime by Fusez successfully loaded!");
print(" Time (1 real minute = 1 second ingame)");
print("--------------------------------------\n");
return 1;
}
When the script starts it sets Hours to 12 and Minutes to 00. So the time is now 12:00.
Ztimer = SetTimer This starts the timer, assigns it to "Ztimer" and sets it to repeat, 1 time each second.
The rest is the textdraw itself (the clock).
And last is the console printmessage.
-----


pawn Code:
public OnFilterScriptExit()
{
    TextDrawDestroy(timetext);
    KillTimer(Ztimer);
    return 1;
}
If you decide to unload the time FS this will destroy the textdraw, and kill the timer. (Ztimer)
-----


pawn Code:
forward timeupdate();
public timeupdate()
{
    Minute += 01;
    if(Minute == 60 && Hour < 24) Hour += 01, Minute = 00;
    if(Hour == 24 && Minute == 00) Hour = 00, Minute = 00;
    format(timestring, sizeof(timestring), "%02d:%02d", Hour, Minute);
    TextDrawSetString(timetext,timestring);
    TextDrawShowForAll(timetext);
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        SetPlayerTime(i,Hour,Minute);
    }
}
pawn Code:
Minute += 1;
This is what the script will perform 1 time each second.
First of it adds 1 minute each second.
-----


pawn Code:
if(Hour == 24 && Minute == 00) Hour = 00, Minute = 00;
Then it checks if the amount of Minutes is 60 and hours are less than 24.
(If the hours are 24, it goes to the next line)
If this is correct, it adds an hour, and sets minutes to 0.
-----
pawn Code:
if(Hour == 24 && Minute == 00) Hour = 00, Minute = 00;
If hours is equal to 24 and minutes 00 It resets them both to 00. (New day)
-----


pawn Code:
format(timestring, sizeof(timestring), "%02d:%02d", Hour, Minute);
(Thanks to vince)
This line formats a string, the string to be shown to the players.
It adds a 0 infront of Minutes Or hours; If any or both of them are less than 10 (2 characters)
Otherwise it will look like "2:54" instead of "02:54".
Example: 20:43
-----


pawn Code:
TextDrawSetString(timetext,timestring);
TextDrawShowForAll(timetext);
This sets the textdraw to show the formated string shown above.
And then Show the clock for all.
-----


pawn Code:
for(new i = 0; i < MAX_PLAYERS; i++)
{
    SetPlayerTime(i,Hour,Minute);
}
This sets every player's time to the server time. (It's a loop) It sets the playertime to the Hours and Minutes shown on the clock.
I know most of you probably dont need a tutorial for this, but some do
FzTime.amx
FzTime.pwn

Good luck// Fusez
Reply
#2

This is what i was in need of.. good tut. Helpful for me.. thank you . +rep
Reply
#3

Like singel game's, good.
Reply
#4

Quote:
Originally Posted by RedFusion
View Post
pawn Code:
if(Hour < 10 && Minute < 10) format(timestring,sizeof(timestring),"0%i:0%i",Hour,Minute);
if(Hour < 10 && Minute > 9) format(timestring,sizeof(timestring),"0%i:%i",Hour,Minute);
if(Hour > 9 && Minute < 10) format(timestring,sizeof(timestring),"%i:0%i",Hour,Minute);
else if(Hour > 9 && Minute > 9) format(timestring,sizeof(timestring),"%i:%i",Hour,Minute);
These lines formats a string, the string to be shown to the players.
The 3 first lines adds a 0 infront of Minutes Or hours; If any or both of them are less than 10 (2 characters)
Otherwise it will look like "2:54" instead of "02:54".
The last line checks if both Minutes and Hours are greater than 9 (2 characters each) If this is the case, no 0 is added infront of any of them. Example: 20:43
-----
Oh god, why? At least, when writing a tutorial do your research.

pawn Code:
format(timestring, sizeof(timestring), "%02d:%02d", Hour, Minute);
This is all you need. Makes sure that the number is 2 digits long and will pad it with zeros if it's not.
Reply
#5

O Didn't know, edited. Thanks.
Reply
#6

Links not working
Reply
#7

Quote:
Originally Posted by MoDee
View Post
Links not working
Try now.
Reply
#8

Thanks
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)