AggrorJorn Posted June 11, 2013 Share Posted June 11, 2013 This is a nice one. I am iterating through a list of gameObjects. I want to know when one of these GameObjects is a Player class (player is a subclass of GameObjects). But the iteration iterates through gameobjects and not players. //GameObject list iteration for (iter; iter != gameObjects.end(); ++iter) { //If the gameobject has sub classtype player.... if(typeid((*iter)) == typeid(Player)) { Is there a nice way to handle this? Quote Link to comment Share on other sites More sharing options...
Rick Posted June 11, 2013 Share Posted June 11, 2013 This is generally why I always use entity->SetUserData(this) when inside a class. Then I make a base class like GameObject, which has a pure virtual ToString() and my sub classes override it and return a string that describes that class. class Player { public: virtual string ToString() { return "player"; } }; Then in your loop you cast the object to GameObject* and if not null call ToString() and compare the value to what you are looking for. OR You set a key for each entity when loaded and get the key in your loop to compare. Quote Link to comment Share on other sites More sharing options...
Furbolg Posted June 11, 2013 Share Posted June 11, 2013 It should be possible to detect "isSubclassOf" (C# like) by create a variable from the type you want to check, then assignt the iterator value (c++ cast) and check if your variable != null. for (iter =gameObjects.begin(); iter != gameObjects.end(); iter++) { Player* playercast = dynamic_cast<Player*>( *iter ); if (playercast != null) { // its a player } } Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted June 11, 2013 Author Share Posted June 11, 2013 Thanks Rick. I thought that there might be some C+ functionality that lets you ask about subclasses or something. But your virtual ToString idea is also very nice. Quote Link to comment Share on other sites More sharing options...
Furbolg Posted June 11, 2013 Share Posted June 11, 2013 Ok, you can use Ricks solution, just to have a complete picture i post mine. Its an attachment, main.cpp clean c++ console application. main.cpp 1 Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted June 11, 2013 Author Share Posted June 11, 2013 Sorry I hadn't seen your response Furbolg. Won't that create a memory leak? Player* playercast = dynamic_cast<Player*>( *iter ); if (playercast != NULL) std::cout << "PLAYER" << std::endl; Quote Link to comment Share on other sites More sharing options...
Furbolg Posted June 11, 2013 Share Posted June 11, 2013 No because its just a pointer // Edit: This would be memory leak: Player* playercast; for (int i = 0; i < 1024; i++) { playercast = new Player(); } Quote Link to comment Share on other sites More sharing options...
AggrorJorn Posted June 12, 2013 Author Share Posted June 12, 2013 Thanks furbolg. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.