Jump to content
Site Stability Read more... ×
  • Advertisement
Sign in to follow this  

Prefabs - an alternative to factories?

This topic is 2871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been playing with component based systems, and comparing them to my older code which relied on inheritance. I've found a kind of hybrid between the two. The more recent component based pattern relies on a collection of components, while composition with traditional OOP relies on members of the class.

My system works like this. This is a constructor:

public Emplacement(TankGameWorld level, ArticulatedModel sourceModel, string faction)
: base(level)
// a member of the entity itself. Not possible with pure component based.
this.ModelInstance = new ArticulatedModelInstance(sourceModel);

// ... some boilerplate removed
this.Components.AddComponent( this.HealthPool = new HealthPool(this, 100, 250));
this.Components.AddComponent( this.FactionMember = new FactionMember(this, faction));
this.Components.AddComponent( this.BlowsUp = new BlowsUp(this));
this.Components.AddComponent( this.Turret = new Turret(this, this.ModelInstance, "turret", "barrel"));
this.Components.AddComponent( this.BodyShape );
this.Components.AddComponent( new Renderable3DComponent(this, this.ModelInstance));

I've implemented a typical component based messaging system. The base class of entity has a map of message type to callbacks, and components subscribe to some type of callback within this list.

Basically, rather than the components within the class being drawn from a data file, the class builds its own components.

This brings about two main advantages.

1. I can have a traditional inheritance tree of entity types, if I wish. With a pure component based system, I've had difficulty in implementing polymorphism with entities, because they are all technically of the same type. In a statically typed language, I can derive entities from a base class of entity in the same way I would in a pure inheritance based system.

2. It is easier to prototype new behaviour as behaviours of the entity itself, and then later on, move it into a component. This is typically what a component based system looks like when one is refactoring a pure inheritance based model into a component based model, but I've begun to implement new behaviour by overloading the methods of the base entity. An entity can subscribe to its own message system, or intervene at various parts of its update cycle.

Since this is a hybrid system, I've not really looked at the disadvantages compared to a pure component based model. What I'm curious about is, has anybody else used this kind of system, and what were the pitfalls, particularly with maintainence of a codebase? OR is this old stuff, and a naive implementation of something that already exists?

Share this post

Link to post
Share on other sites
You can accomplish the precise same concept in a pure component based model approach where you create an entity based on a predetermined set of components with static values. For example, I know that every Orc archetype that I will create in my game must have a specific finite set of characteristics. I can create an entity template that I assign the name of Orc and then whenever I want to create a prefabricated Orc entity, I can just call my entity manager and provide the prefab's name and it does the rest.

The concept is that rather than hard coding the entity hierarchy in code, this is done by creating a binary prefab file that contains all the values needed by the entity manager so that templates can be read at start up and then anytime I need to create a particular entity of a specific archetype, I can get one created for me as though I am using a factory, but the definition is offline and outside of code. This works great for the design team and the programming team because the designers can do what they're good at and design a game while the programmer can continue to improve code without having to be burdened with adding game content related stuff beyond new toys for the designers.

Obviously you can go either way that suits you; however there is far more benefit once your code base grows to having this type of stuff offline in data-driven files (whether binary or XML or some other format) than in the code itself because you can make changes and restart the game with ease and much faster than recompiling the engine and game code.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!