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.

My question continues on topic discussed here

I have tried implementing attribute/behavior design and here is a quick example demonstrating the issue.

class HealthAttribute : public ActorAttribute
{
public:
    HealthAttribute( float val ) : mValue( val ) { }

    float Get( void ) const { return mValue; }
    void Set( float val ) { mValue = val; }

private:
    float mValue;

};


class HealthBehavior : public ActorBehavior
{
public:
    HealthBehavior( shared_ptr< HealthAttribute > health )
        : pHealth( health )
    {
        // Set OnDamage() to listen for game logic event "DamageEvent"
    }

    void OnDamage( IEventDataPtr pEventData )
    {
        // Check DamageEvent target entity
        // ( compare my entity ID with event's target entity ID )

        // If not my entity, do nothing
        // Else, modify health attribute with received DamageEvent data
    }

protected:
    shared_ptr< HealthAttribute > pHealth;

};

My question - is it possible to get rid of this annoying check for game logic events? In the current implementation when some entity must receive damage, game logic just fires off event that contains damage value and the entity id which should receive that damage. And all HealthBehaviors are subscribed to the DamageEvent type, which leads to any entity possesing HealthBehavior call OnDamage() even if he is not the addressee.

share|improve this question
    
If you found the solution, please post it as an answer and accept it. –  J. C. Leitão Dec 4 '12 at 14:14

1 Answer 1

up vote 0 down vote accepted

Ok, I found the solution by myself.

It turned out that I missed the point where every entity should have own event manager. So, I modified the code:

HealthBehavior( shared_ptr< HealthAttribute > health )
    : pHealth( health )
{
    // Set OnDamage() to listen for entity local event "DamageEvent"
}

Now, behaviors dont care about game logic, they listen only to events triggered by their own event manager. And the game logic now fires event to entity's local event manager instead of global event manager:

// somewhere in game logic
{
    // simplified example of usage
    GetEntity( entityId )->GetEventManager()->TriggerEvent( damageEvent );
}

This way OnDamage() gets called only for target entity, so we can remove the check.

share|improve this answer

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.