Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Entity System Architecture with Unity - Unite Europe 2015

3,346 views

Published on

Entity System Architecture with Unity - Unite Europe 2015
Entitas - open source Entity Component System for C# and Unity: https://github.com/sschmid/Entitas-CSharp

Published in: Software
  • Be the first to comment

Entity System Architecture with Unity - Unite Europe 2015

  1. 1. Entity system architecture with Unity
  2. 2. Maxim Zaks @icex33 | github.com/mzaks Simon Schmid @s_schmid | github.com/sschmid
  3. 3. Unity pain points • Testability • Code sharing • Co-dependent logic • Querying • Deleting code
  4. 4. Testability
  5. 5. Code sharing
  6. 6. Co-dependent logic
  7. 7. Co-dependent logic
  8. 8. Co-dependent logic
  9. 9. Querying
  10. 10. Deleting code
  11. 11. Entitas Match One Demo
  12. 12. PositionComponent using Entitas; public class PositionComponent : IComponent { public int x; public int y; }
  13. 13. GameBoardElementComponent using Entitas; public class GameBoardElementComponent : IComponent {}
  14. 14. Entity • Add • Replace • Remove
  15. 15. Create Blocker Entity public static Entity CreateBlocker(this Pool pool, int x, int y) { return pool.CreateEntity() .IsGameBoardElement(true) .AddPosition(x, y) .AddResource(Res.Blocker); }
  16. 16. Pool • Create Entity • Destroy Entity • Get all Entities • Get Group
  17. 17. Group Performance optimization for querying Matcher is a filter description
  18. 18. Get Group _pool.GetGroup( Matcher.AllOf( Matcher.GameBoardElement, Matcher.Position ) );
  19. 19. +------------------+ | Pool | |------------------| | e e | +-----------+ | e e---|----> | Entity | | e e | |-----------| | e e e | | Component | | e e | | | +-----------+ | e e | | Component-|----> | Component | | e e e | | | |-----------| | e e e | | Component | | Data | +------------------+ +-----------+ +-----------+ | | | +-------------+ | | e | Groups | | e e | +---> | +------------+ | e | | | | e | e | e | +--------|----+ e | | e | | e e | +------------+
  20. 20. Behaviour
  21. 21. System • Start / Execute • No State
  22. 22. MoveSystem public void Execute() { var movables = _pool.GetGroup( Matcher.AllOf( Matcher.Move, Matcher.Position )); foreach (var e in movables.GetEntities()) { var move = e.move; var pos = e.position; e.ReplacePosition(pos.x, pos.y + move.speed, pos.z); } }
  23. 23. Chain of Responsibility +------------+ +------------+ +------------+ +------------+ | | | | | | | | | System | +---> | System | +---> | System | +---> | System | | | | | | | | | +------------+ +------------+ +------------+ +------------+
  24. 24. return new Systems() .Add(pool.CreateSystem <GameBoardSystem> ()) .Add(pool.CreateSystem <CreateGameBoardCacheSystem> ()) .Add(pool.CreateSystem <FallSystem> ()) .Add(pool.CreateSystem <FillSystem> ()) .Add(pool.CreateSystem <ProcessInputSystem> ()) .Add(pool.CreateSystem <RemoveViewSystem> ()) .Add(pool.CreateSystem <AddViewSystem> ()) .Add(pool.CreateSystem <RenderPositionSystem> ()) .Add(pool.CreateSystem <DestroySystem> ()) .Add(pool.CreateSystem <ScoreSystem> ());
  25. 25. Reacting to changes in a Group • On Entity added • On Entity removed
  26. 26. ScoreLabelController void Start() { _pool.GetGroup(Matcher.Score).OnEntityAdded += (group, entity) => updateScore(entity.score.value); updateScore(_pool.score.value); } void updateScore(int score) { _label.text = "Score " + score; }
  27. 27. Reactive System • Executed only when entities in group have changed
  28. 28. Render Position System public class RenderPositionSystem : IReactiveSystem { public IMatcher GetTriggeringMatcher() { return Matcher.AllOf(Matcher.View, Matcher.Position); } public GroupEventType GetEventType() { return GroupEventType.OnEntityAdded; } public void Execute(Entity[] entities) { foreach (var e in entities) { var pos = e.position; e.view.gameObject.transform.position = new Vector3(pos.x, pos.y); } } }
  29. 29. Optimizations
  30. 30. Components mutable vs immutable
  31. 31. Entity Store components in Dictionary vs Array
  32. 32. Comparison Dictionary e.AddComponent(new PositionComponent()); var component = e.GetComponent<PositionComponent>(); Array e.AddComponent(new PositionComponent(), 5); var component = (PositionComponent)e.GetComponent(5);
  33. 33. Code Generator
  34. 34. Before Code Generation PositionComponent component; if (e.HasComponent(ComponentIds.Position)) { e.WillRemoveComponent(ComponentIds.Position); component = (PositionComponent)e.GetComponent(ComponentIds.Position); } else { component = new PositionComponent(); } component.x = 10; component.y = 10; e.ReplaceComponent(ComponentIds.Position, component);
  35. 35. After Code Generation e.ReplacePosition(10, 10);
  36. 36. Code Generator Demo
  37. 37. How does it work?
  38. 38. Visual Debugging Demo
  39. 39. Entitas is open source github.com/sschmid/Entitas- CSharp
  40. 40. Recap
  41. 41. Unity pain points • Testability • Code sharing • Co-dependent logic • Querying • Deleting code
  42. 42. Advantages • Straightforward to achieve Determinism and therefore Replay • Simulation Speed (2x, 4x) • Headless Simulation, just remove systems which rely on GameObjects (render systems) • Save Game (Serialization / Deserialization) send data to backend on change • For us it replaced all OO Design Patterns
  43. 43. Q&Atinyurl.com/entitas

×