# Predicted Hierarchy

The `Predicted Hierarchy` system provides a set of methods for dynamically creating, deleting, and managing objects within the simulation. These methods allow developers to interact with the hierarchy in a way that feels natural and Unity-like, while ensuring that all changes are predictable and synchronized with the server.

***

**Pieces**

Every GameObject inside a spawned prefab that carries at least one `PredictedIdentity` is a piece with its own `PredictedObjectID`. The prefab root is always a piece, and nested identity-bearing children each get their own id too. This means you can address, delete, and reparent parts of a prefab individually instead of treating the whole instance as one unit.

* `Create` returns the id of the root piece. Ids for the remaining pieces of the same instance are allocated in a contiguous block right after it, in the prefab's hierarchy order.
* Spawning always instantiates the complete prefab and then prunes any pieces the current state says are deleted. Because all surviving pieces come from the same instantiation, serialized references between them keep working on every peer, including late joiners.
* Scene objects follow the same rules: nested identity-bearing children of a scene object are individually addressable pieces.

***

**Key Methods**

1. **Object Creation**:
   *   **`Create` Methods**:

       * Instantiates objects in the simulation.
       * Supports creating objects from prefab IDs or `GameObject` references.
       * Optionally allows specifying position, rotation, and owner.
       * Returns the `PredictedObjectID` of the root piece.

       ```csharp
       public PredictedObjectID? Create(int prefabId, PlayerID? owner = null);
       public PredictedObjectID? Create(int prefabId, Vector3 position, Quaternion rotation, PlayerID? owner = null);
       public PredictedObjectID? Create(GameObject prefab, PlayerID? owner = null);
       public PredictedObjectID? Create(GameObject prefab, Vector3 position, Quaternion rotation, PlayerID? owner = null);
       ```
   *   **Parented `Create` Overloads**:

       * Instantiates an object already attached to a parent, with a local pose relative to it.
       * The parent link is part of the spawn, so late joiners and replays reproduce it.
       * The `Transform` overloads resolve the predicted object themselves. The transform must be on a GameObject carrying a `PredictedIdentity`; passing a plain nested transform logs an error, attaching at arbitrary child transforms is not supported.

       ```csharp
       public PredictedObjectID? Create(int prefabId, Vector3 localPosition, Quaternion localRotation, PredictedComponentID parent, PlayerID? owner = null);
       public PredictedObjectID? Create(GameObject prefab, Vector3 localPosition, Quaternion localRotation, PredictedComponentID parent, PlayerID? owner = null);
       public PredictedObjectID? Create(GameObject prefab, Vector3 localPosition, Quaternion localRotation, PredictedIdentity parent, PlayerID? owner = null);
       public PredictedObjectID? Create(int prefabId, Vector3 localPosition, Quaternion localRotation, Transform parent, PlayerID? owner = null);
       public PredictedObjectID? Create(GameObject prefab, Vector3 localPosition, Quaternion localRotation, Transform parent, PlayerID? owner = null);
       ```
   *   **`TryCreate` Methods**:

       * Attempts to create an object and returns a boolean indicating success.
       * Outputs the `PredictedObjectID` of the created object.
       * Fails if prefab id is invalid or prefab isn't part of the registered prefab list.

       ```csharp
       public bool TryCreate(int prefabId, out PredictedObjectID id, PlayerID? owner = null);
       public bool TryCreate(GameObject prefab, out PredictedObjectID id, PlayerID? owner = null);
       ```
2. **Object Deletion**:
   *   **`Delete` Methods**:

       * Removes pieces from the simulation. Any piece can be deleted, not just prefab roots.
       * Deleting a piece also deletes the same-instance pieces attached under it. Pieces that were reparented away survive, and foreign pieces that were physically nested under it are detached and kept alive.
       * Deleting a root while some of its pieces live elsewhere promotes the survivors to standalone objects.
       * Handles both pooled and non-pooled objects.

       ```csharp
       public void Delete(PredictedObjectID? id);
       public void Delete(PredictedIdentity pid);
       public void Delete(GameObject go);
       ```

       `Delete(GameObject)` performs a real delete of that piece. If you only want a cheap reversible toggle, use `PredictedGameObject.SetActive` instead.
