Design and logic for user-facing site https://exmods.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

002-ecs_devo.html 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta name="theme-color" content="#20c420" />
  5. <meta name="author" content="Exmods community" />
  6. <meta name="keywords" content="exmods,gamecraft,modding,mod" />
  7. <meta name="og:type" content="website" />
  8. <meta name="og:title" content="Exmods" />
  9. <meta name="og:description" content="Gamecraft modding website" />
  10. <meta name="description" content="Gamecraft modding website" />
  11. <meta name="og:image" content="../static/favicon.png" />
  12. <meta name="og:url" content="https://www.exmods.org" />
  13. <meta charset="UTF-8" />
  14. <link rel="icon" href="../static/favicon.png" type="image/x-icon"/>
  15. <link rel="shortcut icon" href="../static/favicon.png" type="image/x-icon"/>
  16. <link rel="stylesheet" href="../static/style.css" type="text/css" />
  17. <title>Exmods | Modding ECS</title>
  18. </head>
  19. <body>
  20. <h3><a href="index.html" title="You know your browser has a back button, right?">Back to list</a></h3>
  21. <h1>Modding ECS in Gamecraft</h1>
  22. <div class="text-content">
  23. <p>
  24. <span class="tab"></span>
  25. Gamecraft is built on an entity component system created by FreeJam's CTO called <a href="https://github.com/sebas77/Svelto.ECS">Svelto.ECS</a>, which is a quite powerful piece of open-source software.
  26. The ECS paradigm is a relatively novel programming concept, but many frameworks exist including one made by the Unity Engine devs specifically for their engine called <a href="https://connect.unity.com/p/what-is-unity-ecs">UECS</a>.
  27. ECS is a data-oriented system which uses "entities" instead of "objects" from object-oriented programming.
  28. An entity is comprised of independent components which act like individual properties for an object in OOP.
  29. All of the components are stored in some sort of fast in-memory database (Svelto.ECS uses a Dictionary).
  30. What makes ECS great is that it's fast,
  31. since the computer only needs to worry about the component in cache instead of the entire object,
  32. and it makes the code easier to maintain,
  33. since you can add a new component to an existing entity without touching other components in the same entity.
  34. </p>
  35. <p>
  36. <span class="tab"></span>
  37. ECS is great, but it can be a lot to wrap your head around.
  38. I've found that the best way to understand it is to jump in, swear at your computer a bit, and slowly accept it for what it is.
  39. A lot of material I've seen says that ECS is really only practical for games,
  40. but the idea of data-oriented design is a universal concept.
  41. Data-oriented programs are the future, since big data and machine learning are focused on number-crunching.
  42. So if you need a reason to persevere through your ECS learning, think of that.
  43. Unfortunately, ECS is unlikely to be the way to get there, but it is the way to make a (good) modern game.
  44. And Gamecraft is one of those modern good games... well at least it uses ECS.
  45. </p>
  46. <p>
  47. <span class="tab"></span>
  48. Gamecraft is FreeJam's first completely ECS-based game.
  49. Robocraft and CardLife both used Svelto.ECS as well, but the game logic wasn't completely reliant on it.
  50. Because all of Gamecraft's data is handled by Svelto.ECS,
  51. it's easier to mod than CardLife and Robocraft (or so I'd imagine, I've never tried).
  52. The biggest hurdle is to get access to the database containing all of the entity components.
  53. Luckily, Svelto.ECS will automatically provide that database to engines, the logic and behaviour implementors in Svelto.ECS.
  54. In Gamecraft, engines are usually registered during the "compose" stage, which occurs in loading screens or when switching gamemode.
  55. I think of the compose stage as when the composer taps his baton on the stand in front of him,
  56. right after the musicians have walked on stage and sat down (init stage) and right before the music starts playing (gameplay stage).
  57. Registering an engine to Svelto.ECS is made quite trivial by <a href="https://harmony.pardeike.net/">Harmony</a>,
  58. which can intercept a method call and its parameters to allow you to perform your own operations.
  59. Specifically, all of the compose methods in Gamecraft get passed a Svelto.ECS EngineRoots object,
  60. which is how Gamecraft (and now me) registers engines to operate on entity components.
  61. </p>
  62. <p>
  63. <span class="tab"></span>
  64. In the interest of not going sued for reverse-engineering Gamecraft or divulging company secrets I won't go into much more detail
  65. (although they're not very good company secrets if me,
  66. a person living on a different continent,
  67. working for a different company,
  68. figured out their secrets in my spare time).
  69. You can find out more details about Gamecraft by using a
  70. <a href="https://github.com/0xd4d/dnSpy#dnspy---latest-release---%EF%B8%8F-donate">C# decompiler</a> to view the original code,
  71. no reverse-engineering required.
  72. By the way, <a href="https://en.wikipedia.org/wiki/Reverse_engineering#Legality">reverse-engineering</a> is
  73. <a href="https://ghbintellect.com/is-reverse-engineering-legal/">usually legal</a> anyway.
  74. Of course, I'm not a lawyer, and I don't like closed-source software that much so I'm a bit biased.
  75. Interestingly, the European laws about
  76. <a href="https://en.wikipedia.org/wiki/Computer_Programs_Directive">software protection</a>
  77. seem to explicitly allow external mods and servers (you can tell I'm still a bit annoyed about RC14 getting shutdown).
  78. Regardless, FreeJam seems to be ok with the unofficial Gamecraft mods that have been created so far,
  79. so I wouldn't expect them to suddenly change their stance on mods in the near future.
  80. </p>
  81. <p>
  82. <span class="tab"></span>
  83. The <a href="https://git.exmods.org/modtainers/GamecraftModdingAPI#user-content-gamecraftmoddingapi">GamecraftModdingAPI</a>
  84. is probably the best example of modding with ECS.
  85. It provides a friendly object-oriented API for developers who want simplicity.
  86. For developers who want performance or understand the ECS paradigm, the modding API offers an easy way to register engines,
  87. so that Harmony isn't even required in the process of getting the entities database for modding entity components.
  88. As a major contributor to the GamecraftModdingAPI, I've seen how ECS has simplified modding.
  89. One of Svelto.ECS's primary focuses is code maintainability, which is very obvious whenever Gamecraft is updated.
  90. The average Gamecraft update breaks one or two parts of the API,
  91. and that's usually only caused by one or two variables changing name or location.
  92. I'd bet that if half of Gamecraft were rewritten,
  93. the biggest fix that the GamecraftModdingAPI would need would be changing the name of the components the API queries.
  94. The focus on maintainability doesn't just make Gamecraft easier to develop, it also makes mods easier.
  95. </p>
  96. <p>
  97. <span class="tab"></span>
  98. The data-oriented design of ECS helps as well.
  99. The object-oriented API in the GamecraftModdingAPI still queries entity components behind-the-scenes,
  100. so even OOP can take advantage of ECS design, albeit with some drawbacks.
  101. The database system in Svelto.ECS purposely allows for new entities to created but not made accessible immediately for some deterministic reason.
  102. In the event that a new entity is created as an object through the object-oriented API,
  103. the GamecraftModdingAPI has to force the database to sync new entities before any of the object's properties can be accessed.
  104. This database sync operation is a lot of unnecessary overhead which could be avoided with pure ECS.
  105. Pure ECS modding is easy,
  106. since almost every operation can be accomplished by operating on one or more entity components in the entity database.
  107. In Gamecraft's case, there's never more than one entity database on the go at once,
  108. which makes modding easy with a single point of contact.
  109. Once you've got the active entities database, you've basically completely pwned Gamecraft.
  110. </p>
  111. <p>
  112. <span class="tab"></span>
  113. A year ago, I thought ECS was a fancy niche concept which would be worthless for me to learn. It may still be worthless for my job, but its a vital part of Gamecraft and mods that I wouldn't pass up. I wonder when FreeJam will give us an official modding API for interacting with Svelto.ECS...
  114. </p>
  115. </div>
  116. <footer>
  117. exmods.org|<a href="https://git.exmods.org/ExMods/html-site">Site Repository</a>|<a href="https://exmods.org">Home</a>|<a href="https://discord.gg/2CtWzZT">Discord</a>|<a href="https://twitter.com/GamecraftMods">Twitter</a>
  118. </footer>
  119. </body>
  120. </html>