Author Topic: TNet 3 - Host change bug?  (Read 2055 times)

xandeck

  • Jr. Member
  • **
  • Thank You
  • -Given: 1
  • -Receive: 3
  • Posts: 54
    • View Profile
TNet 3 - Host change bug?
« on: May 18, 2016, 10:22:04 PM »
Hello Aren,

After instantiating an object via "TNManager.Instantiate", and set persistent to true, as soon as my host leaves the room, the "tno.isMine" is not changing to another player.
This is supposed to happen, right? I mean, change who owns the object...
Or am I doing anything wrong? Also, what is the real difference between isMine and ownerId?

Thank you
« Last Edit: May 19, 2016, 12:07:17 PM by xandeck »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNet 3 - Host change bug?
« Reply #1 on: May 21, 2016, 09:58:50 PM »
TNObject's owner changes in TNObject.OnPlayerLeave:
  1.         void OnPlayerLeave (int channelID, Player p)
  2.         {
  3.                 if (channelID == this.channelID && p != null && mOwner == p)
  4.                         mOwner = TNManager.GetHost(channelID);
  5.         }
As you can see if the owner of the object leaves, the owner changes to that of the host of the channel that the object belongs to. The server updates the host prior to sending out the player left notification, as you can see in TNGameServer.cs, line 942:
  1.                                 BinaryWriter writer;
  2.  
  3.                                 // Inform the other players that the player's objects should be destroyed
  4.                                 if (mTemp.size > 0)
  5.                                 {
  6.                                         writer = BeginSend(Packet.ResponseDestroyObject);
  7.                                         writer.Write(ch.id);
  8.                                         writer.Write((ushort)mTemp.size);
  9.                                         for (int i = 0; i < mTemp.size; ++i) writer.Write(mTemp[i]);
  10.                                         EndSend(ch, null, true);
  11.                                 }
  12.  
  13.                                 // If this player was the host, choose a new host
  14.                                 if (ch.host == null) SendSetHost(ch, (TcpPlayer)ch.players[0]);
  15.  
  16.                                 // Inform everyone of this player leaving the channel
  17.                                 writer = BeginSend(Packet.ResponsePlayerLeft);
  18.                                 writer.Write(ch.id);
  19.                                 writer.Write(player.id);
  20.                                 EndSend(ch, null, true);

I am also not sure why you are asking about the difference between TNObject.isMine and ownerId. They're right next to each other so you can see for yourself:
  1.         /// <summary>
  2.         /// Whether this object belongs to the player.
  3.         /// </summary>
  4.  
  5.         public bool isMine { get { return (mOwner != null) ? mOwner == TNManager.player : TNManager.isHosting; } }
  6.  
  7.         /// <summary>
  8.         /// ID of the player that owns this object.
  9.         /// </summary>
  10.  
  11.         public int ownerID { get { return (mParent != null) ? mParent.ownerID : (mOwner ?? TNManager.GetHost(channelID)).id; } }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNet 3 - Host change bug?
« Reply #2 on: May 21, 2016, 10:05:05 PM »
P.S. That said, it probably makes sense to change isMine to:
  1.         public bool isMine
  2.         {
  3.                 get
  4.                 {
  5.                         var owner = this.owner;
  6.                         return (owner != null) ? owner == TNManager.player : TNManager.isHosting;
  7.                 }
  8.         }
This way it goes through TNObject.owner which will handle nested objects properly.