Okay, I'll try to explain the issue I've ran into:
Let's say I have two classes named NPC and MOVER. Both of those classes are derived from a class named SPRITE. NPC and MOVER both have a tick() function and a paint() function that is called to tick and draw them respectively, that's not my issue though.
When a map is loaded into my program, I would like to add these classes to a vector when needed. I would like to be able to have both MOVER and NPC (and in the future, more) objects kept in a vector like this:
Calling the functions works fine (assuming that the functions are put in the sprite class as virtual functions, blah blah blah) when I do something along the lines of:
1 2
|
sprites[i].tick();
sprites[i].paint();
|
What if MOVER has a variable (Let's call it MVAR) that only it will use, and I don't want to define it in SPRITE because NPC (and many more) classes don't need it. How can I access this variable from outside the NPC class? An example:
1 2 3 4 5 6
|
Pseudo Code:
if(sprites[i].id=="NPC"){ //If the classes id is marked as a NPC, blah
//We know it's an NPC, let's check what his message is to determine some miscellaneous stuff!
cout<<sprites[i].MVAR;
}
|
If I try and do this, I get an error. I'm very tired (and aggravated at many dead ends on trying to find a solution online) so if someone could (with detail please) point me in the right direction or please give me an example, I would be very grateful. I've see that dynamic_cast may be a solution, but I don't understand how I could use it here. I've also heard that it can be very slow if called often, but I'm not sure about if that's correct.
Any help would be extremely welcome and as I said earlier, an example showing some basic ways to accomplish what I'm trying to do here would be wonderful, as I can work with an example better than explanations (both are even better though). If someone could just write some pseudo code with the class and variable names I supplied, I think I could work most everything out for myself.
Sorry for any mistakes, I'm very tired. I have tried searching (and testing) much more than I would have liked too.