creating a script "class" in C++
by T · in iTorque 2D · 03/27/2009 (12:34 pm) · 2 replies
Hi I'm wondering if anyone can help me to create a script class in C++.
In script I define a class with its own methods called "class_A." To define this class I create a new Animated sprite and do
%newClassA = new t2dAnimatedSprite()
{
class = "Class_A"
}
This works fine in script, I can call Class_A's member function with %newClassA.functions .
Now I'm converting my classes to C++ and I need to spawn "Class_A" objects on the fly and associated them with their script methods.
So in C++ I tried to do
t2dAnimatedSprite *newClassA = new t2dAnimatedSprite();
newClassA->setClassNamespace("Class_A");
Then I tried to call a function that belong to Class_A
executef(newClassA, 1, "classA_function");
This gives me an error telling me newClassA doesn't know what "classA_function" is. Which is because newClassA is a t2dAnimatedSprite pointer and not a "Class_A" pointer. So I'm missing a step that declares and allocates a "Class_A" and getting a pointer to it to make my executef call.
I tried the call newClassA->linkNamespace() but that won't compile and returns an unresolved linker error.
Any help would be appreciated
In script I define a class with its own methods called "class_A." To define this class I create a new Animated sprite and do
%newClassA = new t2dAnimatedSprite()
{
class = "Class_A"
}
This works fine in script, I can call Class_A's member function with %newClassA.functions .
Now I'm converting my classes to C++ and I need to spawn "Class_A" objects on the fly and associated them with their script methods.
So in C++ I tried to do
t2dAnimatedSprite *newClassA = new t2dAnimatedSprite();
newClassA->setClassNamespace("Class_A");
Then I tried to call a function that belong to Class_A
executef(newClassA, 1, "classA_function");
This gives me an error telling me newClassA doesn't know what "classA_function" is. Which is because newClassA is a t2dAnimatedSprite pointer and not a "Class_A" pointer. So I'm missing a step that declares and allocates a "Class_A" and getting a pointer to it to make my executef call.
I tried the call newClassA->linkNamespace() but that won't compile and returns an unresolved linker error.
Any help would be appreciated
About the author
#2
Thanks for the quick response, I've switched to code to do
newClassA->setClass(newClassA,"Class_A");
So this should associate the object with the script "class". But when I call the script method with
Con::executef(newClassA, 1, "classA_function");
I get an error message that says "0 doesn't have a namespace". The problem is that the newClassA isn't registered with the simulator so it doesn't have an ID. So when I switched to
newClassA->setClass(newClassA,"Class_A");
newClassA->registerObject();
Con::executef(newClassA, 1, "classA_function");
"ClassA_function" was called correctly. Thank you for your help.
03/27/2009 (1:33 pm)
Hi Mat,Thanks for the quick response, I've switched to code to do
newClassA->setClass(newClassA,"Class_A");
So this should associate the object with the script "class". But when I call the script method with
Con::executef(newClassA, 1, "classA_function");
I get an error message that says "0 doesn't have a namespace". The problem is that the newClassA isn't registered with the simulator so it doesn't have an ID. So when I switched to
newClassA->setClass(newClassA,"Class_A");
newClassA->registerObject();
Con::executef(newClassA, 1, "classA_function");
"ClassA_function" was called correctly. Thank you for your help.
Torque 3D Owner Mat Valadez
Default Studio Name
When you set a script object's class field, all that does is tell the script interpreter to look up function in the namespace first.
So if you create an object and associate it with the class you want, should only have to call the function from the object.
I don't have the source in front of me, but in C++, I think you do that back calling executef("functionName", object_pointer), and it'll look up the function as an object method.