Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved NetCode and Spawn Classification Examples

Discussion in 'DOTS Dev Blitz Day 2022 - Q&A' started by Kmsxkuse, Dec 7, 2022.

  1. Kmsxkuse

    Kmsxkuse

    Joined:
    Feb 15, 2019
    Posts:
    306
    I was just wondering if there were examples of all three spawning modes (delayed, predicted, and player spawned) in the current netcode samples.

    For future reference because I have no clue what I'm looking at, can you please provide file names and line numbers of each instance in the asteroids sample.
     
  2. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    331
    • The default spawn behaviour is "Delayed". Calling `EntityManager.Instantiate()` with any entity ghost prefab (on the server) will give you this behaviour by default.
    • "Client Predicted" spawning is a layer on top of the above, where the client runs the same code as the server (via prediction) and therefore also spawns the entity (see Bullet spawning in Asteroids via the badly named `SteeringJob`. Note how the `SteeringSystem` is in the `[UpdateInGroup(typeof(PredictedSimulationSystemGroup))]`). The client bullet (which is a fake ghost) then gets mapped to the server bullet via the `DefaultGhostSpawnClassificationSystem`. If it fails to map within a time limit, we assume a miss-prediction occured, and the invalid client bullet is destroyed.
    • "Player Spawned" is "Client Predicted". It must be predicted, because the client does not have the authority to spawn entities by themselves. I.e. The only thing the client is allowed to do is predict spawn a ghost that it expects the server to also predict spawn.
     
    JohngUK and Kmsxkuse like this.
  3. Kmsxkuse

    Kmsxkuse

    Joined:
    Feb 15, 2019
    Posts:
    306
    So just calling EntityManager.Instantate() forms these predictions and the location, whether inside the predicted loop or on the client or both, determines the spawn category?
     
  4. NikiWalker

    NikiWalker

    Unity Technologies

    Joined:
    May 18, 2021
    Posts:
    331
    Correct. If you notice, there is (correction) only one bit of code in the `SteeringSystem` that is client specific.
    `isFirstFullTick == 1` (which is populated via the `NetworkTime` singleton).

    Because it's in the `PredictedSimulationSystemGroup`, it'll run identically on both the client and server. The only nuance is that on the client, prediction runs multiple times per frame, so isFirstFullTick ensures that you only create the bullet once on the client (the first time you predict this frame). I recommend reading this manual on prediction: https://docs.unity3d.com/Packages/com.unity.netcode@1.0/manual/prediction.html

    In earlier versions you had to explicitly define a client ghost as a predicted spawn (via an API), but we were able to remove that in 1.0. See CHANGELOG.md entry:
    • The GhostCollectionSystem.CreatePredictedSpawnPrefab API is deprecated as clients will now automatically have predict spawned ghost prefabs set up for them. They can instantiate prefabs the normal way and don't need to call this API.
     
    Kmsxkuse likes this.