[Tutorial] Time System
#1

This tutorial Is About Making a time system For SAMP Using Text draws
As Timers Which Show The Time of the Day like In Single Player were Removed In Multiplayer This System Might come In Handy

Some Functions You Should know To Fully Understand This Tut:
https://sampwiki.blast.hk/wiki/Category:...ions#Textdraws
https://sampwiki.blast.hk/wiki/SetTimer

First Of All Lets Start With The Includes:
pawn Code:
#include <a_samp>
Next The Defines(You Can use Any Color Defines But Remember To Change The stuff on The functions)
pawn Code:
#define COLOR_GREY 0xAFAFAFAA
#define COLOR_GREEN 0x33AA33AA
#define COLOR_BRIGHTRED 0xFF0000AA
#define COLOR_YELLOW 0xFFFF00AA
#define COLOR_PINK 0xFF66FFAA
#define COLOR_BLUE 0x3A47DEFF
#define COLOR_TAN 0xBDB76BAA
#define COLOR_PURPLE 0x800080AA
#define COLOR_WHITE 0xFFFFFFAA
#define COLOR_LIGHTBLUE 0x33CCFFAA
#define COLOR_ORANGE 0xFF9900AA
#define COLOR_INDIGO 0x1E90FFAA
#define COLOR_BLACK 0x00000000
#define COLOR_DARKGREY 0x696969FF
Next We start With the Actual scripting:
These Are Global Variables
pawn Code:
// new variables
new Text:Servt;                                   //Server Time Text draw Variable
new Tsec;                                         //stores the mins  of time
new THrs;                                         //stores hours of time
Put The Above code At The top Of Your Script

Next We Actually Create Textdraws To show The Time On:
pawn Code:
public OnGameModeInit()
{
    Servt=TextDrawCreate(545, 31, "00:00");
    TextDrawColor(Servt, COLOR_WHITE);
    Tsec= 0;
    THrs= 0;
    SetTimer("TimeU",1000,true);

    return 1;
}
Ok Now To explain That Last Step:
pawn Code:
Servt=TextDrawCreate(545, 31, "00:00");
TextDrawColor(Servt, COLOR_WHITE);
This Part Creates The Text draw Above The Health Bar (You Can Change cords if you like)
The Line After That Sets The Textdraw Color To White
[/PAWN]

pawn Code:
Tsec= 0;
THrs= 0;
This Sets Both The Variables Of Mins And Hrs To 0 This Can be set to whatever time You Want to start at When you Restart Server
pawn Code:
SetTimer("TimeU",1000,true);
This Sets a Timer Which Repeats every 1000 milliseconds That is 1 second

We now have to set up The Timer for This We First Forward the timer
pawn Code:
forward TimeU();
Now We Can Create a Timer
pawn Code:
public TimeU()
{
    new string[7];                              // it makes a variable for a string with size 128
    Tsec+=1;                                      //this adds 1 to the existing time variable
    if(Tsec==60) {                                //this resets the mins to 00 one it reaches 60
        Tsec=00;
        THrs+=1;                                  //this adds 1 to the hours every 60 seconds
    }
    if(THrs==24) {                                //This Checks if time is 24:00
        Tsec=00;                                  //This Sets resets Mins
        THrs=0;                                   //this Resets Hours
    }
    if(Tsec<10) {                                 // This Adds a 0 Before The Mins Display For it To look like an actual clock
                                                  //formats string
        format(string,sizeof(string),"%d:%d0",THrs,Tsec);

    }
    if(Tsec>10) {                                 // This Removes a 0 Before The Mins Display after it has reached 10
                                                  //formats string
        format(string,sizeof(string),"%d:%d",THrs,Tsec);

    }
    if(THrs<10) {
                                                  //formats string
        format(string,sizeof(string),"0%d:%d",THrs,Tsec);

    }
    for(new i; i<MAX_PLAYERS; i++) {              //loops through all players
        if(IsPlayerConnected(i)) {                //checks if the player is connected
            SetPlayerTime(i,THrs,Tsec);           //sets All players time
        }
    }
    TextDrawSetString(Servt,string);              //updates the textdraw

}
Oh And Finally To Show The Textdraws
pawn Code:
Public OnPlayerSpawn(playerid)
{
TextDrawShowForPlayer(playerid,Servt);

return 1;
}
Note:That This Time system Takes 1 hour As one Min And 1second As 1min
Hope This Helps You This Is My First Tutorial
Reply
#2

Aw, you put it in the wrong section. Surely a administrator will move this :P

Good job
Reply
#3

Thanks Lorenc_ how silly of me can some admin please move it?
Reply
#4

helpful tutorial anyways
Reply
#5

Wrong section but good tutorial.
Reply
#6

WILL everybody on server have same time or?
Reply
#7

Nice, but exists %02d.

pawn Code:
format(string,sizeof(string),"%02d:%02d",THrs,Tsec);
Reply
#8

Good Job.
Reply
#9

Usefull thanks. Great job.
Reply
#10

pawn Code:
if(Tsec<10) {                                 // This Adds a 0 Before The Mins Display For it To look like an actual clock
                                                  //formats string
        format(string,sizeof(string),"%d:%d0",THrs,Tsec);

    }
    if(Tsec>10) {                                 // This Removes a 0 Before The Mins Display after it has reached 10
                                                  //formats string
        format(string,sizeof(string),"%d:%d",THrs,Tsec);

    }
    if(THrs<10) {
                                                  //formats string
        format(string,sizeof(string),"0%d:%d",THrs,Tsec);

    }
