4
\$\begingroup\$

How do I create new entities (the collection of their components) in an entity system? I have space ships in my game, for example, they need a lot of information set in their components when I create them. For example I set the mass, size and a moment of inertia tensor in a physicscomponent of the ship. I also have a component that contains a list of engines on the ship (is that right by the way? My ES doesn't support multiple components of the same type, should it?), each engine has a position and a force.

Now when creating a basic ship I see a couple of options. I can just hard-code it in my game. This is the way I have it at the moment, and it doesn't feel too good, there will be a lot of code to create different kind of space ships that are very similar except for some properties which would create lots of code duplication.

I could also use a database, I've seen this used before and I can definitely see how it'd be a good fit. Although it's a pretty small hobby project that I'm working on and it seems like it would be a little overkill. I'm not sure though, maybe it's worth it?

So the only thing remaining I can think of is describing entities in a text file of some sort, which might work. I guess it would require writing lots of code to parse different kinds of values, and where would I put the code that actually sets the values on the components? I'm using C# so I guess I could use reflection on the properties but I fear it'd be slow? And I don't want to clutter up my component code.

Maybe I should mention that it's a multiplayer game. So some components are client-only which could easily be marked in some way.

So how do you create new entities, or their components, in an entity system?

\$\endgroup\$
  • 2
    \$\begingroup\$ My answer here pretty much describes how I've done this: gamedev.stackexchange.com/questions/33453/… \$\endgroup\$ – MichaelHouse Sep 20 '13 at 22:12
  • \$\begingroup\$ Interesting solution, more powerful than XML would be, though right now I'm leaning towards XML. I don't know how I could forget about that. It's basically the middle-ground between text file and database that I was looking for. I also like the "baseAttributes tag" you have. \$\endgroup\$ – user31091 Sep 20 '13 at 23:59
  • \$\begingroup\$ XML can be powerful enough. It might be nice to be able to use a pre-made parser. I think you could do everything I do using XML. Good luck with it. \$\endgroup\$ – MichaelHouse Sep 21 '13 at 6:23
4
\$\begingroup\$

Create 2 factories : the ship factory, and the component factory.

All your ship's components and their configurations are storage in XML, then you feed your ship factory with the XML, it creates the ship first, and it call the component factory on each component described in the XML, to create the appropriate objects and bind them to the ship

By Example :

<ship>
 <Mass>100</Mass>
 <Size x="12" y="18" z="20"/>
 <thruster>
  <force>200</force>
  <warmupTreshold>500</warmupTreshold> 
 </thruster>
</ship>
\$\endgroup\$
  • \$\begingroup\$ Yes, I did something along those lines, with some minor differences. I parse the XML at startup and create a "blueprint" (just a mapping of each component property to a value) of how to make each component, then I feed the factories with that instead of XML. It's hopefully going to be faster than parsing XML if I create lots of entities at some point. Also, I guess it should be noted that the "ship factory" is not a specific ship factory, but more a "list of components factory"(=entity). Thanks for confirming I was on the right path! \$\endgroup\$ – user31091 Sep 25 '13 at 15:45
  • \$\begingroup\$ The way you load your XML depend on your ressource manager, so "blueprint" become useless. The factory is generic, that's the point, for more details, search about factory pattern by the gang of four. \$\endgroup\$ – mcamier Sep 25 '13 at 21:30
  • \$\begingroup\$ We obviously agree on the factory. However with the XML, everything is stored as strings. Take the mass on my ship for example. I'd have to do a float.Parse(...xml value...) every time I wanted to create a new ship entity if passed the XML directly to the factory. That's the problem I'm getting around with my "blueprints", parse it once and pass in objects of the correct type. Otherwise there would be a lot of parsing different kind of strings every time I created a new entity. Please enlighten me if I'm doing something wrong or if there's a better way to handle XML as I'd love to learn! \$\endgroup\$ – user31091 Sep 26 '13 at 17:41
  • \$\begingroup\$ Like byte56 said above, juste use a pre-made parser (like TinyXML in C++) in your factory, the xml itself can be handled by an asset/cache manager. Using TinyXML on each shit creation is not heavy, but more easy to use, understand ans maintainable. See that book for game's architecture's best practices : Game Coding Complete Fourth Edition (ISBN-10: 1133776574) \$\endgroup\$ – mcamier Sep 26 '13 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.