Well this is my remake of my last topic, which is buried under 100s of different topics now in the General C++ Programming fourm.
Well here it is:
I made a SDL Mob Class, and I want to know how I can make my mobs function properly. I don't know how to make my mobs do stuff. (Eat, Drink, Die, Move.. Etc) So I want to know how to make my mobs do stuff.
Oh, I fixed that in the updated version of the code, but would I have to make a loop for every mob type in the game? Give me General things I'd have to do. How would I make my program know what mob to make move? I have a feeling I know how to do it, but I'm not sure.
Last edited on Dec 15, 2012 at 2:39pm by Fredbill30
Write a function that checks the states of the mob (usually it's called Update() ) and calls other functions depending on the states.
Say that your mob moves always horizontally. When it hits a wall a hypotetical collision checking function changes its states from movingRight to movingLeft.
Update() checks if movingRight is true - it's not, so it skips that part of the code. Now it checks if movingLeft is true - yes, so it changes its position: xPos -= speed.
Note that there are other ways to achieve the same result.
Update() must be called for every existing mob. The main game loop is a good place to do this.
Yes like that. You can also use a vector in which you place all the mobs and iterate through it calling each element's MobUpdate(), so you don't have to modify Update() each time you want to spawn more mobs.
I don't understand what's your concern about polymorphism though.
What if they're part of different classes? I'd have to call this for EVERY type of mob?
Or could I do this:
1 2 3 4 5
void KydenEngine::KydenEngineUpdate()
{
Update(Mob); //This finds any Mob that needs to be updated
Update(Vehicle); //Finds any Vehicle that needs to be updated...
}