Design: Where would you program behavior for an object?
Members - Reputation: 460
Posted 26 August 2012 - 08:11 PM
Using that kind of approach, you would need one class only for an infinite number of gameobjects whereas if you derived from a base class you would need one class for each object. My question is, where exactly would you program the behavior for each "property"? Say I get a soldier and his properties from my Unit class, where and what would be the best way to implement it's behavior in game? What class would you place it in, how would you do it? The book doesn't explain how we'd program a gameobject's behavior.
Hope this made sense to you guys. Thanks.
Members - Reputation: 1104
Posted 27 August 2012 - 12:32 AM
Using that kind of approach, you would need one class only for an infinite number of gameobjects whereas if you derived from a base class you would need one class for each object.
You would need one class for each object type, yes, and there is nothing too unwieldy about that. There are better approaches for certain situations, but generally, inheriting from GameObject (or some other such base class) is a perfectly valid approach.
Beyond that, you can use a kind of "component" set up, where you have an Entity base class, which is essentially a container (and a communications layer) for a list of components (state machines, basically) that manipulate the Entity in some specific manner.
That approach has worked fairly well for me in the past; I managed to avoid a bloated base class, and I didn't have to worry about managing a convoluted inheritance hierarchy.
I would be very interested to hear what others have to say on this topic.
Members - Reputation: 1167
Posted 27 August 2012 - 01:10 AM
Simplified I have this: I would have a function or whatever that takes a list of gameobjects of a specific type (for example "Soldier") and manipulates it's internal state according to typical soldier-behaviour. For tanks, there would be another function, for aeroplanes yet another and so on.
As I said, that's the simplified version which should work fine for small projects with limited requirements to flexibility. In reality, a component entity system like the one Goran describes is more extensible and easier to maintain (but takes a bit more work to get up and running in the first place). So what I actually do in my current project is to have a class "EntitySystem" which allows to create entities which are nothing more than identifiers (represented as integers) for game objects. Once I obtain a valid identifier, I can add components to them using another function of the EntitySystem.
Neither the entity nor the components contain any logic but are rather stupid data stores which are manipulated from the outside.
Then I have a bunch of "Systems" which can request the EntitySystem to call a function of their choice for every entity that has a specific combination of components. For example an Entity which qualifies as a soldier may have one component called "WithSoldierBehaviour" and one component "WithPosition". The system responsible for adding behaviour to soldiers (let's call it "SoldierBehaviourSystem") would then ask the EntitySystem to call a function like "SoldierBehaviourSystem::update_soldier(Entity e, WithSoldierBehaviour& behaviour_component, WithPosition& position_component)" for every entity which has both: a WithSoldierBehaviour- and a WithPosition-component.
In reality, soldier behaviour would need a lot more of information, thus needing more components to be added to the entity, like WithHealth, Waypointable, SubmachineGun, and so forth. The SoldierBehaviourSystem would have to take all of these into account.
Using this system also means that there will not be a single way to represent a soldier. This is a great way to add flexibility to your game. Want a Jetpack-Soldier? Add a JetPack-component. Want a vampire soldier? Add a BloodThirsty-component. I think you get the idea ;-)
I hope C++ syntax is okay for you, you didn't mention what language you are using, so my examples are in C++ ;-)
my blog (German)
Members - Reputation: 147
Posted 27 August 2012 - 03:21 AM
In the initialisation methods, I check the type and fill the statistics for that unit. In the AI I check type and switch to determine behaviour. Some variables will exist to define behaviour (eg ability to fly or go on water).
I find it easier to conceptualise this way, and if most of the behaviour between units is similar, with only differences in stats / minor ability differences (move through water / fly), it is simpler than defining a new unit class every time.
You get a slightly bloated class, but most of it is pretty simple to understand - just a few switch statements.
However it might not work for a more complicated object - like a soldier in a FPS. His behaviour would be variable - defined by his equipment (jetpacks, grenade launchers would be different to a normal rifle-trooper), status (elites, grunts, bosses), and would be extremely different to a tank or helicopter (for example). For that I imagine rnlf's approach would be better - with separate components strung together.
Members - Reputation: 370
Posted 28 August 2012 - 06:15 AM
This is cache friendly and extensible but it is much more complex and not really easy.
Every entity has his properties and you can access them via C++/Java/C#... with a hash map or a normal map (other containers are welcome ). The scripting interface can be object oriented, so that you will get an object pointer, where you can access the members and change the properties in an object oriented manner, or also be data based.
/* Stupid Example */ /* C++ */ Point pt = dataManager->property("block1", "pos").toPoint(); /* Lua - Object Oriented*/ function blockClickHandler (block) Point pt = block.position(); end /* Lua - Data Based*/ function blockClickHandler (position) Point pt = position; end
This above is really a simple and incomplete example but shows some ways.
Many big games (with scripting languages), applications and websites use data based design. I really suggest it!
I have a blog: omercan1993.wordpress.com look there for more content
And I also do some art: omercan1993.deviantart.com
And whats about the Pear3DEngine? Never heard of it? Go and look!
Yeah, and currently I do this: SimuWorld
PS: Please look at this poll on my blog about scripting languages: A opinion poll about scripting language