3. **Object Retrieval**:
   *   **`GetGameObject`**:

       * Retrieves the `GameObject` associated with a `PredictedObjectID`.

       ```csharp
       public GameObject GetGameObject(PredictedObjectID? id);
       ```
   *   **`GetComponent`**:

       * Retrieves a specific component from the `GameObject` associated with a `PredictedObjectID`.

       ```csharp
       public T GetComponent<T>(PredictedObjectID? id) where T : Component;
       ```
   *   **`TryGetId`**:

       * Retrieves the `PredictedObjectID` associated with a `GameObject`. Works for any piece, including nested ones.

       ```csharp
       public bool TryGetId(GameObject go, out PredictedObjectID id);
       ```
   *   **`TryGetGameObject`**:

       * Retrieves the `GameObject` associated with a `PredictedObjectID` and returns a boolean indicating success.

       ```csharp
       public bool TryGetGameObject(PredictedObjectID? id, out GameObject go);
       ```
4. **Instance Queries**:
   *   **`TryGetRootId`** resolves the root piece of the instance a piece belongs to.
   *   **`SameInstance`** checks whether two pieces were spawned as part of the same instance.
   *   **`PredictedIdentity.rootObjectId`** is a convenience shortcut for the root piece id of the identity's instance.

       ```csharp
       public bool TryGetRootId(PredictedObjectID pieceId, out PredictedObjectID rootId);
       public bool SameInstance(PredictedObjectID a, PredictedObjectID b);
       public bool SameInstance(PredictedIdentity a, PredictedIdentity b);
       ```

***

**Who Can Create and Delete**

Prediction is server authoritative, whatever your network rules say: a `Create` or `Delete` only sticks when the server performs it. A client can still call them, but the result is only a guess. When the next verified server state arrives, the client rolls back to it, and anything the server never created is removed and sent back to the pool. That's why a client-side `hierarchy.Create` from a UI button or another `MonoBehaviour` callback shows the object for a frame or a few ticks and then it's gone. On a host it seems to work, because the host is the server.

To spawn from a client action, get the request to the server in one of these ways:

*   **As input (predicted)**: put the request in your input and call `Create` inside `Simulate`. The client spawns it right away as a prediction, the server runs the same input and makes it real, and the two match up. Use a counter rather than a one-tick `bool`: the server repeats the last input when one goes missing, and a counter makes a repeat or a lost tick harmless.

    ```csharp
    public struct SpawnInput : IPredictedData
    {
        public uint spawnCount;
        public void Dispose() { }
    }

    public struct SpawnerState : IPredictedData<SpawnerState>
    {
        public uint handledSpawns;
        public void Dispose() { }
    }

    public class PlayerSpawner : PredictedIdentity<SpawnInput, SpawnerState>
    {
        [SerializeField] private GameObject prefab;
        private uint _spawnClicks;

        // Hook the UI button to this instead of calling hierarchy.Create directly
        public void OnSpawnClicked() => _spawnClicks++;

        protected override void UpdateInput(ref SpawnInput input)
        {
            input.spawnCount = _spawnClicks;
        }

        protected override void Simulate(SpawnInput input, ref SpawnerState state, float delta)
        {
            while (state.handledSpawns < input.spawnCount)
            {
                state.handledSpawns++;
                predictionManager.hierarchy.Create(prefab, transform.position, transform.rotation, owner);
            }
        }
    }
    ```
*   **Through a `ServerRpc` (not predicted)**: send the request to the server and call `Create` there. The object appears on the client once the server's state comes back, after a round trip. This fits menus, lobby buttons, and anything that doesn't need instant feedback.

    ```csharp
    public class SpawnMenu : NetworkBehaviour
    {
        [SerializeField] private PredictionManager predictionManager;
        [SerializeField] private GameObject playerPrefab;

        // UI button -> this
        public void OnSpawnClicked() => RequestSpawn();

        [ServerRpc(requireOwnership: false)]
        private void RequestSpawn(RPCInfo info = default)
        {
            predictionManager.hierarchy.Create(playerPrefab, Vector3.zero, Quaternion.identity, info.sender);
        }
    }
    ```

`Delete` works the same way. Deleting on a client outside `Simulate` gets undone by the next server state.

***

**Reparenting**

