SA-MP Forums

SA-MP Forums (https://forum.sa-mp.com/index.php)
-   Scripting Help (https://forum.sa-mp.com/forumdisplay.php?f=12)
-   -   Make the world time the same for everyone (https://forum.sa-mp.com/showthread.php?t=671885)

Proxus 17/01/2020 03:42 PM

Make the world time the same for everyone
 
Hi there, I'm currently trying to make the world time the same for all players (to add a more realistic effect to the server). As of now, I'm fairly sure that the world time is different for each player.

How could I get it to keep the world time for each player the same?

tekass 17/01/2020 03:58 PM

Re: Make the world time the same for everyone
 
i think this can help you:

https://pastebin.com/Yvivm9T7

DomagojSellug 17/01/2020 04:15 PM

Re: Make the world time the same for everyone
 
pawn Code:
// Somewhere in your script;
new hour, minute, second;

public OnGameModeInit(){
    gettime(hour, minute, second); // get current server time
    SetTimer("updateWorldTime", 360000, true); // set a timer every hour
}
forward updateWorldTime();
public updateWorldTime(){
    gettime(hour, minute, second); // get current server time every hour so it's updated
}

OnPlayerConnect(playerid){
    SetPlayerTime(playerid, hour, 0); // when player connects, it sets their time to the server time automatically which is updated every hour
}

That's the simplest I could write here, hope it helps!

FunnyBear 17/01/2020 04:19 PM

Re: Make the world time the same for everyone
 
Put this together quickly

pawn Code:
new Text:td_time, Text:td_day, g_time = 0, g_day = 0;

#define MINUTE  1800
//define your minute in milliseconds (1.8 seconds in a game minute)

//OnGameModeInit

td_time = TextDrawCreate(577.000000, 20.000000, "00:00");
TextDrawFont(td_time, 3);
TextDrawLetterSize(td_time, 0.554166, 2.449999);
TextDrawTextSize(td_time, 400.000000, 17.000000);
TextDrawSetOutline(td_time, 2);
TextDrawSetShadow(td_time, 0);
TextDrawAlignment(td_time, 2);
TextDrawColor(td_time, -1);
TextDrawBackgroundColor(td_time, 255);
TextDrawBoxColor(td_time, 50);
TextDrawUseBox(td_time, 0);
TextDrawSetProportional(td_time, 1);
TextDrawSetSelectable(td_time, 0);

td_day = TextDrawCreate(577.000000, 8.000000, "_");
TextDrawFont(td_day, 3);
TextDrawLetterSize(td_day, 0.266665, 1.299998);
TextDrawTextSize(td_day, 400.000000, 17.000000);
TextDrawSetOutline(td_day, 2);
TextDrawSetShadow(td_day, 0);
TextDrawAlignment(td_day, 2);
TextDrawColor(td_day, -1);
TextDrawBackgroundColor(td_day, 255);
TextDrawBoxColor(td_day, 50);
TextDrawUseBox(td_day, 0);
TextDrawSetProportional(td_day, 1);
TextDrawSetSelectable(td_day, 0);

SetTimer("Timer", MINUTE, 1); //initiate the timer


//OnPlayerConnect
TogglePlayerClock(playerid, 0); //disables default server clock so you can set your own


//somewhere in your script
stock GetDayName()
{
    new dayname[9];
    switch(g_day)
    {
        case 0: format(dayname, sizeof(dayname), "Monday");
        case 1: format(dayname, sizeof(dayname), "Tuesday");
        case 2: format(dayname, sizeof(dayname), "Wednesday");
        case 3: format(dayname, sizeof(dayname), "Thursday");
        case 4: format(dayname, sizeof(dayname), "Friday");
        case 5: format(dayname, sizeof(dayname), "Saturday");
        case 6: format(dayname, sizeof(dayname), "Sunday");
    }
    return dayname;
}

forward Timer();
public Timer()
{
    g_time ++;
    for (new i=0; i<MAX_PLAYERS; i++)
    {
        new
            str[20],
            gseconds = g_time % 60,
            gminutes = (g_time - gseconds) / 60;

        format(str, sizeof (str), "%02d:%02d", gminutes, gseconds);
        TextDrawSetString(td_time, str);
        TextDrawShowForPlayer(i, td_time);

        format(str, sizeof (str), "%s", GetDayName());
        TextDrawSetString(td_day, str);
        TextDrawShowForPlayer(i, td_day);

        if(g_day == 6 && g_time == 1440) //if week is over
        {
            g_day = 0, g_time = 0;
        }
        else if(g_time == 1440) //if day is over
        {
            g_day ++, g_time = 0;
            format(str, sizeof (str), "00:00");
            TextDrawSetString(td_time, str);
        }
        SetPlayerTime(i, gminutes, gseconds); //set global time (darkness changes depending on the hour)
    }
    return 1;
}

NaS 17/01/2020 05:53 PM

Re: Make the world time the same for everyone
 
Quote:

Originally Posted by FunnyBear (Post 4128533)
Put this together quickly

pawn Code:
new Text:td_time, Text:td_day, g_time = 0, g_day = 0;

#define MINUTE  1800
//define your minute in milliseconds (1.8 seconds in a game minute)

//OnGameModeInit

td_time = TextDrawCreate(577.000000, 20.000000, "00:00");
TextDrawFont(td_time, 3);
TextDrawLetterSize(td_time, 0.554166, 2.449999);
TextDrawTextSize(td_time, 400.000000, 17.000000);
TextDrawSetOutline(td_time, 2);
TextDrawSetShadow(td_time, 0);
TextDrawAlignment(td_time, 2);
TextDrawColor(td_time, -1);
TextDrawBackgroundColor(td_time, 255);
TextDrawBoxColor(td_time, 50);
TextDrawUseBox(td_time, 0);
TextDrawSetProportional(td_time, 1);
TextDrawSetSelectable(td_time, 0);

td_day = TextDrawCreate(577.000000, 8.000000, "_");
TextDrawFont(td_day, 3);
TextDrawLetterSize(td_day, 0.266665, 1.299998);
TextDrawTextSize(td_day, 400.000000, 17.000000);
TextDrawSetOutline(td_day, 2);
TextDrawSetShadow(td_day, 0);
TextDrawAlignment(td_day, 2);
TextDrawColor(td_day, -1);
TextDrawBackgroundColor(td_day, 255);
TextDrawBoxColor(td_day, 50);
TextDrawUseBox(td_day, 0);
TextDrawSetProportional(td_day, 1);
TextDrawSetSelectable(td_day, 0);

SetTimer("Timer", MINUTE, 1); //initiate the timer


//OnPlayerConnect
TogglePlayerClock(playerid, 0); //disables default server clock so you can set your own


//somewhere in your script
stock GetDayName()
{
    new dayname[9];
    switch(g_day)
    {
        case 0: format(dayname, sizeof(dayname), "Monday");
        case 1: format(dayname, sizeof(dayname), "Tuesday");
        case 2: format(dayname, sizeof(dayname), "Wednesday");
        case 3: format(dayname, sizeof(dayname), "Thursday");
        case 4: format(dayname, sizeof(dayname), "Friday");
        case 5: format(dayname, sizeof(dayname), "Saturday");
        case 6: format(dayname, sizeof(dayname), "Sunday");
    }
    return dayname;
}

forward Timer();
public Timer()
{
    g_time ++;
    for (new i=0; i<MAX_PLAYERS; i++)
    {
        new
            str[20],
            gseconds = g_time % 60,
            gminutes = (g_time - gseconds) / 60;

        format(str, sizeof (str), "%02d:%02d", gminutes, gseconds);
        TextDrawSetString(td_time, str);
        TextDrawShowForPlayer(i, td_time);

        format(str, sizeof (str), "%s", GetDayName());
        TextDrawSetString(td_day, str);
        TextDrawShowForPlayer(i, td_day);

        if(g_day == 6 && g_time == 1440) //if week is over
        {
            g_day = 0, g_time = 0;
        }
        else if(g_time == 1440) //if day is over
        {
            g_day ++, g_time = 0;
            format(str, sizeof (str), "00:00");
            TextDrawSetString(td_time, str);
        }
        SetPlayerTime(i, gminutes, gseconds); //set global time (darkness changes depending on the hour)
    }
    return 1;
}

You can optimize the timer a lot by moving everything out of the for loop except SetPlayerTime. You are reformatting a string with the exact same text 1000 times in a row basically, and sending TextDraw updates 1000 times to all players.
Instead of TextDrawShowForPlayer you can also just use TextDrawShowForAll, or do that once in OnPlayerConnect.

Proxus 17/01/2020 06:34 PM

Re: Make the world time the same for everyone
 
Quote:

Originally Posted by DomagojSellug (Post 4128532)
pawn Code:
// Somewhere in your script;
new hour, minute, second;

public OnGameModeInit(){
    gettime(hour, minute, second); // get current server time
    SetTimer("updateWorldTime", 360000, true); // set a timer every hour
}
forward updateWorldTime();
public updateWorldTime(){
    gettime(hour, minute, second); // get current server time every hour so it's updated
}

OnPlayerConnect(playerid){
    SetPlayerTime(playerid, hour, 0); // when player connects, it sets their time to the server time automatically which is updated every hour
}

That's the simplest I could write here, hope it helps!

Isn't gettime() for getting the server time? I didn't mean real time, but ingame time (that changes every 1 second, I'm using the TogglePlayerClock one).

DomagojSellug 17/01/2020 06:57 PM

Re: Make the world time the same for everyone
 
Quote:

Originally Posted by Proxus (Post 4128551)
Isn't gettime() for getting the server time? I didn't mean real time, but ingame time (that changes every 1 second, I'm using the TogglePlayerClock one).

Ooh, my bad, I didn't know that's what you meant lol. Well, you can try to set a timer that repeats itself and every second it'll gettime() and set the time for every player, but that wouldn't be good for optimization. Hopefully someone else will have a better solution. ;)

PPC23 17/01/2020 07:33 PM

Re: Make the world time the same for everyone
 
https://wiki.sa-mp.com/wiki/TogglePlayerClock
Note: Time is not synced with other players! Time can be synced using SetPlayerTime.

Proxus 17/01/2020 07:35 PM

Re: Make the world time the same for everyone
 
Quote:

Originally Posted by PPC23 (Post 4128560)
https://wiki.sa-mp.com/wiki/TogglePlayerClock
Note: Time is not synced with other players! Time can be synced using SetPlayerTime.

That's the point of why I made the topic though. No need to make obvious posts.

PPC23 17/01/2020 07:52 PM

Re: Make the world time the same for everyone
 
Minutes doesn't change the sky, they change only the clock. If the time is 10:00 or 10:59 - it will be the same. The sky will change at 11, etc..

You can use this to set the hour and minutes for all the players to match the clock:
pawn Code:
#undef SetWorldTime
#define SetWorldTime(%1, %2) for(new i = 0,j = GetPlayerPoolSize();i <= j;i++) if(IsPlayerConnected(i)) SetPlayerTime(i, %1, %2);
Minutes doesn't matter to the sky neither in SetWorldTime or SetPlayerTime.


All times are GMT. The time now is 07:55 PM.

Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.