• Advertisement

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites

  • Advertisement