Transform parents are not synchronized by default. A piece without a [`PredictedParent`](https://purrnet.dev/docs/client-side-prediction/built-in-components/predicted-parent) component always belongs to its authored prefab parent as far as the simulation is concerned, and physically reparenting it is treated as client-local decoration. Add `PredictedParent` to a piece to make its parent part of predicted state, so reparenting rolls back, replays, and reaches late joiners like any other state change.

Delete cascades follow the simulated graph, meaning `PredictedParent` state where present and the authored default otherwise. Local-only decoration never changes what gets deleted.

***

**Prefabs and Pooling**

* Predicted object creation uses a central `PredictedPrefabs` asset referenced by `PredictionManager`.
* Prefabs can opt into pooling. Deleted pieces are kept in a pool for the rollback window so a mispredicted delete can resurrect the exact same objects, serialized references intact.
* When reconciling, pooled objects may be disabled/enabled; avoid parenting critical visuals to the root identity if you rely on `SetActive` state, or use `PredictedTransform`'s optional graphics unparenting.

***

**Spawning Network Identities**

_Applies to <1.4.0:_

* Use `PredictedIdentitySpawner` to mirror a set of `NetworkIdentity` objects between server and clients based on prediction state.
* The spawner coordinates early spawn, observer assignment, ownership, and finalization in both server and verified client passes.

_Applies to >=1.4.0:_

* Any `NetworkIdentity` inside a predicted prefab is spawned automatically when the instance enters the authoritative topology and despawned when it leaves. Ownership and observers follow the predicted object.
* Clients only spawn them once the instance is part of a verified frame, so speculative creates and rollbacks never touch PurrNet.
* See [Network Identities in Predicted Prefabs](https://purrnet.dev/docs/client-side-prediction/built-in-components/predicted-identity-spawner) for the details and the upgrade note on the old `PredictedIdentitySpawner` component.

***

**Runtime-Created Objects**

Every `PredictedIdentity` must either exist in the scene file at discovery time or be spawned through `PredictionManager.hierarchy.Create`. An identity instantiated at runtime outside the hierarchy (via `Object.Instantiate` or procedural creation) is never discovered during registration: it never simulates or syncs, its state stays uninitialized, and physics events involving it pass a null `other` to callbacks.

The manager logs a warning during scene discovery when it finds such an identity, naming the object and the fix: spawn it with `hierarchy.Create`, keep it in the scene file, or enable **Include Instantiated Scene Objects** in your Network Rules if you intentionally instantiate scene-style predicted objects at runtime.

_Applies to >=1.4.1:_

**Pre-Spawn Hook**

`PredictedHierarchy.onPreSpawn` is a static event raised when a GameObject enters a prediction world, before any of its predicted identities run setup (`OnPreSetup`, `LateAwake`). It fires for prefab instances, whether freshly instantiated or taken from the prefab pool, and for scene objects when they are registered. It is the place to inject dependencies, like `HierarchyV2.onPreSpawn` for network identities. Rollbacks that bring back a GameObject the hierarchy is still holding do not raise it again.

```csharp
PredictedHierarchy.onPreSpawn += spawn =>
{
    // spawn.gameObject, spawn.predictionManager, spawn.isSceneObject, spawn.fromPool
    container.InjectGameObject(spawn.gameObject);
};
```

***

**Best Practices**

* Only create/destroy from within simulation code paths (or via machine/state transitions) to keep behavior deterministic under replay. For client actions such as UI buttons, see **Who Can Create and Delete** above.
* Never `Object.Instantiate` a predicted prefab directly; go through `hierarchy.Create` so the object is registered and simulated.
* Prefer `TryCreate` and check the boolean to handle invalid or unregistered prefabs gracefully.
* Store and pass around `PredictedObjectID` rather than `GameObject` to remain resilient across rollbacks.
* When you need to know if two ids belong to the same spawned prefab, use `SameInstance` or `TryGetRootId` instead of comparing raw ids.

***

**Upgrade Notes**

Older versions assigned one id per spawned prefab. Now every identity-bearing GameObject gets its own id:

* `id.objectId` identifies the piece, not the whole instance. Code that compared object ids to group components of the same prefab should use `SameInstance` or `rootObjectId`.
* `TryGetId` now succeeds for nested piece GameObjects, not just prefab roots.
* `Delete(GameObject)` used to reroute to `PredictedGameObject.SetActive(false)`. It now deletes the piece for real.
* The wire and state formats changed, so all peers must run the same version.
