OnStartServer isServer check + initialization moved to NetworkServer for cleaner code

This commit is contained in:
vis2k 2022-11-10 20:11:12 +01:00
parent d79de6bf0c
commit f2b0f2bab2
4 changed files with 22 additions and 26 deletions

View File

@ -635,13 +635,6 @@ void OnDestroy()
internal void OnStartServer()
{
// do nothing if already spawned
if (isServer)
return;
// set isServer flag
isServer = true;
// set isLocalPlayer earlier, in case OnStartLocalplayer is called
// AFTER OnStartClient, in which case it would still be falsse here.
// many projects will check isLocalPlayer in OnStartClient though.

View File

@ -1182,7 +1182,16 @@ static void SpawnObject(GameObject obj, NetworkConnection ownerConnection)
if (ownerConnection is LocalConnectionToClient)
identity.isOwned = true;
identity.OnStartServer();
// only call OnStartServer if not spawned yet.
// check used to be in NetworkIdentity. may not be necessary anymore.
if (!identity.isServer)
{
// configure NetworkIdentity
identity.isServer = true;
// callback after all fields were set
identity.OnStartServer();
}
// Debug.Log($"SpawnObject instance ID {identity.netId} asset ID {identity.assetId}");

View File

@ -108,9 +108,8 @@ public void IsServerOnly()
{
CreateNetworked(out _, out NetworkIdentity identity, out EmptyBehaviour emptyBehaviour);
// call OnStartServer so isServer is true
identity.OnStartServer();
Assert.That(identity.isServer, Is.True);
// set isServer
identity.isServer = true;
// isServerOnly should be true when isServer = true && isClient = false
Assert.That(emptyBehaviour.isServer, Is.True);
@ -558,9 +557,8 @@ public void GetSyncVarGameObjectOnServer()
{
CreateNetworked(out GameObject gameObject, out NetworkIdentity identity, out NetworkBehaviourGetSyncVarGameObjectComponent comp);
// call OnStartServer so isServer is true
identity.OnStartServer();
Assert.That(identity.isServer, Is.True);
// set isServer
identity.isServer = true;
// create a syncable GameObject
CreateNetworked(out GameObject go, out NetworkIdentity ni);
@ -584,9 +582,8 @@ public void GetSyncVarGameObjectOnServerNull()
{
CreateNetworked(out GameObject gameObject, out NetworkIdentity identity, out NetworkBehaviourGetSyncVarGameObjectComponent comp);
// call OnStartServer and assign netId so isServer is true
identity.OnStartServer();
Assert.That(identity.isServer, Is.True);
// set isServer
identity.isServer = true;
// get it on the server. null should work fine.
GameObject result = comp.GetSyncVarGameObjectExposed();
@ -719,9 +716,8 @@ public void GetSyncVarNetworkIdentityOnServer()
{
CreateNetworked(out GameObject _, out NetworkIdentity identity, out NetworkBehaviourGetSyncVarNetworkIdentityComponent comp);
// call OnStartServer so isServer is true
identity.OnStartServer();
Assert.That(identity.isServer, Is.True);
// set isServer
identity.isServer = true;
// create a syncable GameObject
CreateNetworked(out _, out NetworkIdentity ni);
@ -742,9 +738,8 @@ public void GetSyncVarNetworkIdentityOnServerNull()
{
CreateNetworked(out GameObject _, out NetworkIdentity identity, out NetworkBehaviourGetSyncVarNetworkIdentityComponent comp);
// call OnStartServer so isServer is true
identity.OnStartServer();
Assert.That(identity.isServer, Is.True);
// set isServer
identity.isServer = true;
// get it on the server. null should work fine.
NetworkIdentity result = comp.GetSyncVarNetworkIdentityExposed();

View File

@ -551,9 +551,8 @@ public void AssignAndRemoveClientAuthority()
// server is needed
NetworkServer.Listen(1);
// call OnStartServer so that isServer is true
identity.OnStartServer();
Assert.That(identity.isServer, Is.True);
// set isServer to true
identity.isServer = true;
// assign authority
result = identity.AssignClientAuthority(owner);