Download

Best way to do a timer in network game ?

Yo,

what the best way to do a timer in a network game?

first idea:

-Server start timer for every clients and himself with a ONE RPC EVENT (problem? if one client had lag at this moment, he will have a noticeable difference for his timer)

second idea:

-Server start timer for himself only, and every second he send a rpc to every clients (seem ok? at least client may have a better sync for timer, but seem not good enough for a “racing game” start timer)

conclusion:

it seem that i dont know how to do a good and simple timer in a multplayer game :frowning:

i dont like my two idea, you probably have better & easier way to achieve a good timer like a “start timer” for racing game

how would you do a starting timer for a race game in multiplayer ? :slight_smile:

i think i need to know a “time” when the server send the rpc, so i can adjust the timer on client when he receive the rpc event

but i dont know how to do :o

Stick the timer variable somewhere server side like the gamemode. Then replicate the variable. If you look in the networking docs the example for replicating variables is a timer that countdowns and is displayed on all clients. Should be exactly what you want.

I’ll find the link later when I’m on my PC if you can’t find it.

thanks, can you send me the link your talking about?

on another note, if you want to show real time tick in ms, you would better do local timer update, but only “unlock control/movement” when server sents out the event.
Reason being, network update aren’t and can’t(shouldn’t) be updated every tick, if you rely on replicated number for racing count down timer, you could have more issues.

Also, if you want to be extra precise, you might want to see if there is a average ping between server and client and “schedule” your unlock event when it’s sub 1 second.
It says somewhere on internet that human have average reaction speed about 250+ms, so if your “Go” between server and client can be under 100ms it makes it seem almost start at the same time.

As a general rule of thumb, don’t abuse tick or any event that updates per frame. Schedule and work around it generally gives you better result when your game scales up.

sorry bud forgot to send it :slight_smile:

You can’t replicate a variable in the GameMode. The GameMode only exists on the Server, so no instance to replicate it to.

Use the GameState Class. That’s replicated and more meant to have GAMESTATE related variables.

My solution would be either:

Start Timer only on Server, since logic should happen on Server! Save the RemainingTime of the TimerHandle to a replicated float.
That would need a the Tick to constantly replicate the Variable though. So nothing you want to do through out the whole game.

OR

Start the Timer for Logic on the Server and do a MULTICAST to start a second timer on all Clients that does not do any logic stuff.
It’s only to have the timer. Though keep in mind, that the Timer could be offset if someone lags and the Multicast gets delayed.

sorry gamestate not gamemode, to many things called near identical names :wink:

if you read the link i sent to the official docs it doesn’t use the tick to replicate the variable it uses a a 1 second timer to update the variable.

basically just read the link it’s exactly what you want to do :slight_smile:

Thanks for answers :slight_smile: i will study the doc you provided