Jump to content

View more

Image of the Day

Inventory ! Va falloir trouver une autre couleur pour le cadre D: #AzTroScreenshot #screenshotsaturday https://t.co/PvxhGL7cOH
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

Design: Where would you program behavior for an object?

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 SonicD007   Members   


Posted 26 August 2012 - 08:11 PM

So I recently read Head First: Object Oriented Analysis and Design, and in one particular part of the book, it states that instead of having a base class with a bunch of derived classes for each object such as a soldier, tank, plane, etc. you should have one class called Unit that would get properties, set properties, get the type, set the type, and hold a container for you're different properties a.k.a. game objects.

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.

#2 Goran Milovanovic   Members   


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.


| Game Dev video tutorials  ->   http://www.youtube.com/goranmilovano |

#3 rnlf   Members   


Posted 27 August 2012 - 01:10 AM

There are as always lot's of possibilities. One approach I never took into account but with which I am very happy at the moment is to define all behaviour outside of the actual game objects. So the objects are reduced to nothing more but a collection of data, which is manipulated from external systems.

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++ ;-)

#4 NapoleonicMonkey   Members   


Posted 27 August 2012 - 03:21 AM

For my games so far, I've been happy with a single all-encompassing object with a variable "Type" or some such. This is mainly for stategy games, where each trooper would boil down to only a few stats and behavioural differences.

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.

#5 omercan   Members   


Posted 28 August 2012 - 06:15 AM

I use a data based design and do the behavior only with a scripting language like Lua.
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 Posted Image). 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();
/* Lua - Data Based*/
function blockClickHandler (position)
	 Point pt = position;

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!
Reading, Reading, Reading... why do you read not more?
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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.