@@ -919,7 +919,10 @@ void SceneSynchronizerBase::on_peer_connected(int p_peer) {
919919 return ;
920920 }
921921
922- pd_it->second .make_controller (*this , p_peer);
922+ pd_it->second .make_controller ();
923+ pd_it->second .get_controller ()->setup_synchronizer (*this , p_peer);
924+ // Clear the process function because they need to be rebuild to include the new peer.
925+ process_functions__clear ();
923926 reset_controller (*pd_it->second .get_controller ());
924927
925928 event_peer_status_updated.broadcast (p_peer, true , true );
@@ -940,6 +943,9 @@ void SceneSynchronizerBase::on_peer_disconnected(int p_peer) {
940943
941944 peer_data.erase (p_peer);
942945
946+ // Clear the process function to make sure the peer process functions are removed.
947+ process_functions__clear ();
948+
943949#ifdef DEBUG_ENABLED
944950 ASSERT_COND_MSG (peer_data.count (p_peer) <= 0 , " The peer was just removed. This can't be triggered." );
945951#endif
@@ -1386,11 +1392,29 @@ void SceneSynchronizerBase::process_functions__execute() {
13861392 NS_PROFILE_WITH_INFO (info);
13871393
13881394 if (cached_process_functions_valid == false ) {
1389- // Clear the process_functions.
1395+ // Clear all the process_functions.
13901396 for (int process_phase = PROCESS_PHASE_EARLY; process_phase < PROCESS_PHASE_COUNT; ++process_phase) {
13911397 cached_process_functions[process_phase].clear ();
13921398 }
13931399
1400+ // Add a new process function for each peer
1401+ {
1402+ // Fetch the connected peers and sort them
1403+ std::vector<int > peers;
1404+ for (const auto &[peer, _] : peer_data) {
1405+ peers.push_back (peer);
1406+ }
1407+ std::sort (peers.begin (), peers.end ());
1408+
1409+ // For each peer, add the process function.
1410+ for (int peer : peers) {
1411+ PeerNetworkedController *peer_controller = get_controller_for_peer (peer, false );
1412+ if (peer_controller) {
1413+ cached_process_functions[PROCESS_PHASE_PROCESS].bind (std::bind (&PeerNetworkedController::process, peer_controller, std::placeholders::_1));
1414+ }
1415+ }
1416+ }
1417+
13941418 // Build the cached_process_functions, making sure the node data order is kept.
13951419 for (auto od : objects_data_storage.get_sorted_objects_data ()) {
13961420 if (od == nullptr || (is_client () && od->realtime_sync_enabled_on_client == false )) {
@@ -1411,27 +1435,7 @@ void SceneSynchronizerBase::process_functions__execute() {
14111435 SceneSynchronizerDebugger::singleton ()->print (INFO, " Process functions START" );
14121436
14131437 // Pre process phase
1414- for (int process_phase = PROCESS_PHASE_EARLY; process_phase < PROCESS_PHASE_PROCESS; ++process_phase) {
1415- const std::string info = " process phase: " + std::to_string (process_phase);
1416- NS_PROFILE_WITH_INFO (info);
1417- cached_process_functions[process_phase].broadcast (get_fixed_frame_delta ());
1418- }
1419-
1420- // Controller process
1421- {
1422- const std::string info = " process phase -- CONTROLLER --" ;
1423- NS_PROFILE_WITH_INFO (info);
1424-
1425- // TODO optimize this, as `can_simulate` is expensive.
1426- for (auto &pd : peer_data) {
1427- if (pd.second .get_controller () && pd.second .get_controller ()->can_simulate ()) {
1428- pd.second .get_controller ()->process (get_fixed_frame_delta ());
1429- }
1430- }
1431- }
1432-
1433- // Post process
1434- for (int process_phase = PROCESS_PHASE_PROCESS; process_phase < PROCESS_PHASE_COUNT; ++process_phase) {
1438+ for (int process_phase = PROCESS_PHASE_EARLY; process_phase < PROCESS_PHASE_COUNT; ++process_phase) {
14351439 const std::string info = " process phase: " + std::to_string (process_phase);
14361440 NS_PROFILE_WITH_INFO (info);
14371441 cached_process_functions[process_phase].broadcast (get_fixed_frame_delta ());
0 commit comments