Esempio n. 1
0
 /// <summary>
 /// Despawn the specified game object, syncing to all clients. Should only be called
 /// on networked objects (i.e. ones which have NetworkIdentity component). Despawning removes an
 /// object from the game, but may not necessarilly destroy it completely - it may end up back in the
 /// object pool to be later reused.
 /// </summary>
 /// <param name="toDespawn"></param>
 /// <param name="skipInventoryDespawn">If the indicated object is in inventory, it will
 /// be despawned via the inventory API instead. Set this to true to bypass this. This is only
 /// intended to be set to true for particular internal use cases in the lifecycle system, so should
 /// almost always be left at the default</param>
 /// <returns></returns>
 public static async Task <DespawnResult> ServerSingle(GameObject toDespawn, bool skipInventoryDespawn = false)
 {
     return(await Server(DespawnInfo.Single(toDespawn), skipInventoryDespawn));
 }
Esempio n. 2
0
 /// <summary>
 /// Despawn the specified game object, syncing to all clients. Should only be called
 /// on networked objects (i.e. ones which have NetworkIdentity component). Despawning removes an
 /// object from the game, but may not necessarilly destroy it completely - it may end up back in the
 /// object pool to be later reused.
 /// </summary>
 /// <param name="toDespawn"></param>
 /// <param name="skipInventoryDespawn">If the indicated object is in inventory, it will
 /// be despawned via the inventory API instead. Set this to true to bypass this. This is only
 /// intended to be set to true for particular internal use cases in the lifecycle system, so should
 /// almost always be left at the default</param>
 /// <returns></returns>
 public static DespawnResult ServerSingle(GameObject toDespawn, bool skipInventoryDespawn = false)
 {
     return(Server(DespawnInfo.Single(toDespawn)));
 }
Esempio n. 3
0
 /// <summary>
 /// Despawn the specified game object locally, on this client only. Should ONLY be called on non-networked objects
 /// (i.e. ones which don't have NetworkIdentity component).
 /// Despawning removes an object from the game, but may not necessarilly destroy it completely - it may end up back in the
 /// object pool to be later reused.
 /// </summary>
 /// <param name="toDespawn"></param>
 /// <returns></returns>
 public static DespawnResult ClientSingle(GameObject toDespawn)
 {
     return(Client(DespawnInfo.Single(toDespawn)));
 }
Esempio n. 4
0
    /// <summary>
    /// FOR DEV / TESTING ONLY! Simulates destroying and recreating an item by putting it in the pool and taking it back
    /// out again. If item is not pooled, simply destroys and recreates it as if calling Despawn and then Spawn
    /// Can use this to validate that the object correctly re-initializes itself after spawning -
    /// no state should be left over from its previous incarnation.
    /// </summary>
    /// <returns>the re-created object</returns>
    public static GameObject ServerPoolTestRespawn(GameObject target)
    {
        var poolPrefabTracker = target.GetComponent <PoolPrefabTracker>();

        if (poolPrefabTracker == null)
        {
            //destroy / create using normal approach with no pooling
            Logger.LogWarningFormat("Object {0} has no pool prefab tracker, thus cannot be pooled. It will be destroyed / created" +
                                    " without going through the pool.", Category.ItemSpawn, target.name);

            //determine prefab
            var position = target.TileWorldPosition();
            var prefab   = DeterminePrefab(target);
            if (prefab == null)
            {
                Logger.LogErrorFormat("Object {0} at {1} cannot be respawned because it has no PoolPrefabTracker and its name" +
                                      " does not match a prefab name, so we cannot" +
                                      " determine the prefab to instantiate. Please fix this object so that it" +
                                      " has an attached PoolPrefabTracker or so its name matches the prefab it was created from.", Category.ItemSpawn, target.name, position);
                return(null);
            }

            Despawn.ServerSingle(target);
            return(ServerPrefab(prefab, position.To3Int()).GameObject);
        }
        else
        {
            //destroy / create with pooling
            //save previous position
            var destination = SpawnDestination.At(target);
            var worldPos    = target.TileWorldPosition();
            var transform   = target.GetComponent <IPushable>();
            var prevParent  = target.transform.parent;

            //this simulates going into the pool
            Despawn._ServerFireDespawnHooks(DespawnResult.Single(DespawnInfo.Single(target)));

            if (transform != null)
            {
                transform.VisibleState = false;
            }

            //this simulates coming back out of the pool
            target.SetActive(true);

            target.transform.parent   = prevParent;
            target.transform.position = worldPos.To3Int();

            var cnt = target.GetComponent <CustomNetTransform>();
            if (cnt)
            {
                cnt.ReInitServerState();
                cnt.NotifyPlayers();                 //Sending out clientState for already spawned items
            }
            var       prefab    = DeterminePrefab(target);
            SpawnInfo spawnInfo = SpawnInfo.Spawnable(
                SpawnablePrefab.For(prefab),
                destination);


            _ServerFireClientServerSpawnHooks(SpawnResult.Single(spawnInfo, target));
            return(target);
        }
    }