This can be done with an easier way.
Change the '%d' to '%02d'. This will show the 0 in front of the (1-9) in the minutes, aswell as the hours.
Reply
#11

Quote:
Originally Posted by System64
View Post
WILL everybody on server have same time or?
yes Everyone will Have The same Time
Quote:

This can be done with an easier way.
Change the '%d' to '%02d'. This will show the 0 in front of the (1-9) in the minutes, aswell as the hours.

Lol I Never knew about That Thanks
Reply
#12

There's already one ( https://sampwiki.blast.hk/wiki/TogglePlayerClock )

Nice script though, I used to create a script like this when I didn't know what is %02d.
Just sharing it here ... =D

pawn Code:
#define     FILTERSCRIPT

#include    < a_samp >

new
    Text:   td_SAClock[ 2 ],
            currentMinutes,
            currentHours,
            clockTimer
;

forward SAClock_Update( );
public  SAClock_Update( )
{
    static
        hoursOutput[ 4 ], // 3chars + NULL
        minutesOutput[ 3 ] // 2chars + NULL
    ;

    if ( 0 <= currentMinutes <= 58 )
        currentMinutes ++;
    else
        currentHours = ( currentHours == 23 ) ? 0 : currentHours + 1, currentMinutes = 0;
       
    if ( 0 <= currentMinutes <= 9 )
        format( minutesOutput, 3, "0%d", currentMinutes );
    else
        format( minutesOutput, 3, "%d", currentMinutes );

    if ( 0 <= currentHours <= 9 )
        format( hoursOutput, 4, "0%d:", currentHours );
    else
        format( hoursOutput, 4, "%d:", currentHours );
       
    TextDrawSetString( td_SAClock[ 0 ], hoursOutput );
    TextDrawSetString( td_SAClock[ 1 ], minutesOutput );
   
    for ( new slots = GetMaxPlayers( ), i; i < slots; i ++ )
    {
        if ( !IsPlayerConnected( i ) || IsPlayerNPC( i ) )
            continue;

        SetPlayerTime( i, currentHours, currentMinutes );
    }
       
    return 1;
}

public OnFilterScriptInit( )
{
    td_SAClock[ 0 ] = TextDrawCreate( 547.000000, 36.000000, "00" );
    TextDrawBackgroundColor( td_SAClock[ 0 ], 255 );
    TextDrawFont( td_SAClock[ 0 ], 1 );
    TextDrawLetterSize( td_SAClock[ 0 ], 0.509999, 1.800000 );
    TextDrawColor( td_SAClock[ 0 ], -1 );
    TextDrawSetOutline( td_SAClock[ 0 ], 0 );
    TextDrawSetProportional( td_SAClock[ 0 ], 1 );
    TextDrawSetShadow( td_SAClock[ 0 ], 1 );

    td_SAClock[ 1 ] = TextDrawCreate( 578.000000, 36.000000, ":00" );
    TextDrawBackgroundColor( td_SAClock[ 1 ], 255 );
    TextDrawFont( td_SAClock[ 1 ], 1 );
    TextDrawLetterSize( td_SAClock[ 1 ], 0.509999, 1.800000 );
    TextDrawColor( td_SAClock[ 1 ], -1 );
    TextDrawSetOutline( td_SAClock[ 1 ], 0 );
    TextDrawSetProportional( td_SAClock[ 1 ], 1 );
    TextDrawSetShadow( td_SAClock[ 1 ], 1 );

    clockTimer  = SetTimer( "SAClock_Update", 1000, true );
   
    currentMinutes =   0;
    currentHours   =   0;
   
    return 1;
}

public OnFilterScriptExit( )
{
    TextDrawHideForAll( td_SAClock[ 0 ] );
    TextDrawHideForAll( td_SAClock[ 1 ] );
   
    KillTimer( clockTimer );
   
    currentMinutes  = -1;
    currentHours    = -1;
   
    return 1;
}

public OnPlayerConnect( playerid )
{
    TextDrawShowForPlayer( playerid, td_SAClock[ 0 ] );
    TextDrawShowForPlayer( playerid, td_SAClock[ 1 ] );
   
    return 1;
}
( My code is more complex though )

I'll rate 7.0 in explaining, and I think 128 is too enough for the string, because it's like " 23:54 " which is only 5 chars, I'll create
pawn Code:
new string[ 7 ];
Instead of 128.
Reply
#13

Quote:

I'll create
pawn Code:
new string[ 7 ];

Instead of 128.

Ill fix That
Quote:

( My code is more complex though )

Well I Made It Simple as Some People May Not Understand complex codes
Quote:

I'll rate 7.0 in explaining,

Thanks
Reply
#14

not god, when you connect to server, first it goes (for example)
12:10, 12:20, 12:30, 12:40, 12:50, 12:60, 12:70, 12:80, 12:90, than gores 12:1, 12.2...
Reply
#15

strange worked fine for me before lol
Give me a moment ill sheck it out maybe a bug caused due to type errors
Reply
#16

um system whose tut are u talking about lol mine works perfectly if you have any doubt Try it Or send me any Warning messages you get or maybe a screenie
Reply
#17

No problem, I'll try to fix it
Reply
#18

I improved system, this is realy realy bad, this isn't hours, this is minutes and seconds, it's realy unclean...

I'll make FS and than release (Also give you some credits )
Reply
#19

Quote:

Note:That This Time system Takes 1 hour As one Min And 1second As 1min

It Was supposed to be minutes and seconds
Reply
#20

Quote:
Originally Posted by AceFlyer
View Post
It Was supposed to be minutes and seconds
Yeah, but isn't it better with hours:minuteseconds?
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)