Author Topic: Post Match Cleanup  (Read 8103 times)

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: Post Match Cleanup
« Reply #15 on: August 03, 2017, 05:16:01 AM »
But then TNManager.LoadLevel will always cause bugs because Channel.Reset is called on the server.

Player A joins Channel 1. Player A instantiates TNObject#1. Player A calls TNManager.LoadLevel. Server calls channel.Reset.
Player B joins Channel 1. Player A calls RFC#1 on TNObject#1. TNObject#1 does not exist for Player B: error.

I don't think TNet should control object lifetime. Unity will only destroy an object if the scene it belongs to is unloaded. You can still load additive scenes.
I think objects should still be destroyed when leaving their channel, but they should also be destroyed when unloading their scene.

rxmarcus

  • Jr. Member
  • **
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 62
    • View Profile
Re: Post Match Cleanup
« Reply #16 on: August 07, 2017, 11:09:10 PM »
Agreed.
It is super problematic if I want to reset / start a new match with existing players in a channel as I don't want to have to kick them all out of a channel and have them join a new one just to clean up my network objects properly.....

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Post Match Cleanup
« Reply #17 on: August 12, 2017, 12:54:42 PM »
So sounds to me like the real issue is that OnLoadLevel notification doesn't remove TNObjects belonging to the channel where the level was just switched, not that DontDestroyOnLoad is being used. Here's how I'd fix it properly:

1. Add one line to TNManager.cs, line 1772:
  1.                         mClient.onLoadLevel = delegate (int channelID, string levelName)
  2.                         {
  3.                                 lastChannelID = channelID;
  4.                                 TNObject.CleanupChannelObjects(channelID); // <-- this here
  5.  
  6.                                 if (!string.IsNullOrEmpty(levelName))
  7.                                 {
  8.                                         mLoadingLevel.Add(channelID);
  9.                                         StartCoroutine("LoadLevelCoroutine", new System.Collections.Generic.KeyValuePair<int, string>(channelID, levelName));
  10.                                 }
  11.                         };
2. Add the function to TNObject:
  1.                 /// <summary>
  2.                 /// Called by TNManager when loading a new level. All objects belonging to the previous level need to be destroyed.
  3.                 /// </summary>
  4.  
  5.                 static internal void CleanupChannelObjects (int channelID)
  6.                 {
  7.                         List<TNObject> temp = null;
  8.  
  9.                         foreach (var pair in mList)
  10.                         {
  11.                                 var list = pair.Value;
  12.  
  13.                                 for (int i = 0; i < list.size; ++i)
  14.                                 {
  15.                                         var ts = list[i];
  16.  
  17.                                         if (ts != null && ts.channelID == channelID)
  18.                                         {
  19.                                                 if (temp == null) temp = new List<TNObject>();
  20.                                                 temp.Add(ts);
  21.                                         }
  22.                                 }
  23.                         }
  24.  
  25.                         if (temp != null) foreach (var ts in temp) ts.OnDestroyPacket();
  26.                 }