Skip to content

Commit

Permalink
feat!: clearing SceneId when spawning object with PrefabHash
Browse files Browse the repository at this point in the history
when spawning an object `SceneId` is used first if set. When spawning and explicitly setting `PrefabHash`  mirage now clears the `SceneId` to ensure that the new `PrefabHash` is used

BREAKING CHANGE: SceneId is now cleared when calling Spawn with PrefabHash
  • Loading branch information
James-Frowen authored Dec 6, 2024
1 parent a492705 commit d638088
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Assets/Mirage/Runtime/ServerObjectManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,10 @@ private static void ThrowIfNoCharacter(INetworkPlayer player)
/// <summary>
/// Assigns <paramref name="prefabHash"/> to the <paramref name="identity"/> and then spawns it with <paramref name="owner"/>
/// <para>
/// <see cref="NetworkIdentity.PrefabHash"/> can only be set on an identity if the current value is Empty
/// <see cref="NetworkIdentity.PrefabHash"/> can only be set to a non-zero value.
/// </para>
/// <para>
/// <see cref="NetworkIdentity.SceneId"/> will be cleared when calling this method, this will ensure that the object is spawned using the new PrefabHash rather than SceneId
/// </para>
/// <para>
/// This method is useful if you are creating network objects at runtime and both server and client know what <see cref="Guid"/> to set on an object
Expand All @@ -355,6 +358,13 @@ public void Spawn(NetworkIdentity identity, int prefabHash, INetworkPlayer owner
ThrowIfPrefab(identity);

identity.PrefabHash = prefabHash;

if (identity.IsSceneObject)
{
if (logger.LogEnabled()) logger.Log($"Clearing SceneId on {identity} because setting prefabHash when spawning. Old sceneId={identity.SceneId:X} New PrefabHash:{prefabHash:X}");
identity.ClearSceneId();
}

Spawn(identity, owner);
}

Expand Down

0 comments on commit d638088

Please sign in to comment.