Author Topic: TNManager.isHosting always returning true  (Read 2684 times)

gevarre

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 15
    • View Profile
TNManager.isHosting always returning true
« on: February 05, 2016, 03:07:49 PM »
Hi,
So I'm trying to get a spawning system going for target objects controlled by the host. I've got this at the start of the control script:

  1. using UnityEngine;
  2. using System.Collections;
  3. using TNet;
  4.  
  5. public class TargetSpawnControl : TNBehaviour
  6. {
  7.         public GameObject target;
  8.         public GameObject[] targetSpawners;
  9.         public bool hosting = false;   
  10.        
  11.         void Awake()
  12.         {
  13.                 Debug.LogError(TNManager.isHosting);
  14.                
  15.                 if(!TNManager.isHosting) return;
  16.                 if(TNManager.isHosting) hosting = true;
  17.  
  18.                 StartCoroutine(StartTargetSpawnTimer());       
  19.         }
  20. }
  21.  

But for some reason, no matter what I do, TNManager.isHosting always returns true on all the clients and hijinks ensue. Anyone know what's happening? I'm taking this from the tutorial here.

cmifwdll

  • Global Moderator
  • Sr. Member
  • *****
  • Thank You
  • -Given: 0
  • -Receive: 149
  • Posts: 285
  • TNet Alchemist
    • View Profile
Re: TNManager.isHosting always returning true
« Reply #1 on: February 05, 2016, 04:25:44 PM »
You shouldn't be doing that in Awake(). Awake gets called as soon as the object is loaded. So if your object's Awake gets called before TNet's Awake then you'll run into this issue. Awake should only be used for initializing that specific gameobject (without referencing any other object). Consider using Start() if you need to initialize your object based on another object.

Assuming you're putting this object in the scene before running the game, putting this code in Start won't work either, because Start gets called immediately after the last Awake call, and it's impossible that you'd be able to connect by then.

Try using the OnNetworkConnect(bool result, string message), OnNetworkJoinChannel(bool result, string message), or OnSetHost(bool hosting) events. For the latter, you'll have to subscribe to the event like so:
  1. void Start()
  2. {
  3.     TNManager.client.onSetHost += OnSetHost;
  4. }
  5.  
  6. void OnSetHost(bool hosting)
  7. {
  8.    
  9. }
  10.  

Alternatively, you can just delay the creation of the TargetSpawnControl gameobject until TNet has loaded, then TNManager.isHosting should work properly in all cases.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: TNManager.isHosting always returning true
« Reply #2 on: February 07, 2016, 12:04:45 AM »
There is no need to have a local "hosting" flag. TNManager.isHosting is all you need, although in most cases I recommend using tno.isMine instead. Basically whoever creates the object will be responsible for it. TNManager.isHosting is useful for cases where you want to use the lowest ping player for something, such as AI logic, since you can transfer the host status to another player.

Also note that TNManager.isHosting will return 'true' if you are not connected or if you are not in a channel.