SA-MP Forums Archive
[Tutorial] How to make a global Timescript - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Tutorials (https://sampforum.blast.hk/forumdisplay.php?fid=70)
+---- Thread: [Tutorial] How to make a global Timescript (/showthread.php?tid=367473)



How to make a global Timescript - RedFusion - 10.08.2012

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



Re: How to make a global Timescript - Jarnu - 10.08.2012

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


Re: How to make a global Timescript - Matz - 10.08.2012

Like singel game's, good.


Re: How to make a global Timescript - Vince - 10.08.2012

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.


Re: How to make a global Timescript - RedFusion - 10.08.2012

O Didn't know, edited. Thanks.


Re: How to make a global Timescript - MoDee - 24.08.2012

Links not working


Re: How to make a global Timescript - RedFusion - 25.08.2012

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


Re: How to make a global Timescript - MoDee - 25.08.2012

Thanks