Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

ECS and Determinism

Discussion in 'Entity Component System' started by Allan-Smith, Jul 15, 2018.

  1. Allan-Smith

    Allan-Smith

    Joined:
    Feb 7, 2012
    Posts:
    57
    Hi,

    I am developing a multiplayer strategy game and relying entirely on determinism to make sure simulation is equal across players. I just send the player inputs across the network and clients have to simulate the game using a lockstep. I really enjoy the ECS paradigm, but I felt that if I were to use the ECS, the whole nature of it not having a deterministic execution order and etc would make determinism impossible. Is there a currently a way to use the ECS and Job system in an entirely deterministic way, even if that means only using one thread or something like that?

    Currently I have 2 "layers" to my game. Network synced systems (need to be 100% deterministic) which calculate every decision for everything, and Local systems, which do basically visuals, interpolation based on networked decisions, etc etc... would be awesome if I could have Network synced systems running 100% deterministically on the main thread or something like that and the other systems running however the hell they want, but I get a feeling that currently it is impossible to achieve?

    At any rate, excited to see the system, cant wait to see it grow and to use it in other projects if I cant use it in this one,
    Best,
    Allan
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    ECS is designed to be eventually deterministic, it might be you are too early. Did you test the mathematics library for determinism?
     
  3. Allan-Smith

    Allan-Smith

    Joined:
    Feb 7, 2012
    Posts:
    57
    So far I'm working around using only fixed point for my network synced systems. I don't use physics and game logic doesn't rely on animations or anything of the like so even though its a bit of headache at times, I've been able to keep it deterministic thus far.
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The job system gurantees deterministic execution by default unless you opt out of it with various attributes on the containers. Between same platforms ECS should be deterministic.

    You need to take advantage of multiple world concept in ECS to guarantee determinism. Eg. bringing up a menu item, rendering with interpolated entities should be happening in a separate world.

    API for this is all available but documentation & samples on this topic is lacking at the moment. We will improve on that as we build more networking samples.
     
    Gekigengar, OndrejP and optimise like this.
  5. zyc_dc

    zyc_dc

    Joined:
    May 11, 2018
    Posts:
    42
    Very excited about it. I am building an RTS demo which needs multiple worlds, but has no clue how to transfer information from simulation world to presentation world. What would be the mechanism in future?
     
  6. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You can do that today. You simply create a system that has a reference to the EntityManager of the other world. Simplest is to transfer data via EntityManager.GetComponent / EntityManager.SetComponent.

    We found that using ISystemStateComponentData and reactive system patterns create the hookup for pulling data from one world to another and caching the entity from which the entity in the presentation world was created works best.
     
    OndrejP likes this.
  7. zyc_dc

    zyc_dc

    Joined:
    May 11, 2018
    Posts:
    42
    Cool! Will try it definitely. My another problem is that I want to run different worlds at different frame rates. For instance, the simulation runs at a low and fixed frame rate and the presentation world runs at the target frame rate. Is there any way I can do that for now except executing systems manually?
     
    Vanamerax likes this.
  8. Vanamerax

    Vanamerax

    Joined:
    Jan 12, 2012
    Posts:
    937
    In addition to this, I would also like to know if it is possible or if there are any plans to be able to manually control the update loop of a particular world (eg not only a single system, let unity still handle the update order of individual systems).

    For example, I would like to be able to pause and/or speed up the simulation speed based on player input.
     
  9. OndrejP

    OndrejP

    Joined:
    Jul 19, 2017
    Posts:
    296
    Does this mean that when I use producer Job which writes into container (e.g. concurrent queue), it will be deterministic no matter what number of threads it will run on?

    Does it work like this?
    Thread A processes entities 0-99 and writes 10 pieces of data into container section 0
    Thread B processes entities 100-199 and writes 5 pieces into container section 1
    Thread C processes entities 200-299 and writes 20 pieces into container section 2
    Thread A again processes entities 300-350 and writes 4 pieces into container section 3
    ...

    Then when reading on main thread, will I read "by sections"?
    That means 10 pieces, 5 pieces, 20 pieces, 4 pieces even that Thread A,B and C might write data simultaneously?
     
  10. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    3,977
    NativeQueue.ParallelWriter is non-deterministic. NativeStream and EntityCommandBuffer are deterministic though and will probably meet your needs.