Derived SimObject question
by John Vanderbeck · in Torque Game Engine · 04/08/2004 (6:11 pm) · 14 replies
Ok i'm working on making a tutorial object. Basically its a very simple scriptable simobject for people to start with as a base for making thier own scriptable objects.
Everythign is owrking except for one piece. I can't seem to get the C++ code to call a script function (for callbacks). It is driving me nuts. I'm sure i'm missing something obvious, but I just can't see it. Any help would be appreciated.
C++ code
Script code
And i'm calling it like this:
Tracing it in the debugger, its running into this problem in console.cc
Everythign is owrking except for one piece. I can't seem to get the C++ code to call a script function (for callbacks). It is driving me nuts. I'm sure i'm missing something obvious, but I just can't see it. Any help would be appreciated.
C++ code
void BasicObject::doCallback()
{
Con::executef(this, 2, "onCallback", "string_variable");
}
ConsoleMethod(BasicObject, doCallback, void, 2, 2, "Calls the BasicObject callback script function")
{
object->doCallback();
}Script code
function myObject::onCallback(%this, %var)
{
echo("myObject::onCallback(" @ %var @ ") has been called");
}And i'm calling it like this:
// $BasicObject is already created before this as follows:
function initTest()
{
$BasicObject = new BasicObject(myObject);
echo("BasicObject = " @ $BasicObject);
}
// i'm attempting to initiate the callback as follows
$BasicObject.doCallback();Tracing it in the debugger, its running into this problem in console.cc
Namespace::Entry *ent = object->getNamespace()->lookup(funcName);
if(!ent)
{
warnf(ConsoleLogEntry::Script, "%s: undefined for object '%s' - id %d", funcName, object->getName(), object->getId());
return "";
}
#2
04/08/2004 (6:37 pm)
No I hadn't. My chnages were all taking effect, so I didin't think to. I'll do one now. It only takes a few days on my machine :p
#3
04/08/2004 (6:46 pm)
Ok I get the exact same behaviour after a clean build.
#4
04/08/2004 (7:00 pm)
Are there any errors in the console? Maybe your BasicObject is not getting registered properly?
#5
04/08/2004 (7:10 pm)
All of the other functions in BasicObject (not shown above) work fine. They are all Script -> C++ and I can execute them all with no problems.
#6
Header file
04/08/2004 (7:35 pm)
Here is all the code.Header file
#ifndef _BASICOBJECT_H_
#define _BASICOBJECT_H_
#ifndef _SIMBASE_H_
#include "console/simBase.h"
#endif
class BasicObject : public SimObject
{
protected:
typedef SimObject Parent;
public:
BasicObject();
~BasicObject();
bool processArguments(S32 argc, const char **argv);
bool onAdd();
void onRemove();
static void initPersistFields();
void setVariable(int var);
int sum(int a, int b);
void doCallback();
private:
int mMemberVar;
public:
DECLARE_CONOBJECT(BasicObject);
};
#endif // _BASICOBJECT_H_
#7
04/08/2004 (7:35 pm)
C++ Source#include "console/basicobject.h"
#include "console/simBase.h"
#include "console/consoleInternal.h"
IMPLEMENT_CONOBJECT(BasicObject);
BasicObject::BasicObject()
{
mMemberVar = 0;
}
BasicObject::~BasicObject()
{
}
bool BasicObject::processArguments(S32 argc, const char **argv)
{
if(argc == 0)
return true;
else
return true;
return false;
}
bool BasicObject::onAdd()
{
return Parent::onAdd();
}
void BasicObject::onRemove()
{
Parent::onRemove();
}
void BasicObject::initPersistFields()
{
Parent::initPersistFields();
}
//-----------------------------------------------------------------------
void BasicObject::setVariable(int var)
{
mMemberVar = var;
}
int BasicObject::sum(int a, int b)
{
return a + b;
}
void BasicObject::doCallback()
{
Con::executef(this, 2, "onCallback", "string_variable");
}
//-----------------------------------------------------------------------
ConsoleMethod(BasicObject, setVariable, void, 3, 3, "(int variable) Sets a variable in the BasicObject")
{
object->setVariable(dAtoi(argv[2]));
}
ConsoleMethod(BasicObject, sum, S32, 4, 4, "(int var1, int var2) sums the two ints")
{
return object->sum(dAtoi(argv[2]),dAtoi(argv[3]));
}
ConsoleMethod(BasicObject, doCallback, void, 2, 2, "Calls the BasicObject callback script function")
{
object->doCallback();
}
#8
The rest of the "script code" is just m typing into the console, such as
04/08/2004 (7:37 pm)
Script codefunction initTest()
{
$BasicObject = new BasicObject(myObject);
echo("BasicObject = " @ $BasicObject);
}
function myObject::onCallback(%this, %var)
{
echo("myObject::onCallback(" @ %var @ ") has been called");
}
function endTest()
{
$BasicObject.destroy();
}The rest of the "script code" is just m typing into the console, such as
// These two work fine echo ($BasicObject.sum(1,2)); $BasicObject.setVariable(100); // This does not $BasicObject.doCallback();
#9
I'm assuming you saw it already, but it might be worth quickly checking out this thread on script callbacks.
That thread isn't extremely informative, but it's the closest thing I could find to info on the topic. Even if the proposed solution works for your situation, it'd still be nice to know exactly what's happening here.
What release are you working with? If you don't mind, send me the relevant C++ and script files, joshw@garagegames.com, and I'll try to have a gander tomorrow.
04/09/2004 (12:57 am)
Hey John,I'm assuming you saw it already, but it might be worth quickly checking out this thread on script callbacks.
That thread isn't extremely informative, but it's the closest thing I could find to info on the topic. Even if the proposed solution works for your situation, it'd still be nice to know exactly what's happening here.
What release are you working with? If you don't mind, send me the relevant C++ and script files, joshw@garagegames.com, and I'll try to have a gander tomorrow.
#10
A namespace is created for "BasicObject", but one is NOT created for "myObject". I "assumed" one was because thats how it works in most of the other objects. Turns out that that only happens if the Object's code specifically does it. If I change my in-script callback to use the BasicObject namespace it gets called correctly.
So what I need to do now is try to figure out what code I need in my C++ code to also create the "named" namespace like most of the other objects do.
04/09/2004 (5:17 am)
Ok after reading some things I think I know what the problem is. It has to do with namespaces. When I create an instance of the object in script like this:$BasicObject = new BasicObject(myObject);
A namespace is created for "BasicObject", but one is NOT created for "myObject". I "assumed" one was because thats how it works in most of the other objects. Turns out that that only happens if the Object's code specifically does it. If I change my in-script callback to use the BasicObject namespace it gets called correctly.
So what I need to do now is try to figure out what code I need in my C++ code to also create the "named" namespace like most of the other objects do.
#11
04/09/2004 (5:19 am)
Woohoo! Got it working. That was easy once I figured out what my problem was :) Now I can release this to the community to learn from.
#12
04/09/2004 (7:31 am)
Good job. :P
#14
04/12/2004 (11:26 am)
Thanks John :)
Associate Kyle Carter