Sign in to follow this  

SOLVED: Multiple Class Instances

This topic is 3465 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
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[i].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

This topic is 3465 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this