Game Models - A Different Approach

1,193 views
1,083 views

Published on

The design and rules of games constantly change during development, invalidating your carefully engineered software from day to day. Entity systems are a great approach for getting rid of the many drawbacks of inheritance-based game models like the “diamond of death”, moving on to a much more flexible aggregation-based model which has been popular since Gas Powered Games’ Dungeon Siege.

Published in: Technology, Design
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,193
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
23
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Game Models - A Different Approach

  1. 1. Game Models A Different Approach Nick Prühs July 12, 2013
  2. 2. About Me „Best Bachelor“ Computer Science Kiel University, 2009 Master Games Hamburg University of Applied Sciences, 2011 Lead Programmer Daedalic Entertainment, 2011-2012 Co-Founder slash games, 2013 2 / 58
  3. 3. PART I ENTITY SYSTEMS 3 / 58
  4. 4. Objectives • To understand the disadvantages of inheritance- based game models • To learn how to build an aggregation-based game models • To understand the advantages and disadvantages of aggregation-based game models 4 / 58
  5. 5. “Say you’re an engineer… … set out to create a new Game Object System from scratch, and you’re going to ‘do it right the first time’. You talk to your designer and say ‘What kind of content are we going to have in this game?’ They respond with ‘Oh lots of stuff, trees, and birds, and bushes, and keys and locks and … <trailing off>’ And your eyes glaze over as you start thinking of fancy C++ ways to solve the problem. The object oriented programming sages tell you to try to determine Is-A relationships and abstract functionality and all that other fun stuff. You go to the book store and buy a C++ book just to be sure, and it tells you to fire up your $5000 UML editor. [...]” - Scott Bilas 5 / 58
  6. 6. Entities • object in your game world • can (or cannot)… • be visible • move around • attack • explode • be targeted • become selected • follow a path • common across all genres 6 / 58
  7. 7. Entities 7 / 58
  8. 8. Approach #1: Inheritance • Entity base class • that class and its subclasses encapsulate the main game logic 8 / 58
  9. 9. Example #1: Unreal Engine 3 • base class Actor • rendering • animation • sound • physics • almost everything in Unreal is an Actor • Pawn extends by taking damage • Projectile extends by spawning impact effects 9 / 58
  10. 10. Drawbacks of inheritance-based game models • Diamond of Death 10 / 58
  11. 11. Drawbacks of inheritance-based game models • code added to the root of the inheritance tree causes big overhead • code added to the leafs of the tree tends to get copied • root and leaf classes tend to get very big 11 / 58
  12. 12. Where is Waldo? public override void Update(float dt) { this.SystemManager.Update(dt); this.EventManager.ProcessEvents(dt); } 12 / 58
  13. 13. Where is Waldo? public override void Update(float dt) { this.SystemManager.Update(dt); this.EventManager.ProcessEvents(dt); } 13 / 58
  14. 14. Where is Waldo? public override void Update(float dt) { base.Update(dt); this.SystemManager.Update(dt); this.EventManager.ProcessEvents(dt); } 14 / 58
  15. 15. Drawbacks of inheritance-based game models • always need to understand all base classes along the inheritance tree • impossible to enforce calling base class functions • Someone will forget it. Trust me. • And you’re gonna spend your whole evening finding that one missing base.Update(). • deep class hierarchies will more likely run into call order issues 15 / 58
  16. 16. Inheritance-based game models are… • … difficult to develop • … difficult to maintain • … difficult to extend 16 / 58
  17. 17. “There are probably hundreds of ways… … you could decompose your systems and come up with a set of classes […], and eventually, all of them are wrong. This isn’t to say that they won’t work, but games are constantly changing, constantly invalidating your carefully planned designs. [...] So you hand off your new Game Object System and go work on other things. Then one day your designer says that they want a new type of “alien” asteroid that acts just like a heat seeking missile, except it’s still an asteroid.” - Scott Bilas 17 / 58
  18. 18. Approach #2: Aggregation • popular since Gas Powered Games’ Dungeon Siege • introduced long before • entities are aggregations of components • which in turn encapsulate independent functionality • corresponds to recommendations by the Gang of Four • “favor object composition over class inheritance” • similar approach is used by the Unity3D game engine • just for clarification: Unreal uses components as well, called ActorComponent 18 / 58
  19. 19. Approach #2a • create an Entity class • add references to all available components • has obvious disadvantages: • many component references will be null pointers for most entities • big unnecessary memory overhead • Entity class has to be updated each time a new component is introduced 19 / 58
  20. 20. Approach #2b • create an Entity class • introduce a common base class for components • entities hold a collection of Component objects • reduced the memory overhead • increased extensibility • already gets close to an optimal solution • easy to build, maintain and debug • easy to implement new design ideas without breaking existing code 20 / 58
  21. 21. However, we can do better. 21 / 58
  22. 22. Approach #2c: Entity Systems There is no Entity class at all. 22 / 58
  23. 23. Approach #2c: Entity Systems 23 / 58
  24. 24. Approach #2c: Entity Systems • game entities are nothing more than just an id • thus, no data or methods on entities • no methods on components, either: all functionality goes into what is called a system • PhysicsSystem • HealthSystem • FightSystem • entirely operate on their corresponding components 24 / 58
  25. 25. “All the data goes into the Components. All of it. Think you can take some “really common” data, e. g. the x-/y-/z-coordinates of the in-game object, and put it into the Entity itself? Nope. Don’t go there. As soon as you start migrating data into the Entity, you’ve lost. By definition the only valid place for the data is inside the Component.” - Adam Martin 25 / 58
  26. 26. Example #2: Simple Fight 26 / 58
  27. 27. Example #2: Simple Fight 27 / 58
  28. 28. Example #2: Simple Fight 28 / 58
  29. 29. Example #2: Simple Fight 29 / 58
  30. 30. Example #2: Simple Fight 30 / 58
  31. 31. Example #2: Simple Fight 31 / 58
  32. 32. Example #2: Simple Fight 32 / 58
  33. 33. Example #2: Simple Fight 33 / 58
  34. 34. Example #2: Simple Fight 34 / 58
  35. 35. Example #2: Simple Fight 35 / 58
  36. 36. Inter-System Communication Systems communicate by the means of events, only. • no coupling between systems • easy to add or remove systems at any time • great architectural advantage for general game features • need multiplayer? just send the events over the network! • need AI? just make it create events which are handled just like player input is! • need replays? just write all events with timestamps to a file! 36 / 58
  37. 37. Entity Systems – Implementation DEMO 37 / 58
  38. 38. Advantages of Entity Systems • update order is obvious • components can easily be pooled and re-used • independent systems can be updated by separate threads • data can easily be serialized and stored in a database 38 / 58
  39. 39. Disadvantages of Entity Systems (?) • lookups cause performance hit • resist the urge to add cross-component references – this would make you lose all of the advantages mentioned before • just don’t flood your system with unnecessary component types – just as you would always do • misbelief that it takes longer to “get the job done” • used at the InnoGames Game Jam #3 for creating a multi-platform multi-player real-time tactics game in just 48 hours – spending the little extra effort at the beginning pays off • Always. 39 / 58
  40. 40. Conclusion • inheritance-based game models show a lot of disadvantages • entity systems are easy to maintain and debug • provide great extensibility without the necessity of modifying existing code • show better performance characteristics for both memory and CPU load • easy to implement commonly used features • scripting • serialization • logging 40 / 58
  41. 41. PART II ENTITY SYSTEMS & UNITY 41 / 58
  42. 42. Objectives • To learn how to write .NET libraries for Unity3D • To learn how to use entity systems with Unity3D 42 / 58
  43. 43. Entity Systems & Unity 43 / 58
  44. 44. Entity Systems & Unity – Project Structure • Source folder • Unity project • Logic project • Build project • Unit Tests • Vendor folder • third party libraries 44 / 58
  45. 45. Entity Systems & Unity – Setup 1. Create Unity project as usual. 2. Create shared solution. 1. Switch target framework to .NET 3.5. 2. Add Unity project to game solution. 3. Create Build project. 1. Add reference to logic project. 2. Write batch file for copying .dlls to the Unity Plugins folder (see next slide). 3. Set post-build event. 45 / 58
  46. 46. Entity Systems & Unity – Post-Build Script REM Copies all libraries to the Unity Plugins folder. SET PATH_TO_UNITY_PROJECT=%1 SET DLL_TARGET_DIR=%PATH_TO_UNITY_PROJECT%AssetsPlugins SET DLL_SOURCE_DIR=%2 XCOPY "%DLL_SOURCE_DIR%*.dll" "%DLL_TARGET_DIR%" /D /Y Visual Studio Build Event: $(ProjectDir)PostBuild.bat $(ProjectDir)..Unity $(TargetDir) 46 / 58
  47. 47. Entity Systems & Unity – Write Logic Base • Game class • GameSystem base class • virtual initialization method • reference to type-casted game • GameEvent enum 47 / 58
  48. 48. Entity Systems & Unity – Link to Unity 1. Create GameBehaviour passing Unity Start and Update to the logic. 2. Create LogBehaviour passing all logic events to the Unity log. 3. Add game systems, components and events as desired. 4. Add entity-object-map to GameBehaviour. 5. Implement OnEntityCreated an OnEntityRemoved event handler. 48 / 58
  49. 49. Gotcha! Don’t forget to rebuild the solution! 49 / 58
  50. 50. Gotcha! Don’t forget to add new game systems to your game class! 50 / 58
  51. 51. Hint Override ToString in event data classes for more useful log output! 51 / 58
  52. 52. Entity Systems & Unity – Unit Testing After all, this is one of the reasons we did that, right? 1. Add the unit test framework of your choice to the Vendor folder (e.g. NUnit). 2. Add unit test project to the solution and add a reference to the framework. 3. Write unit tests as usual. 52 / 58
  53. 53. Gotcha! Don’t forget to initialize, start and update your game in unit tests! 53 / 58
  54. 54. Entity Systems & Unity – Setup MonoDevelop 1. Ensure solution file has version 11: Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2008 2. Add „After Build“ Custom Command in PostBuildLibraries project options. 54 / 58
  55. 55. Future Prospects • Attribute Tables • store arbitrary key-value-pairs • used for initializing all components of an entity • Blueprints • consist of a list of components and an attribute table • created with some kind of editor tool by designers • used for creating entites at run-time • Hierarchical Attribute Tables • used for overloading blueprints with specific entity attribute values • e.g. reduced initial health 55 / 58
  56. 56. Future Prospects • building physics system outside of Unity • requires own Vector2 and Vector3 data structures (and conversions) • position of Unity game objects should always reflect physics position • converting PDBs to MDBs • provides more verbose debug output in Unity 56 / 58
  57. 57. References • Mick West. Evolve Your Hierarchy. http://cowboyprogramming.com/2007/01/05/evolve-your- heirachy/, January 2007. • Levi Baker. Entity Systems Part 1: Entity and EntityManager. http://blog.chronoclast.com/2010/12/entity-systems-part-1-entity-and.html, December 2010. • Kyle Wilson. Game Object Structure: Inheritance vs. Aggregation. http://gamearchitect.net/Articles/GameObjects1.html, July 2002. • Adam Martin. Entity Systems are the future of MMOG development – Part 1. http://t- machine.org/index.php/2007/09/03/entity-systems-are-the-future-of-mmog-development-part- 1/, September 2007. • Adam Martin. Entity Systems: what makes good Components? good Entities? http://t- machine.org/index.php/2012/03/16/entity-systems-what-makes-good-components-good- entities/, March 2012. • Scott Bilas. A Data-Driven Game Object System. http://scottbilas.com/files/2002/gdc_san_jose/game_objects_slides_with_notes.pdf, Slides, GDC 2002. • Scott Bilas. A Data-Driven Game Object System. http://scottbilas.com/files/2002/gdc_san_jose/game_objects_paper.pdf, Paper, GDC 2002. • Insomniac Games. A Dynamic Component Architecture for High Performance Gameplay. http://www.insomniacgames.com/a-dynamic-component-architecture-for-high-performance- gameplay/, June 2010. 57 / 58
  58. 58. Thank you for your attention! Contact Mail dev@npruehs.de Blog http://www.npruehs.de Twitter @npruehs Github https://github.com/npruehs 58 / 58

×