SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 17/01/2020, 03:42 PM   #1
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 440
Reputation: 20
Default 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?
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Old 17/01/2020, 03:58 PM   #2
tekass
Little Clucker
 
Join Date: May 2017
Posts: 21
Reputation: 0
Default Re: Make the world time the same for everyone

i think this can help you:

https://pastebin.com/Yvivm9T7
tekass is offline   Reply With Quote
Old 17/01/2020, 04:15 PM   #3
DomagojSellug
Huge Clucker
 
DomagojSellug's Avatar
 
Join Date: Sep 2016
Location: Croatia
Posts: 255
Reputation: 22
Default 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!
__________________

San Andreas Multiplayer server based off Counter Strike: Global Offensive.

Join us today!

DISCORD: discord.sa-go.info
WEBSITE: http://sa-go.info
SERVER IP: samp.sa-go.info
DomagojSellug is offline   Reply With Quote
Old 17/01/2020, 04:19 PM   #4
FunnyBear
Gangsta
 
Join Date: Mar 2013
Posts: 510
Reputation: 15
Default 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;
}
FunnyBear is offline   Reply With Quote
Old 17/01/2020, 05:53 PM   #5
NaS
High-roller
 
NaS's Avatar
 
Join Date: Mar 2008
Location: 🇩🇪
Posts: 1,774
Reputation: 619
Default Re: Make the world time the same for everyone

Quote:
Originally Posted by FunnyBear View Post
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.
NaS is offline   Reply With Quote
Old 17/01/2020, 06:34 PM   #6
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 440
Reputation: 20
Default Re: Make the world time the same for everyone

Quote:
Originally Posted by DomagojSellug View Post
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).
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Old 17/01/2020, 06:57 PM   #7
DomagojSellug
Huge Clucker
 
DomagojSellug's Avatar
 
Join Date: Sep 2016
Location: Croatia
Posts: 255
Reputation: 22
Default Re: Make the world time the same for everyone

Quote:
Originally Posted by Proxus View Post
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.
__________________

San Andreas Multiplayer server based off Counter Strike: Global Offensive.

Join us today!

DISCORD: discord.sa-go.info
WEBSITE: http://sa-go.info
SERVER IP: samp.sa-go.info
DomagojSellug is offline   Reply With Quote
Old 17/01/2020, 07:33 PM   #8
PPC23
Little Clucker
 
Join Date: Feb 2013
Posts: 28
Reputation: 9
Default 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.
PPC23 is offline   Reply With Quote
Old 17/01/2020, 07:35 PM   #9
Proxus
Huge Clucker
 
Proxus's Avatar
 
Join Date: May 2018
Location: United Kingdom
Posts: 440
Reputation: 20
Default Re: Make the world time the same for everyone

Quote:
Originally Posted by PPC23 View Post
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.
__________________
Developer @ Trucknation
"Never tell people how to do things. Tell them what to do and they will surprise you with their ingenuity"


IMPORTANT LINKS
An upcoming mix of trucking & light roleplay
Proxus is offline   Reply With Quote
Old 17/01/2020, 07:52 PM   #10
PPC23
Little Clucker
 
Join Date: Feb 2013
Posts: 28
Reputation: 9
Default 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.
PPC23 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
World Time AnonScripter Scripting Help 3 03/10/2013 09:01 PM
how to set player's time same as world time memeli11 Scripting Help 0 02/05/2013 07:35 PM
Get World Time VirSpectorX Help Archive 13 22/06/2010 01:36 PM
[HELP] World time. screw Help Archive 3 02/01/2010 06:46 PM
[HELP]How To set world time. arachimi Help Archive 1 22/12/2009 10:10 PM


All times are GMT. The time now is 02:20 AM.


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