I am currently working on my own spin on a component entity system. I do not plan on looking at much "examples" or anything like that until I am done because I want to not just unconsciously copy other work but try and solve the problems myself. However I also want the final product to work WELL. I wouldn't post here if it wasn't a serious problem for me.
In school I learned how to do a simple singleton pattern. The problem I am having is solved by using this pattern, however I have heard and experienced about the problems in considered and improper use of a singleton can cause. Since I do want to make this program as large as I can without going into development hell, I would like to avoid this.
The problem:
In my component system a component is simply a data class that is stored in a templated class called the "ComponentManager". There is one for each type, as the type is specified when initialising the template.
A component may include 100% of the behaviour of a different component. For example a component representing a model in 3D-Space will also inculde the behaviour of a simple point in 3D space (because it has a location in 3D space).
To make it possible to manage all objects that have a certain property, for example all objects that can be placed in 3D space, I have decided that instead of just leaving this implementation to the data class id make it possible to store a direct link to another component on the same entity using a "component" class (this class can be used to retrieve a certain component reliably and easily from the ComponentManager).
All data classes therefore have the option to overwrite a "init" function. This function is necessary to be overwriteable, since I dont want to enforce a constructor on each data class that passes a Key for the entity as a parameter. However the initRelations function (thats what I called it) does have a default implementation which is simply empty.
The actual problem comes here: How can I easily and efficently get the ComponentManagers for the Components I might want to add? A singleton would be great but the ComponentManager classes directly store ALL the data in them and therefore use a lot of memory. They are also not simple classes and I kinda want to preserve the option for the ComponentManager class to recieve a baseclass and have a "script-ready" version which does manual runtime typechecks and a "fast" version which can be used for inbuilt types and is just the templated version (This however I can forget about if it makes things a lot more complicated and the solution without this is much cleaner. Maybe I can even do this with singletons but I just dont know about that yet?).
My question:
Are singletons truly the best way for my problem? Are there any better ways of going about this?
Aucun commentaire:
Enregistrer un commentaire