Help converting from obsolete Con::addCommand to new ConsoleMeth
by Ryan Scott · in Torque Game Engine · 10/29/2003 (6:17 pm) · 5 replies
I've got a tutorial referencing this:
Could someone help me convert that to the new ConsoleMethod()?
Any help would be greatly appreciated.
Thanks,
R
void Trigger::consoleInit()
{
// Object level commands
// Manipulation commands
Con::addCommand("Trigger", "setOwner", cTriggerSetOwner, "[TriggerObject].setOwner(obj)", 3, 3);
}
void cTriggerSetOwner(SimObject* obj, S32 argc, const char** argv )
{
AssertFatal(dynamic_cast<Trigger*>(obj) != NULL, "Error, how did a non-trigger get here?");
Trigger* trigger = static_cast<Trigger*>(obj);
if (argc > 2)
{
GameBase* owner = NULL;
if (Sim::findObject(argv[2], owner) == false)
return;
trigger->setOwner(owner);
}
}
void Trigger::setOwner(GameBase *owner)
{
mOwner = owner;
}Could someone help me convert that to the new ConsoleMethod()?
Any help would be greatly appreciated.
Thanks,
R
#2
Worked great -- I didn't realize I could remove the
That part was throwing me.
Again, thanks for taking time to help out-
R
10/29/2003 (7:00 pm)
Many thanks Ben! Worked great -- I didn't realize I could remove the
AssertFatal(dynamic_cast<Trigger*>(obj) != NULL, "Error, how did a non-trigger get here?"); Trigger* trigger = static_cast<Trigger*>(obj);
That part was throwing me.
Again, thanks for taking time to help out-
R
#3
I'm using the latest HEAD from about a week ago and it has tons of them.
10/30/2003 (5:45 am)
What is wrong with Con::addCommand() ??I'm using the latest HEAD from about a week ago and it has tons of them.
#4
Using Con::addCommand is deprecated because it's more complicated and dispersed than ConsoleMethod/ConsoleFunction. In Ryan's example, we went from about 20 lines to 8 lines. We got rid of of several dispersed definitions. The usage string is now right next to the code that it describes, meaning it's easier to keep up to date. We don't have a "dummy" function (ie, cSetOwner) that we have to worry about namespacing, and we don't have to maintain consoleInit(). In about 90% of cases, moving to Con::addCommand means you can get rid of consoleInit entirely.
The code is also very readable. Most ConsoleMethods end up being:
Which is a lot nicer than:
Note that ConsoleMethod already does the casting and AssertFatal() internally, which saves you a lot of effort. In a release build, it drops the assert, so that you get a small performance gain. You can also strip out documentation for release builds using ConsoleMethod; the same is not possible using Con::addCommand. (You can disable docs if you're using Con::addCommand, but the strings will still be present in the final application.)
ConsoleFunction is especially nice because you don't have to find a place to stuff your Con::addCommand call. Both ConsoleMethod and ConsoleFunction are essentially self-registering.
It's not so much what's wrong with Con::addCommand, but what's right with ConsoleMethod/ConsoleFunction. Hope that explains things.
10/30/2003 (7:34 am)
Where? I removed the last few from fxLight and the Mac and Unix platform layers. There should be none in HEAD.Using Con::addCommand is deprecated because it's more complicated and dispersed than ConsoleMethod/ConsoleFunction. In Ryan's example, we went from about 20 lines to 8 lines. We got rid of of several dispersed definitions. The usage string is now right next to the code that it describes, meaning it's easier to keep up to date. We don't have a "dummy" function (ie, cSetOwner) that we have to worry about namespacing, and we don't have to maintain consoleInit(). In about 90% of cases, moving to Con::addCommand means you can get rid of consoleInit entirely.
The code is also very readable. Most ConsoleMethods end up being:
ConsoleMethod( SomeObject, SomeMethod, int, 2, 2, "() do something")
{
return object->SomeMethod();
}Which is a lot nicer than:
void SomeObject::consoleInit()
{
Con::addCommand( "SomeObject", "SomeMethod", cSomeMethod, "() do something", 2, 2);
}
static int cSomeMethod(SimObject * obj, int argc, const char *argv[])
{
AssertFatal(dynamic_cast<SomeObject*>(obj) != NULL, "Error, how did a non-SomeObject get here?");
SomeObject* so = static_cast<SomeObject*>(obj);
return so->SomeMethod();
}Note that ConsoleMethod already does the casting and AssertFatal() internally, which saves you a lot of effort. In a release build, it drops the assert, so that you get a small performance gain. You can also strip out documentation for release builds using ConsoleMethod; the same is not possible using Con::addCommand. (You can disable docs if you're using Con::addCommand, but the strings will still be present in the final application.)
ConsoleFunction is especially nice because you don't have to find a place to stuff your Con::addCommand call. Both ConsoleMethod and ConsoleFunction are essentially self-registering.
It's not so much what's wrong with Con::addCommand, but what's right with ConsoleMethod/ConsoleFunction. Hope that explains things.
#5
10/30/2003 (8:50 am)
Yes it does explain things very nicely. Thanks. It seems I was wrong.. I just went back and looked at the files I Was messing with earlier, and its con::addVariable that I was seeing not con:addCommand.
Associate Kyle Carter
Replace the void cTrigger... with a ConsoleMethod line.
Follow the example elsewhere.
In this case:
ConsoleMethod( Trigger, setOwner, void, 3, 3, "Set owner.") { GameBase* owner = NULL; if (Sim::findObject(argv[2], owner) == false) return; object->setOwner(owner); }