Jump to content

Client synced clock


Spakye

Recommended Posts

Hi, Im working on a server project and i would need some suggestion to synchronise clients clocks.

I have zombies spawning only  from 23:00 till 08:00, also a script change the weather during this period to create a mist, so everynight there is zombies everywhere and you see nothing.

Tonight my gf joined the server so we could do some testing, and we realised our clocks were not synced wich just ruin all the night experience, when it was night for me it was day for her.

Setting time from the admin panel does solve the problem but on the long run i would like a script to do that automaticly.

So im wondering what would be the best way to do that.

Each time a player join i could get the time server side and set it again ?

or something else using a timer ?

What do you thing would be the most efficent and less laggy for the server ?

Edited by Spakye
Link to comment

Hello Spakye,

I think that time synchronization can be very accurate. If you count the time on the server then you can tell the client on join what time it is. Then, assuming that the clients count the time the same speed, they can tell all the future time values without a single message by the server. So it does not stress the network and does not create performance problems on the client. I do not understand how you assumed that it would be "laggy for the server"?

I recommend the following functions to synchronize the time:

  • setMinuteDuration - recommended to set something higher than 1000 to have more accurate time if players have like 500ms lag
  • getTickCount - you can create some kind of time counter using this function
  • getRealTime - if you want to use the wall-clock as time using a minute duration of 60000 ms then use this

Hopefully my answer could help you get the hang of this problem. :) 

Link to comment

Ty for your reply its very helpfull !

i did take a look into theses functions but im a novice in scripting so its a bit overwhelming for me right now, but ill put myself into it when i get some more skills.

I did code that, as a temporary fix :

function setTimeOnJoin()
    timehour, timeminute = getTime()
    triggerClientEvent(source, "eventSetTimeOnJoin", source,timehour,timeminute)
end
addEventHandler ( "onPlayerJoin", getRootElement(), setTimeOnJoin )
function setTimeClientSide (timehour,timeminute)
    setTime(timehour,timeminute)
end
addEvent( "eventSetTimeOnJoin", true )
addEventHandler( "onPlayerJoin", localPlayer, setTimeClientSide )

i didnt have opportunity to test it with someone yet but i think it should work.

Link to comment
1 hour ago, Spakye said:

Ty for your reply its very helpfull !

i did take a look into theses functions but im a novice in scripting so its a bit overwhelming for me right now, but ill put myself into it when i get some more skills.

I did code that, as a temporary fix :


function setTimeOnJoin()
    timehour, timeminute = getTime()
    triggerClientEvent(source, "eventSetTimeOnJoin", source,timehour,timeminute)
end
addEventHandler ( "onPlayerJoin", getRootElement(), setTimeOnJoin )

function setTimeClientSide (timehour,timeminute)
    setTime(timehour,timeminute)
end
addEvent( "eventSetTimeOnJoin", true )
addEventHandler( "onPlayerJoin", localPlayer, setTimeClientSide )

i didnt have opportunity to test it with someone yet but i think it should work.

Good solution! Sorry if my reply were a little confusing because I tried to understand the scope of your problem. But if you just want to use the standard game time system then this should work. But you made one tiny mistake: you used the event "onPlayerJoin" for the clientside addEventHandler call, should have been "eventSetTimeOnJoin".

clientside script:

function setTimeClientSide (timehour,timeminute)
    setTime(timehour,timeminute)
end
addEvent( "eventSetTimeOnJoin", true )
addEventHandler( "eventSetTimeOnJoin", localPlayer, setTimeClientSide )

EDIT: there is a race condition in your script if the client has not loaded the Lua files yet but you trigger the eventSetTimeOnJoin anyway. To fix that you have to trigger a server event "onPlayerReady" at the end of your clientside script, then add this event "onPlayerReady" on the server-side + attach the server-side event handler, previously from "onPlayerJoin", you made to that.

Edited by The_GTA
  • Like 1
Link to comment

Ty i didnt see my mistake !

Im a bit confused about the event "onPlayerReady" that you talk about, i can't find anything about it in the wiki. Though i did try to follow your instructions
 

server side

function setTimeOnJoin()
    timehour, timeminute = getTime()
    triggerClientEvent(source, "eventSetTimeOnJoin", source,timehour,timeminute)
end
-- addEventHandler ( "onPlayerJoin", getRootElement(), setTimeOnJoin )


addEvent( "playerIsReady", true )
addEventHandler( "playerIsReady", getRootElement(), setTimeOnJoin )

Client side

function setTimeClientSide (timehour,timeminute)
    setTime(timehour,timeminute)
end
addEvent( "eventSetTimeOnJoin", true )
addEventHandler( "eventSetTimeOnJoin", localPlayer, setTimeClientSide )


addEventHandler("onPlayerReady", localPlayer, function()
    triggerServerEvent("playerIsReady", localPlayer)
end)
Edited by Spakye
Link to comment

Let me help you a little bit. Please replace your client-side script with this:

client-side

function setTimeClientSide (timehour,timeminute)
    setTime(timehour,timeminute)
end
addEvent( "eventSetTimeOnJoin", true )
addEventHandler( "eventSetTimeOnJoin", localPlayer, setTimeClientSide )

triggerServerEvent("playerIsReady", localPlayer)

I have got years of experience in making MTA resources and this client synchronization is a common issue I faced. ;) 

Edited by The_GTA
  • Thanks 1
Link to comment

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.
×
×
  • Create New...