Now, this question does seem to have been asked millions of times before, but I think mine's a special case...
In any case, I have this class:
class Personality {
public:
Personality() {};
virtual void Start () {
}
virtual void Update () {
}
virtual ~Personality() {};
};
And I have this overridden version:
class example : public Personality {
public:
example() {};
void Start (FuzzyGame::FuzzyObject& o);
void Update (FuzzyGame::FuzzyObject& o);
~example() {};
};
Then I have my GameObject class (aka. FuzzyObject):
class FuzzyObject {
public:
std::string name;
std::string tag;
std::string type;
FuzzyForm xform;
bool enabled = false;
std::string className;
Personality Pclass;
std::string path;
std::string texPath;
MD2Object animMesh;
bool isMD2;
LightData light;
CamData cam;
GLuint Texture;
BillboardType BBType;
};
What I would like to know: is it possible to call a method, say Update(), from the example() class, with this instance of the base class, with some way of passing the class it is attached to (the "FuzzyObject") to that function?
Personality Pclass;
Basically, this is all so I can have a C++-based scripting system for my game engine.
UPDATE: From reading the answer I approved, I am actually using this way of doing it currently:
example e;
e.Start(/*whatever should be here*/);
But I found feeding the vector element in directly caused a segfault. I tried a pointer to the vector element, and that worked. sort of... Anyone know why feeding in a vector's element like this:
example.Start(&myArray<FuzzyObject>[i]); //assuming this is in a for-loop
Into my overloaded function modifies ALL the elements in my vector?!
aka. when running Update on one object (the code modifies the transform component to offset the affected object by 0.1) it offsets all the other objects too, which is obviously not what I want. Anyone know the cause of this weird behavior?
All I can assume is that the root of the problem is some weird memory allocation problem that only C++ would cook up... :D
UPDATE 2: I finally found the source of my other problem (update 1), and it was something REAL stupid that would only occur in OpenGL... :D
It turns out the reason it APPEARED to be modifying the whole bang lot in std::vector was actually not the vector pointer at all; it was because I had forgotten to load the identity matrix before drawing the next object! And likewise, as one was being moved, the previous transform made the others decide to drift along like drunken sailors... ;)
And likewise, sorry to waste you guys time... :D I should have known better to reset the matrix first! :)