Jump to content
  • Advertisement
Sign in to follow this  
frogtag

SOLVED: Multiple Class Instances

This topic is 3777 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

Just a theory question. Using C++/OpenGL/VC++2008. If I wanted to make a lot of characters for a game, using a character class. How do I do it without calling every characters instance. For example... character NPC1; character NPC2; character NPC3; character NPC4; ... [Edited by - frogtag on June 21, 2008 3:27:28 AM]

Share this post


Link to post
Share on other sites
Advertisement
same way as you do with any data type (int, char etc.) - an array or a list.


cMyClass players[50];

for(int i = 0; i < 50; i++)
{
players.DoThings();
}


for lists, you can either use your own implementation of linked list, or use the STL implementations..


std::list<cMyClass> players;

cMyClass dude;
players.push_back(dude);

for(std::list<cMyClass>::iterator it = players.begin();
it != players.end() ; ++it)
{
it->DoThings();
}


Share this post


Link to post
Share on other sites
Use a container. An example would be std::vector.

For example, to create a vector of 10 npcs, numbered 0-9, you would write:


std::vector<character> npcs(10);
npcs[0]; // First NPC
npcs[9]; // Last NPC


Of course, you can add or remove elements from a vector after it has been created.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!