• Finding classes
    7 replies, posted
I have a problem, I have a world object a base object and 2 other classes (zombies,Humans) Each of the 2 classes is derived from the base class. This is for creating the classes : [cpp] void World::CreateZombie(float x, float y) { NumZombies++; Zombie *zombie = new Zombie(x,y); Entities.push_back(zombie); } void World::CreateHuman(float x, float y) { NumHumans++; Human *human = new Human(x,y); Entities.push_back(human); } [/cpp] Now my humans have to find the zombies (or other way around). How can I get all the zombies on a human class so I can get their x,y coordinates ? Thanks in advance.
Add a virtual bool IsHuman-function. Return false by default, let Human override it to return true. You should be able to figure out the rest :)
Wow, never thought of it that simple :D, Another thought of me was to make a array with the possition of each zombie/human. Is this also oke ? (just want to know)
[code]Human *human = new Human(this, x, y); [/code] Store a pointer to World inside of the entities so that they can access the other entities. And now you can do [code] Human* Zombie::GetClosestHuman() { Human* Closest = NULL; for(int i = 0; i < world->Ents.length(); i++) { if (world->Ents[0]->GetType() == HUMAN) { //Calculate shit here } } return Closest; }[/code]
[QUOTE=quincy18;18713870]Wow, never thought of it that simple :D, Another thought of me was to make a array with the possition of each zombie/human. Is this also oke ? (just want to know)[/QUOTE] How would that help ô.o [QUOTE=high;18713944][code]Human *human = new Human(this, x, y); [/code] Store a pointer to World inside of the entities so that they can access the other entities.[/QUOTE] There might be a singleton or a global variable.. (s)he didn't state that of being the problem. Also, if you need this, you can use a reference, since it should never be NULL.
Human* Zombie::GetClosestHuman() Why the *human in front of it ? And nvm the other question.
To return the pointer to the human? That way you can get more than just the X/Y.
Dude, I feel so stupid, not recognizing it was the data type :hitshead:
Sorry, you need to Log In to post a reply to this thread.