Sign in to follow this  
Zneak

Entity management in RTS game

Recommended Posts

Hello guys, So during this semester we're going to make a real-time strategy game. Somewhat Starcraft-like in terms of mechanics. Right now we're planning our game components, and things are *mostly* going right up to now (some problems and clashes expected, but we should make it). However there's one thing that's really bugging us: entity management. I mean, obviously, the game's gonna be made up of several different entities, but there's no inheritance strategy we can apply without ending up with some kind of blob superclass (since some buildings can attack, we'd have to move the Attack method to the common ancestor of Unit and Building, but since most buildings can't attack, that's pretty dumb). One suggested solution was to use a components-based system, so instead of having a superclass with the Attack method, it'd be a "Fighter" component providing everything required to attack, and then we wouldn't need to clog objects with useless code. This all sounds good, except maybe for composing attributes like armor and damage. In my mind components need to be somewhat independent, but there are problems to it. Like, somewhere in the tech tree, it's very possible that a research will give, say, a +1 bonus to armor for all units; or maybe a special status could decrease a unit's efficiency. The system we thought of would imply that when you asked a component for a value, it would check other components susceptible of providing modifiers to it, and then return a final value; that's not a very independent design. Another way would have been to query entities directly for a specific information, and then the entity itself would query every single component it has to see if it gives bonuses and return the sum, but that would be mostly not very elegant. So I wondered if any of you had an idea about how to build up entities without reusing too much code or having a blob class, and having the possibility of composing attribute values in a more elegant way.

Share this post


Link to post
Share on other sites
You could implement a special "attribute" class. So an entity consists of a list of components and a list of attributes. The attribute class itself would then contain a list of modifiers which affects the value of that attribute. Basically, to get the value of the "defense" attribute, you call entity->get_attribute("defense")->get_value(). The get_value method then looks at the base value, and applies all of the modifiers to return the "final" value.

Say you have a unit whose special power is to increase the "attack" attribute on all nearby friendlies. You can just have that unit add a modifier to all of the "attack" attributes on the entities nearby.

Then you can implement all kinds of fancy attribute modifiers. You can have ones that only last a certain amount of time, you can have ones that affect a unit only when it's a certain distance from another unit (and it could even scale it's effect so that it works better the closer you are, etc), and so on.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this