@@ -345,9 +345,7 @@ var _last_process_time: float = 0.
345345var _clock : NetworkClocks .SteppingClock = NetworkClocks .SteppingClock .new ()
346346var _clock_stretch_factor : float = 1.
347347
348- # Cache the synced clients, as the rpc call itself may arrive multiple times
349- # ( for some reason? )
350- var _synced_clients : Dictionary = {}
348+ var _synced_peers : Dictionary = {}
351349
352350static var _logger : _NetfoxLogger = _NetfoxLogger .for_netfox ("NetworkTime" )
353351
@@ -367,9 +365,8 @@ func start():
367365 _tick = 0
368366 _initial_sync_done = false
369367
370- after_client_sync .connect (func (pid ):
371- _logger .debug ("Client #%s is now on time!" % [pid ])
372- )
368+ # Host is always synced, as their time is considered ground truth
369+ _synced_peers [1 ] = true
373370
374371 NetworkTimeSynchronizer .start ()
375372
@@ -380,13 +377,13 @@ func start():
380377 _initial_sync_done = true
381378 _active = true
382379
383- _submit_sync_success .rpc_id ( 1 )
380+ _submit_sync_success .rpc ( )
384381 else :
385382 _active = true
386383 _initial_sync_done = true
387-
388- # Remove clients from the synced cache when disconnected
389- multiplayer .peer_disconnected .connect (func (peer ): _synced_clients .erase (peer ))
384+
385+ # Remove clients from the synced cache when disconnected
386+ multiplayer .peer_disconnected .connect (func (peer ): _synced_peers .erase (peer ))
390387
391388 _clock .set_time (NetworkTimeSynchronizer .get_time ())
392389 _last_process_time = _clock .get_time ()
@@ -400,6 +397,7 @@ func start():
400397func stop ():
401398 NetworkTimeSynchronizer .stop ()
402399 _active = false
400+ _synced_peers .clear ()
403401
404402## Check if the initial time sync is done.
405403func is_initial_sync_done () -> bool :
@@ -409,11 +407,7 @@ func is_initial_sync_done() -> bool:
409407##
410408## Using this from a client is considered an error.
411409func is_client_synced (peer_id : int ) -> bool :
412- if not multiplayer .is_server ():
413- _logger .error ("Trying to check if client is synced from another client!" )
414- return false
415- else :
416- return _synced_clients .has (peer_id )
410+ return _synced_peers .has (peer_id )
417411
418412## Convert a duration of ticks to seconds.
419413func ticks_to_seconds (ticks : int ) -> float :
@@ -497,10 +491,14 @@ func _notification(what):
497491 if what == NOTIFICATION_UNPAUSED :
498492 _was_paused = true
499493
500- @rpc ("any_peer" , "reliable" , "call_remote " )
494+ @rpc ("any_peer" , "reliable" , "call_local " )
501495func _submit_sync_success ():
502496 var peer_id = multiplayer .get_remote_sender_id ()
503497
504- if not _synced_clients .has (peer_id ):
505- _synced_clients [peer_id ] = true
506- after_client_sync .emit (multiplayer .get_remote_sender_id ())
498+ _logger .trace ("Received time sync success from #%s , synced peers: %s " % [peer_id , _synced_peers .keys ()])
499+
500+ # Check is necessary, RPC arrives twice for some reason; see trace log above
501+ if not _synced_peers .has (peer_id ):
502+ _synced_peers [peer_id ] = true
503+ after_client_sync .emit (peer_id )
504+ _logger .debug ("Peer #%s is now on time!" % [peer_id ])
0 commit comments