Added OnGainedOwnership and OnLostOwnership virtual methods

This commit is contained in:
Albin Corén 2018-03-05 15:42:30 +01:00
parent 1fe6a74ad6
commit 87658b2ce9
3 changed files with 46 additions and 0 deletions

View File

@ -87,6 +87,16 @@ namespace MLAPI
}
public virtual void OnGainedOwnership()
{
}
public virtual void OnLostOwnership()
{
}
protected int RegisterMessageHandler(string name, Action<int, byte[]> action)
{
int counter = MessageManager.AddIncomingMessageHandler(name, action, networkId);

View File

@ -61,6 +61,32 @@ namespace MLAPI
SpawnManager.ChangeOwnership(NetworkId, newOwnerClientId);
}
internal void InvokeBehaviourOnLostOwnership()
{
NetworkedBehaviour[] netBehaviours = GetComponentsInChildren<NetworkedBehaviour>();
for (int i = 0; i < netBehaviours.Length; i++)
{
//We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject.
if (netBehaviours[i].networkedObject == this)
{
netBehaviours[i].OnLostOwnership();
}
}
}
internal void InvokeBehaviourOnGainedOwnership()
{
NetworkedBehaviour[] netBehaviours = GetComponentsInChildren<NetworkedBehaviour>();
for (int i = 0; i < netBehaviours.Length; i++)
{
//We check if we are it's networkedObject owner incase a networkedObject exists as a child of our networkedObject.
if (netBehaviours[i].networkedObject == this)
{
netBehaviours[i].OnGainedOwnership();
}
}
}
internal void InvokeBehaviourNetworkSpawn()
{
NetworkedBehaviour[] netBehaviours = GetComponentsInChildren<NetworkedBehaviour>();

View File

@ -645,6 +645,16 @@ namespace MLAPI
{
uint netId = messageReader.ReadUInt32();
int ownerClientId = messageReader.ReadInt32();
if (SpawnManager.spawnedObjects[netId].OwnerClientId == MyClientId)
{
//We are current owner.
SpawnManager.spawnedObjects[netId].InvokeBehaviourOnLostOwnership();
}
if(ownerClientId == MyClientId)
{
//We are new owner.
SpawnManager.spawnedObjects[netId].InvokeBehaviourOnGainedOwnership();
}
SpawnManager.spawnedObjects[netId].OwnerClientId = ownerClientId;
}
}