Angel

Client side time progression script

Recommended Posts

Hello everyone,

I thought I would share this code snippet that others may find useful. It is a purely client side time of day progression script that gives the same time progression as it is in single player. To use it, simply `require` the js file and it automatically starts.
 

Quote
(function() {
	var clockInterval = 0;
	if (!clockInterval) {
		var hour = 0;
		var min = 0;
		var sec = 0;
		clockInterval = setInterval(function() {
			var d = new Date();
			var h1 = d.getUTCHours();
			var m1 = d.getUTCMinutes();
			var s1 = d.getUTCSeconds();
			var ms = d.getUTCMilliseconds();

			hour = (Math.floor(m1/2) + h1 * 6) %24;
			min =  (Math.floor(s1/2) + m1 * 30) % 60;
			sec =  (Math.floor(ms*0.03) + s1 * 30) % 60;

			mp.game.time.setClockTime(hour, min, sec);
			mp.game.time.setClockDate(d.getUTCDate(), d.getUTCMonth(), d.getUTCFullYear());
		}, 250);
	}
})();


 

  • Like 1

Share this post


Link to post
Share on other sites

If possible it's best to run serverside if your whole server depends on everyone having the same time. This code would execute based on that clients current time and not the server time. So someone in America, for example, would have their world in the daytime, but someone playing in Australia would be playing with their world in the nighttime.

It's a good share though.

Share this post


Link to post
Share on other sites

Actually, if the users have their time and timezone set correct, each client will have the same in game time as the script uses GMT as its reference.

 

I've tested this with two computers running different timezones, in game time is the same.

Share this post


Link to post
Share on other sites
1 hour ago, Angel said:

Actually, if the users have their time and timezone set correct, each client will have the same in game time as the script uses GMT as its reference.

 

I've tested this with two computers running different timezones, in game time is the same.

You're right my mistake, you're using UTC, I quickly read over and thought you were simply using getHours() etc to grab the computers time. Still great though and this is really useful.

  • Like 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.