How to properly use IAnimationEndCallBack

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
spopo
Posts: 33
Joined: Wed Mar 28, 2007 1:46 pm

How to properly use IAnimationEndCallBack

Post by spopo »

Hello again, i came with new problems once more. Right now it's with class IAnimationEndCallBack usage. So, let's start :
i have class Enemy, which has private (it could be public) members that shows which animation is currently being played, and which not.

Code: Select all


struct anim {
	bool run;
	bool attack;
};

class Enemy {
	                ..............
	anim EnemyAnim;
	                .............
};
And what i do not know, is how to implement the IAnimationEndCallBack class into the Enemy class, so that every Enemy class would have it's own IAnimationEndCallBack class, which would change the Enemy class members, for example : run animation has ended - EnemyAnim.run = false;

And one more thing : in the main.cpp i'd only have to create an instance of Enemy class. Any help will be appreciated ! :wink:
Last edited by spopo on Sun Apr 22, 2007 3:47 pm, edited 2 times in total.
spopo
Posts: 33
Joined: Wed Mar 28, 2007 1:46 pm

Post by spopo »

Anyone there? Or are there any ways to avoid using this class? Are there any other ways to see what animation has ended?
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

Mind showing how you solved? I've been curious of this myself.
spopo
Posts: 33
Joined: Wed Mar 28, 2007 1:46 pm

Post by spopo »

Sure.

Enemy.h

Code: Select all

struct anim {
        bool run;
        bool attack;
};
 
class Enemy;
 
class AnimCallback : public irr::scene::IAnimationEndCallBack
{
public:
    void OnAnimationEnd(irr::scene::IAnimatedMeshSceneNode* node);
 
    void setEnemy (Enemy *enemy);
    
private:
    Enemy *m_Enemy;
};
 
class Enemy {
        irr::scene::IAnimatedMeshSceneNode *Node;
        anim EnemyAnim;
        AnimCallback  Callback;
public:
         anim* getAnim(void);    	
         void attack (void);
};
Enemy.cpp

Code: Select all


void Enemy::attack (void)
{
	EnemyAnim.attack = true;
	Node->setMD2Animation(irr::scene::EMAT_ATTACK);
	Node->setAnimationEndCallback(&Callback);
}	


anim* Enemy::getAnim(void)
 {
       return &EnemyAnim;
 }


void AnimCallback::OnAnimationEnd(irr::scene::IAnimatedMeshSceneNode* node)
{
       if (m_Enemy->getAnim()->run) m_Enemy->getAnim()->run = false;	   
       if (m_Enemy->getAnim()->attack) m_Enemy->getAnim()->attack = false;
}

void AnimCallback::setEnemy (Enemy *enemy)
 {
        m_Enemy = enemy;
 }

// And in the Enemy constructor 

Callback.setEnemy (this);

vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

It seems that you could simplify things just a tad by refactoring and making the enemy respond to its on animation end callback...

Code: Select all

class Enemy : private irr::scene::IAnimationEndCallBack
{ 
private:
  irr::scene::IAnimatedMeshSceneNode *Node; 
private:
  virtual void OnAnimationEnd(irr::scene::IAnimatedMeshSceneNode* node); 

public: 
  void attack (void); 
}; 
 
void Enemy::attack (void) 
{ 
  //... snip ...

  Node->setMD2Animation(irr::scene::EMAT_ATTACK); 
  Node->setAnimationEndCallback(this); 
}    

void Enemy::OnAnimationEnd(irr::scene::IAnimatedMeshSceneNode* node) 
{
  //... snip ...
} 
spopo
Posts: 33
Joined: Wed Mar 28, 2007 1:46 pm

Post by spopo »

To vitek: Node->setAnimationEndCallback(this);

gives

error C2664: 'irr::scene::IAnimatedMeshSceneNode::setAnimationEndCallback' : cannot convert parameter 1 from 'Enemy *const ' to 'irr::scene::IAnimationEndCallBack *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cas
monkeycracks
Posts: 1029
Joined: Thu Apr 06, 2006 12:45 am
Location: Tennesee, USA
Contact:

Post by monkeycracks »

Ahh thanks. I've always wondered how to use this.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

@spopo: Maybe you did not drive it correctly, or you made the attack method const (which would make this const also)?
spopo
Posts: 33
Joined: Wed Mar 28, 2007 1:46 pm

Post by spopo »

Nevermind :oops:
doqkhanh
Posts: 158
Joined: Sat Mar 01, 2008 3:14 am
Location: Tokyo, Japan
Contact:

Post by doqkhanh »

Post Reply