Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm trying to design my game to be component based rather than overly hierarchical. Essentially, every high level object in the game (like gun, or whatever) inherits and implements the interfaces that it needs. For example, IPhysics, IRenderable, IWeapon, etc.

There are a few cases where I'm uncertain how to obtain a specific interface from another.

For example. A Gun might inherit from IWeapon, IPhysics, and IRenderable. I then also have a player which inherits from IPlayer, IPhysics, and IRenderable. I need a way so that, when Player and Gun collide, if Player is not currently wielding a weapon, he should be able to wield the gun and hold a pointer to the IWeapon of the Gun.

Right now, when Box2D detects a collision between 2 fixtures, I use the UserData field to store the IPhysics pointer associated with the fixture, and therefore, the IPhysics.beginContact method is invoked.

From here, the Player needs to somehow be able to know that this is a weapon, and obtain an IWeapon* . This is the part I really have no idea on.

Any advice on solving something like this is appreciated, even if it means some kind of design change on my part. I'm just looking for a clear way on how to solve this sort of problem.

Thanks

share|improve this question
    
It depends of your message system organization. –  momboco Jul 18 '11 at 23:04
    
@ momboco could you elaborate with some examples? –  Milo Jul 18 '11 at 23:06
    
A lot of physic engines has a void* to user data or a function pointer to attach a callback. You can attach your entity to these elements and avoid the need to communicate between components. –  momboco Jul 18 '11 at 23:18
add comment

2 Answers

up vote 5 down vote accepted

Updated to the new specifics of the question

For example. A Gun might inherit from IWeapon, IPhysics, and IRenderable.

This is not a component system. This is not to say its not a bad system, but in this case you should be able to cast the pointer to any of the interfaces (or just cast it to a Gun object and it should Contain the interfaces) and then use it as appropriate.

As for determining what kind of object it is that the player has collided with, you can attempt to use the internal RTTI abilities like dynamic_cast<>() or potentially implement your own RTTI (Run Time Type Information) system and add in an IType object to query if an object is of a specific type or to return the type that it is, etc. Once you validate the type you can safely cast it as desired.

If you do have a component system and we are just not meshing on nomenclature, then just leave me a comment and explain how the components are related to each other. Often this is done by an ID of the object they are a part of.

Hope this Helps

share|improve this answer
    
Well essentially the first part of your answer is what I'm struggling with. I need a way for a player to collide with a weapon, and somehow come out of it with an Item* or something where it could then call doAction() or fire(). –  Milo Jul 18 '11 at 23:16
    
@Milo Then I think instead of a messaging system directly I think you are looking more indirectly of how to go from an IPhysic's to an IWeapon component. As mentioned there should be something tieing the components together and I would suggest something like an IPickupable that you can have the IPhysics call on contact for items that are pickupable to handle just this logic. –  James Jul 19 '11 at 1:30
    
do you have an example of how this IPickupable system would work? Would this mean I would need to provide an IPickupable* to the IPhysics ? Thanks –  Milo Jul 19 '11 at 1:50
    
Thanks, I'll go with dynamic casting and testing for NULL then. –  Milo Jul 19 '11 at 3:49
add comment

If all your objects implement interfaces (Rather than being a set of components), all you have to do to get the IWeapon from the IPhysics is cast it. In C++, a dynamic cast will return null if the object doesn't inherit from IWeapon. In C#, 'as' will return null if the object doesn't implement IWeapon. Your mileage will vary in other languages.

But I wouldn't call this method of creating game objects a component-based system.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.