{"id":1648,"date":"2011-08-22T00:11:45","date_gmt":"2011-08-21T23:11:45","guid":{"rendered":"http:\/\/t-machine.org\/?p=1648"},"modified":"2015-12-31T18:35:10","modified_gmt":"2015-12-31T17:35:10","slug":"entity-system-rdbms-beta-a-new-example-with-source","status":"publish","type":"post","link":"http:\/\/new.t-machine.org\/index.php\/2011\/08\/22\/entity-system-rdbms-beta-a-new-example-with-source\/","title":{"rendered":"Entity System: RDBMS-Beta (a new example with source)"},"content":{"rendered":"<p>I&#8217;ve just added <a href=\"http:\/\/entity-systems.wikidot.com\/rdbms-beta\">a new, improved Entity System design<\/a> to the wiki. I&#8217;ve also <a href=\"https:\/\/github.com\/adamgit\/Entity-System-RDBMS-Beta--Java-\">created a github project for it<\/a> where I&#8217;m about to check-in working source.<\/p>\n<p>The only source I&#8217;ve provided is Java &#8211; but it&#8217;s an evolution upon <a href=\"http:\/\/entity-systems.wikidot.com\/rdbms-with-code-in-systems\">the old &#8220;RDBMS with Code in Systems&#8221; type<\/a>, that already has full source for Java and Objective-C &#8211; so you could easily upgrade the obj-c version to this new type (hint, hint).<\/p>\n<h3>What&#8217;s new?<\/h3>\n<p>Three major changes:<\/p>\n<ol>\n<li>Included a bunch of sensible java-specific improvements, based on the forks other people had done of the previous Java implementation\n<li>Added a bunch of FANTASTICALLY USEFUL methods to the EntityManager &#8211; things you definitely need when writing a game\n<li>Added a new concept: the Meta Entity\n<\/ol>\n<p>The first one is obvious, the second is obvious when you look at what&#8217;s added (e.g. a &#8220;removeComponent&#8221; method; why wasn&#8217;t it there originally? Solely because it wasn&#8217;t necessary to show &#8220;the simplest possible implementation&#8221;).<\/p>\n<p>The interesting one is number three: the Meta Entity<\/p>\n<h3>What&#8217;s a MetaEntity?<\/h3>\n<p>A while back, in the <a href=\"http:\/\/t-machine.org\/index.php\/2010\/05\/09\/entity-system-1-javaandroid\/\">first Java\/Android ES I wrote about<\/a>, I mentioned that I was using a fake Entity class.<\/p>\n<p>I made a big fuss about how this was NOT being used to store any game data &#8211; but was instead an OOP trick to make it easier to write code.<\/p>\n<p>Thinking about this later, I realised that there was really no need for that class to be named &#8220;Entity&#8221; &#8211; and calling it that ran the risk of tempting people into using OOP for their core data \/ components (Remember: Never do that!). Instead, looking back at a couple of example ES&#8217;s I&#8217;d written, I saw that every method &#8211; and all data &#8211; in this class was a &#8220;meta method&#8221;.<\/p>\n<p>For the methods, you need to read the source AND the javadocs to see this.<\/p>\n<p>For the data, it&#8217;s a lot more obvious: the only data that a MetaEntity has is:<\/p>\n<ul>\n<li>The Entity itself (remember: that&#8217;s just an integer &#8211; or a UUID object in Java, which is a cleaner Java-specific way of handling it)\n<li>The EntityManager object which created this Entity, and which contains the Components for that Entity, and &#8230; basically provides all access to the data etc\n<\/ul>\n<p>i.e. if you pass around MetaEntity objects, you can feel safe that you know where they came from, where their components are, etc.<\/p>\n<p>Because when you pass around raw Entity&#8217;s, they&#8217;re just an integer &#8211; which makes it easy to create an Entity in one EntityManager, then accidentally start using it in another EntityManager. Technically that&#8217;s illegal &#8211; but from a compiler perspective, it&#8217;s 100% legal &#8230; so you&#8217;ll get bizarre runtime bugs. Ouch.<\/p>\n<p>Equally, the MetaEntity can contain a lot of single-argument and zero-argument versions of methods that exist in the EntityManager as 2-argument, or 1-argument methods. This greatly reduces typing, increases readability, and reduces opportunities for bugs. It may sound like a very small change (2 arguments dropping to 1), but I promise you: in practice, it makes a big difference.<\/p>\n<h3>Why not use MetaEntity&#8217;s all the time?<\/h3>\n<p>They&#8217;re very inefficient in how they use memory, and they throw away many (but not all) of the performance advantages of an ES.<\/p>\n<p>For instance, because you&#8217;re moving from &#8220;integer + array of structs&#8221; to &#8220;linked-list of objects&#8221;, you&#8217;re making your game go from &#8220;cache friendly&#8221; to &#8220;cache poor&#8221;, and making your MINIMUM mem usage go from &#8220;tiny&#8221; to &#8220;small&#8221;.<\/p>\n<p>In practice &#8230; if those differences matter to you, you&#8217;re probably writing a hardcore custom ES anyway.<\/p>\n<p>More importantly: even in a &#8220;hardcore&#8221; ES, you don&#8217;t actually *need* that performance all the time. If you&#8217;re just pulling out a handful of Entities and their Components &#8211; e.g. a player Entity (of which there&#8217;s only 1 &#8211; or a few for multiplayer) &#8211; then the above performance differences are NON EXISTENT (vanishingly small).<\/p>\n<p>&#8230;but the code is more readable, easier to maintain, and more robust.<\/p>\n<p>So. I recommend using the MetaEntity for *all* your ES coding &#8230; until you reach a point where performance is low. Then look at re-coding *only* the affected loops \/ methods (remember: you can do this on a System-by-System basis; your code is nicely separated already!). That way, you get the best of both worlds.<\/p>\n<blockquote><p>\nHowever &#8230; the reason none of this was included in the first Java ES I posted onto the wiki &#8211; the ultra-simple &#8220;RDBMS-inspired with Code in Systems&#8221; &#8211; is that really this is all just gloss on top of an ES. You don&#8217;t need it. I believe it makes your ES easier to work with &#8211; but it distracts from the core theory.<\/p>\n<p>I&#8217;d recommend you start with the simpler ES, and understand it, before moving onto something like this for your practical work.\n<\/p><\/blockquote>\n<p>For more info, check out the wikidot link at the top of this post &#8211; and try the github project linked from it (currently empty, until I do a check-in)<\/p>\n<h2>Did this post help you?<\/h2>\n<p><a href=\"http:\/\/t-machine.org\/index.php\/2015\/12\/12\/support-me-on-patreon-for-early-access-to-new-entity-systems-articles\/\">Support me on Patreon, writing about Entity Systems and sharing tech demos and code examples<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve just added a new, improved Entity System design to the wiki. I&#8217;ve also created a github project for it where I&#8217;m about to check-in working source. The only source I&#8217;ve provided is Java &#8211; but it&#8217;s an evolution upon the old &#8220;RDBMS with Code in Systems&#8221; type, that already has full source for Java [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60],"tags":[],"_links":{"self":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/1648"}],"collection":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/comments?post=1648"}],"version-history":[{"count":3,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/1648\/revisions"}],"predecessor-version":[{"id":3769,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/posts\/1648\/revisions\/3769"}],"wp:attachment":[{"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/media?parent=1648"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/categories?post=1648"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/new.t-machine.org\/index.php\/wp-json\/wp\/v2\/tags?post=1648"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}