Skip to content

Commit

Permalink
feat: NetworkIdentity lifecycle events (#88)
Browse files Browse the repository at this point in the history
NetworkIdentity now has events related to lifecycle.  This means you no longer override lifecycle events in NetworkBehaviors,  but you can call AddListener or hook to the events in the inspector.

BREAKING CHANGE: NetworkBehavior no longer has virtuals for lifecycle events
  • Loading branch information
uweeby committed Mar 22, 2020
1 parent 027173e commit 9a7c572
Show file tree
Hide file tree
Showing 17 changed files with 226 additions and 270 deletions.
4 changes: 3 additions & 1 deletion Assets/Mirror/Components/NetworkSceneChecker.cs
Expand Up @@ -31,9 +31,11 @@ void Awake()
{
currentScene = gameObject.scene;
if (LogFilter.Debug) Debug.Log($"NetworkSceneChecker.Awake currentScene: {currentScene}");

netIdentity.OnStartServer.AddListener(OnStartServer);
}

public override void OnStartServer()
public void OnStartServer()
{
if (!sceneCheckerObjects.ContainsKey(currentScene))
sceneCheckerObjects.Add(currentScene, new HashSet<NetworkIdentity>());
Expand Down
Expand Up @@ -8,16 +8,19 @@ public class PlayerController : NetworkBehaviour
{
public CharacterController characterController;

void Awake()
{
netIdentity.OnStartLocalPlayer.AddListener(OnStartLocalPlayer);
}

void OnValidate()
{
if (characterController == null)
characterController = GetComponent<CharacterController>();
}

public override void OnStartLocalPlayer()
public void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();

Camera.main.orthographic = false;
Camera.main.transform.SetParent(transform);
Camera.main.transform.localPosition = new Vector3(0f, 3f, -8f);
Expand Down
8 changes: 6 additions & 2 deletions Assets/Mirror/Examples/AdditiveScenes/Scripts/RandomColor.cs
Expand Up @@ -4,9 +4,13 @@ namespace Mirror.Examples.Additive
{
public class RandomColor : NetworkBehaviour
{
public override void OnStartServer()
void Awake()
{
netIdentity.OnStartServer.AddListener(OnStartServer);
}

public void OnStartServer()
{
base.OnStartServer();
color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
}

Expand Down
19 changes: 10 additions & 9 deletions Assets/Mirror/Examples/Basic/Scripts/Player.cs
Expand Up @@ -23,6 +23,13 @@ public class Player : NetworkBehaviour
[SyncVar(hook = nameof(OnPlayerDataChanged))]
public int playerData;

void Awake()
{
netIdentity.OnStartServer.AddListener(OnStartServer);
netIdentity.OnStartClient.AddListener(OnStartClient);
netIdentity.OnStartLocalPlayer.AddListener(OnStartLocalPlayer);
}

// This is called by the hook of playerData SyncVar above
void OnPlayerDataChanged(int oldPlayerData, int newPlayerData)
{
Expand All @@ -31,10 +38,8 @@ void OnPlayerDataChanged(int oldPlayerData, int newPlayerData)
}

// This fires on server when this player object is network-ready
public override void OnStartServer()
public void OnStartServer()
{
base.OnStartServer();

// Set SyncVar values
playerNo = connectionToClient.connectionId;
playerColor = Random.ColorHSV(0f, 1f, 0.9f, 0.9f, 1f, 1f);
Expand All @@ -51,10 +56,8 @@ void UpdateData()
}

// This fires on all clients when this player object is network-ready
public override void OnStartClient()
public void OnStartClient()
{
base.OnStartClient();

// Make this a child of the layout panel in the Canvas
transform.SetParent(GameObject.Find("PlayersPanel").transform);

Expand All @@ -69,10 +72,8 @@ public override void OnStartClient()
}

// This only fires on the local client when this player object is network-ready
public override void OnStartLocalPlayer()
public void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();

// apply a shaded background to our player
image.color = new Color(1f, 1f, 1f, 0.1f);
}
Expand Down
7 changes: 5 additions & 2 deletions Assets/Mirror/Examples/Pong/Scripts/Ball.cs
Expand Up @@ -7,10 +7,13 @@ public class Ball : NetworkBehaviour
public float speed = 30;
public Rigidbody2D rigidbody2d;

public override void OnStartServer()
void Awake()
{
base.OnStartServer();
netIdentity.OnStartServer.AddListener(OnStartServer);
}

public void OnStartServer()
{
// only simulate ball physics on server
rigidbody2d.simulated = true;

Expand Down
9 changes: 6 additions & 3 deletions Assets/Mirror/Examples/Room/Scripts/NetworkRoomPlayerExt.cs
Expand Up @@ -6,11 +6,14 @@ namespace Mirror.Examples.NetworkRoom
[AddComponentMenu("")]
public class NetworkRoomPlayerExt : NetworkRoomPlayer
{
public override void OnStartClient()
void Awake()
{
if (LogFilter.Debug) Debug.LogFormat("OnStartClient {0}", SceneManager.GetActiveScene().name);
netIdentity.OnStartClient.AddListener(OnStartClient);
}

base.OnStartClient();
public void OnStartClient()
{
if (LogFilter.Debug) Debug.LogFormat("OnStartClient {0}", SceneManager.GetActiveScene().name);
}

public override void OnClientEnterRoom()
Expand Down
9 changes: 6 additions & 3 deletions Assets/Mirror/Examples/Room/Scripts/PlayerController.cs
Expand Up @@ -8,16 +8,19 @@ public class PlayerController : NetworkBehaviour
{
public CharacterController characterController;

void Awake()
{
netIdentity.OnStartLocalPlayer.AddListener(OnStartLocalPlayer);
}

void OnValidate()
{
if (characterController == null)
characterController = GetComponent<CharacterController>();
}

public override void OnStartLocalPlayer()
public void OnStartLocalPlayer()
{
base.OnStartLocalPlayer();

Camera.main.orthographic = false;
Camera.main.transform.SetParent(transform);
Camera.main.transform.localPosition = new Vector3(0f, 3f, -8f);
Expand Down
8 changes: 6 additions & 2 deletions Assets/Mirror/Examples/Room/Scripts/RandomColor.cs
Expand Up @@ -4,9 +4,13 @@ namespace Mirror.Examples.NetworkRoom
{
public class RandomColor : NetworkBehaviour
{
public override void OnStartServer()
void Awake()
{
netIdentity.OnStartServer.AddListener(OnStartServer);
}

public void OnStartServer()
{
base.OnStartServer();
color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
}

Expand Down
7 changes: 6 additions & 1 deletion Assets/Mirror/Examples/Room/Scripts/Spawner.cs
Expand Up @@ -6,7 +6,12 @@ public class Spawner : NetworkBehaviour
{
public NetworkIdentity prizePrefab;

public override void OnStartServer()
void Awake()
{
netIdentity.OnStartServer.AddListener(OnStartServer);
}

public void OnStartServer()
{
for (int i = 0; i < 10; i++)
SpawnPrize();
Expand Down
7 changes: 6 additions & 1 deletion Assets/Mirror/Examples/Tanks/Scripts/Projectile.cs
Expand Up @@ -8,7 +8,12 @@ public class Projectile : NetworkBehaviour
public Rigidbody rigidBody;
public float force = 1000;

public override void OnStartServer()
void Awake()
{
netIdentity.OnStartServer.AddListener(OnStartServer);
}

public void OnStartServer()
{
Invoke(nameof(DestroySelf), destroyAfter);
}
Expand Down
40 changes: 0 additions & 40 deletions Assets/Mirror/Runtime/NetworkBehaviour.cs
Expand Up @@ -796,46 +796,6 @@ internal void DeSerializeObjectsDelta(NetworkReader reader)
}
}

/// <summary>
/// This is invoked on clients when the server has caused this object to be destroyed.
/// <para>This can be used as a hook to invoke effects or do client specific cleanup.</para>
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
///<summary>Called on clients when the server destroys the GameObject.</summary>
public virtual void OnNetworkDestroy() { }

/// <summary>
/// This is invoked for NetworkBehaviour objects when they become active on the server.
/// <para>This could be triggered by NetworkServer.Listen() for objects in the scene, or by NetworkServer.Spawn() for objects that are dynamically created.</para>
/// <para>This will be called for objects on a "host" as well as for object on a dedicated server.</para>
/// </summary>
public virtual void OnStartServer() { }

/// <summary>
/// Called on every NetworkBehaviour when it is activated on a client.
/// <para>Objects on the host have this function called, as there is a local client on the host. The values of SyncVars on object are guaranteed to be initialized correctly with the latest state from the server when this function is called on the client.</para>
/// </summary>
public virtual void OnStartClient() { }

/// <summary>
/// Called when the local player object has been set up.
/// <para>This happens after OnStartClient(), as it is triggered by an ownership message from the server. This is an appropriate place to activate components or functionality that should only be active for the local player, such as cameras and input.</para>
/// </summary>
public virtual void OnStartLocalPlayer() { }

/// <summary>
/// This is invoked on behaviours that have authority, based on context and <see cref="NetworkIdentity.hasAuthority">NetworkIdentity.hasAuthority</see>.
/// <para>This is called after <see cref="OnStartServer">OnStartServer</see> and before <see cref="OnStartClient">OnStartClient.</see></para>
/// <para>When <see cref="NetworkIdentity.AssignClientAuthority"/> is called on the server, this will be called on the client that owns the object. When an object is spawned with <see cref="NetworkServer.Spawn">NetworkServer.Spawn</see> with a NetworkConnection parameter included, this will be called on the client that owns the object.</para>
/// </summary>
public virtual void OnStartAuthority() { }

/// <summary>
/// This is invoked on behaviours when authority is removed.
/// <para>When NetworkIdentity.RemoveClientAuthority is called on the server, this will be called on the client that owns the object.</para>
/// </summary>
public virtual void OnStopAuthority() { }

/// <summary>
/// Callback used by the visibility system to (re)construct the set of observers that can see this object.
/// <para>Implementations of this callback should add network connections of players that can see this object to the observers set.</para>
Expand Down
10 changes: 5 additions & 5 deletions Assets/Mirror/Runtime/NetworkClient.cs
Expand Up @@ -819,7 +819,7 @@ void UnSpawn(NetworkIdentity identity)
{
Guid assetId = identity.assetId;

identity.OnNetworkDestroy();
identity.NetworkDestroy();
if (unspawnHandlers.TryGetValue(assetId, out UnSpawnDelegate handler) && handler != null)
{
handler(identity.gameObject);
Expand Down Expand Up @@ -892,7 +892,7 @@ void ApplySpawnPayload(NetworkIdentity identity, SpawnMessage msg)
if (isSpawnFinished)
{
identity.NotifyAuthority();
identity.OnStartClient();
identity.StartClient();
CheckForLocalPlayer(identity);
}
}
Expand Down Expand Up @@ -992,7 +992,7 @@ internal void OnObjectSpawnFinished(ObjectSpawnFinishedMessage _)
foreach (NetworkIdentity identity in Spawned.Values.OrderBy(uv => uv.netId))
{
identity.NotifyAuthority();
identity.OnStartClient();
identity.StartClient();
CheckForLocalPlayer(identity);
}
isSpawnFinished = true;
Expand Down Expand Up @@ -1049,7 +1049,7 @@ internal void OnHostClientSpawn(SpawnMessage msg)

localObject.hasAuthority = msg.isOwner;
localObject.NotifyAuthority();
localObject.OnStartClient();
localObject.StartClient();
localObject.OnSetHostVisibility(true);
CheckForLocalPlayer(localObject);
}
Expand Down Expand Up @@ -1102,7 +1102,7 @@ void CheckForLocalPlayer(NetworkIdentity identity)
{
// Set isLocalPlayer to true on this NetworkIdentity and trigger OnStartLocalPlayer in all scripts on the same GO
identity.connectionToServer = connection;
identity.OnStartLocalPlayer();
identity.StartLocalPlayer();

if (LogFilter.Debug) Debug.Log("ClientScene.OnOwnerMessage - player=" + identity.name);
}
Expand Down

0 comments on commit 9a7c572

Please sign in to comment.