Hello,

i got a problem with identifieng my classes upon its static ID number.
Upon creation of each class, it recieves its individual number, the numbers are ascending from 0 to "endless".
So now, I want to do a loop like this

for(int i = 0; i <= Class::GetMaxIDNumber(); ++i)
{
	nSpriteNr = spriteNumberOfClass(i);
}

int spriteNumberOfClass(int nClassID)
{
	//takes the class number and returns its corresponding private: m_nSpriteID number
	//how to do this part?
}

and I want to know the corresponding membervariable m_nSpriteID, but I do not know how to identify a class based on an ID number (or another way of looping through all classes of a given type)

thanks very much

Recommended Answers

All 7 Replies

I'm not sure I fully understand your question here. Could we see some code. Just pare down some example classes to the bare minimum required to illustrate what your question is asking and post them.

ok, i will try to further illustrate it. so i got this class:

#include "pixel_collision.h"

class Character
{
private:
	int m_nCID;
	int m_nSID;

public:
	static int m_nIDGen;

	//constructors
	Character(int nID, char *chFileName);
	Character(int nID, char *chFileName, char *chCurrentDir);

	//destructor
	~Character();

	int FindSpriteID(int nCID);
};

the constructor looks like this:

{
	[...]
	m_nCID = m_nIDGen++;
}

CID means ClassID.
SID means SpriteID.

before the beginning of int main() i set m_nIDGen to 0.

then I want to call a collision function that should look somewhat like this

int Character::CollidesWith()
{
int nReturn = -1;
for(int i = 0; i <= nTotalInstanceNumber; ++i)
{
if(PixelCollision(m_nSID, GetSpriteID(i)) //PixelCollision is a bool 
        nReturn = i;
}
return nReturn;
}

and in this example, int i would be the iterator iterating through the ClassIDs.
My problem is, that i do not know how to (start) coding GetSpriteID(int nClassID);

i hope someone could help me out
thanks for your attention

yours sincerely, replax

How are you storing the instances of your Character classes? An array? Vector?

So to get this straight, for each Character object you want to check it against all other character objects for collisions?

the instances should be able to be hardcoded as well as stored in an array. most probaly, the character class will be a base class to many derived classes, but i still got to think about that, really.

and i would be checking against all other character objects in certain occasions, for example in case of an "earthquake" taking place etc.

Still not making much sense here...

i am not exactly sure what part you do not understand, so i will try to explain most parts of it in more detail.

firstaful i got a function, called PixelCollision(int nSprite1, int nSprite2), that returns true if the two sprites are colliding. each sprite as an ID number to identify it. originally, sprites are not instances of classes, but i created a class to handle most of their functionallity (such as moving, shrinking, turning etc.). the class stores the sprites id number, too.

so now, i want to iterate through all sprites, to check wheather a given sprite is colliding with any of those. then the function will return the id number of the sprite it is colliding with, if it is not colliding, it will return -1.
to iterate through all instances of a single class with different names etc. i created a static int that is included in every instatnce of the class and is increased by one each time an instance of the class is created. that class then recieves the current value of the static int as class id.

now my problem is, how i identify the class based on its id number, so i can return/use the corresponding sprite id, while still being able to iterate through all class instances.

unfortunatly, i cannot give the sprites id numbers such as 1,2,3,4,5,6,7 so i can iterate through them.

i hope you may now understand my problem. if you have still any questions feel free to ask.

i appreciate your time,

yours,

replax

You can't iterate through all the sprites unless you maintain some form of collection of them. For example an array or vector, maybe a list. If you want to identify each of your sprites by an id number then this would mean you have to use a container which supports indexing.

eg.

vector<Character>  vcharacter;  // store all you created character objects in here

now you could refer to individual character objects using

vcharacter[i];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.