#### Archived

This topic is now archived and is closed to further replies.

# what a stupid question !!!

This topic is 5946 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i''m the newbie among the newbies and i have very basic question about game programming. Suppose you have a class CAlien with a Render() function. You also have a game loop and within the game loop you call the Render() function of all the CAlien objects. Ok, now Suppose one of the alien got hit by a missile. I want the alien to act differently as soon as he got hit by the missile. How can I do that ? Does the Render() function have to be changed to do something else ? Can somebody explain to me how to do it thanks. Spill "not the great" some.

##### Share on other sites
Try having different modes for the alien, ie Alive, Dead, Hurt etc, (hint use enum in C++) and set the mode when it changes. Then when you call render() check what mode the alien is in and act accordingly.

http://kickme.to/BallisticPrograms

##### Share on other sites
thanks
somebody told me that i should use function pointer.
Do you think it is a good idea ?

##### Share on other sites
The reason you''d use a function pointer is to avoid having to test for which state the entity is in inside of your Render function.

For example, let''s say that you have your render function implemented something like this to handle your entities different visual states:
  void CAlien::Render(){ switch(state) { case SHOOTING: // draw stuff break; case ATTACKING: // draw stuff break; case DYING: // draw stuff break; }}

For every frame, the Render function will have to test the value of state, possibly trodding to lots of different possiblities. This takes time, of course.

Instead, you could have a different function for each visual state. For example, CAlien::RenderShooting, CAlien::RenderAttacking, and CAlien::Dying. Then you use a function pointer that points to the appropriate drawing functions. So, when you set the entity''s state, just set that pointer to the address of the drawing function you need. Then, instead of calling a drawing function directly, just call it through the function pointer. This eliminates the need for all the tests, but is a little tricker.