Esempio n. 1
0
        /// <summary>
        /// Used to manually remove a networkobject from the networker
        /// </summary>
        /// <param name="networkObject"></param>
        public void ManualRemove(NetworkObject networkObject)
        {
            lock (NetworkObjects)
            {
                NetworkObjects.Remove(networkObject.NetworkId);
            }

            lock (NetworkObjectList)
            {
                NetworkObjectList.Remove(networkObject);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Used to manually remove a bunch of networkobjects from the networker
        /// </summary>
        /// <param name="networkObjects"></param>
        public void ManualRemove(List <NetworkObject> networkObjects)
        {
            foreach (NetworkObject obj in networkObjects)
            {
                lock (NetworkObjects)
                {
                    NetworkObjects.Remove(obj.NetworkId);
                }
            }

            foreach (NetworkObject obj in networkObjects)
            {
                lock (NetworkObjectList)
                {
                    NetworkObjectList.Remove(obj);
                }
            }
        }
        /// <summary>
        /// Register a networked object with this networker
        /// </summary>
        /// <param name="networkObject">The object that is to be registered with this networker</param>
        /// <returns><c>true</c> if the object was registered successfully, else <c>false</c> if it has already been registered</returns>
        public bool RegisterNetworkObject(NetworkObject networkObject, uint forceId = 0)
        {
            uint id = currentNetworkObjectId;

            lock (NetworkObjects)
            {
                // If we are forcing this object
                if (forceId != 0)
                {
                    if (NetworkObjects.ContainsKey(forceId))
                    {
                        return(false);
                    }

                    id = forceId;

                    if (!networkObject.RegisterOnce(id))
                    {
                        throw new BaseNetworkException("The supplied network object has already been assigned to a networker and has an id");
                    }

                    //AddNetworkObject(forceId, networkObject);
                    //NetworkObjectList.Add(networkObject);
                }
                else
                {
                    do
                    {
                        if (NetworkObjects.ContainsKey(++currentNetworkObjectId))
                        {
                            continue;
                        }

                        if (!networkObject.RegisterOnce(currentNetworkObjectId))
                        {
                            // Backtrack since the next call to this method will increment before checking
                            currentNetworkObjectId--;

                            throw new BaseNetworkException("The supplied network object has already been assigned to a networker and has an id");
                        }

                        //AddNetworkObject(currentNetworkObjectId, networkObject);
                        //NetworkObjectList.Add(networkObject);
                        break;
                    } while (IsBound);
                }
            }

            // Assign the network id to the network object
            networkObject.RegisterOnce(id);

            // When this object is destroyed it needs to remove itself from the list
            networkObject.onDestroy += (NetWorker sender) =>
            {
                lock (NetworkObjects)
                {
                    NetworkObjects.Remove(networkObject.NetworkId);
                    NetworkObjectList.Remove(networkObject);
                }
            };

            return(true);
        }