addCommand method
by Rob Segal · in Torque Game Engine · 08/12/2001 (5:45 pm) · 12 replies
I'm trying to figure what exactly the addCommand method is doing. Its a member of clas "Con".
Console.cc is the c++ source file. When the test app calls addCommand is it sending messages out to
a queue or something like that. The code for the addCommand method is just one line and not making
a whole lot of sense.
This making any sense to anyone?
Console.cc is the c++ source file. When the test app calls addCommand is it sending messages out to
a queue or something like that. The code for the addCommand method is just one line and not making
a whole lot of sense.
This making any sense to anyone?
#2
08/12/2001 (6:24 pm)
I think you use addCommand to get a command in the scripting language to be recognized, and associate it with a C++ function.
#3
>associate it with a C++ function.
I think you may be right about this Mark. There's other functions similar to addCommand such as
'addVariable', 'getVariable' which I think are used to set environment variables to be used by the
game scripts. Anyone agree with that?
08/12/2001 (7:35 pm)
>I think you use addCommand to get a command in the scripting language to be recognized, and>associate it with a C++ function.
I think you may be right about this Mark. There's other functions similar to addCommand such as
'addVariable', 'getVariable' which I think are used to set environment variables to be used by the
game scripts. Anyone agree with that?
#4
But then, I'm pretty new to the scene.
-Rogue101
08/12/2001 (9:34 pm)
That's been my read on it so far, anyways (:But then, I'm pretty new to the scene.
-Rogue101
#5
08/13/2001 (12:23 am)
Con is not a class, it's a namespace. You set a callback function with the addCommand function.
#6
Yes my mistake it is a namespace.
08/13/2001 (3:38 pm)
>Con is not a class, it's a namespace. You set a callback function with the addCommand function.Yes my mistake it is a namespace.
#7
08/14/2001 (11:10 am)
I believe that addcommand is used to create global console functions for use from within script. I'm not sure how it differs from consolefunction, but it seems to be used for more basic functions. ie: vector/matrix functions, etc. I also don't have a great grasp of how the compiler works, so I can't tell more than that.
#8
All classes that are derived from say ShapeBase have an initializer method called consoleInit(). Inside this method you can add console commands that let you call methods on that object from script. Here is an example:
Lets say I need a linear projectile class.
in our header...
class LinearProjectile : public ShapeBase
{
Point3F mInitialPos;
Point3F getInitialPosition() { return mInitialPos; }
// bunch of other methods here
// initialize our interface with script
consoleInit();
}
in our cc file...
LinearProjectile::consoleInit()
{
Con::addCommand("LinearProjectile", "getInitPos", cGetInitPos, "obj.getInitPos()", 2, 2);
}
addCommand takes 6 vars.
1. Name of the interfacing class
2. defined script function name
3. the static function to call in c code
4. output when called with wrong arg count
5. min number of args that NEED to be passed
6. max number of args that CAN be passed
in the same cc file we need to add the static fucntion cGetInitPos
static const char *cGetInitPos(SimObject *obj, S32 argc, const char *argv)
{
// here we cast this to our LinearProjectile
LinearProjectile *lp = static_cast(obj);
// buffer used to send results back to console
char* buff = Con::getReturnBuffer(256);
Point3F p = lp->getInitialPosition();
dSprintf(buff, 128, "%f %f %f", p.x, p.y, p.z);
return buff;
}
in script...
function Weapon::onFire( %this, %obj )
{
// by way of our console function
%position = %obj.getInitPos();
}
Hope this helps somewhat.
-jett
08/15/2001 (11:42 am)
For any class that you create(projectiles, items.. ect), you can add commands that can be called from script on an instance of that class. In other words....All classes that are derived from say ShapeBase have an initializer method called consoleInit(). Inside this method you can add console commands that let you call methods on that object from script. Here is an example:
Lets say I need a linear projectile class.
in our header...
class LinearProjectile : public ShapeBase
{
Point3F mInitialPos;
Point3F getInitialPosition() { return mInitialPos; }
// bunch of other methods here
// initialize our interface with script
consoleInit();
}
in our cc file...
LinearProjectile::consoleInit()
{
Con::addCommand("LinearProjectile", "getInitPos", cGetInitPos, "obj.getInitPos()", 2, 2);
}
addCommand takes 6 vars.
1. Name of the interfacing class
2. defined script function name
3. the static function to call in c code
4. output when called with wrong arg count
5. min number of args that NEED to be passed
6. max number of args that CAN be passed
in the same cc file we need to add the static fucntion cGetInitPos
static const char *cGetInitPos(SimObject *obj, S32 argc, const char *argv)
{
// here we cast this to our LinearProjectile
LinearProjectile *lp = static_cast
// buffer used to send results back to console
char* buff = Con::getReturnBuffer(256);
Point3F p = lp->getInitialPosition();
dSprintf(buff, 128, "%f %f %f", p.x, p.y, p.z);
return buff;
}
in script...
function Weapon::onFire( %this, %obj )
{
// by way of our console function
%position = %obj.getInitPos();
}
Hope this helps somewhat.
-jett
#10
ConsoleFunction(name,returnType,minArgs,maxArgs,usage)
ConsoleMethod(className,name,returnType,minArgs,maxArgs,usage)
example:
ConsoleFunction(HelloWorld,void,1,1,"HelloWorld();")
{
Con::printf("Hello World!");
}
or
ConsoleMethod(SimObject,HelloWorld,void,1,1,"obj.HelloWorld();")
{
Con::printf("Hello World!");
Con::printf("Called on object %d", object->getId());
}
These macros are declared at the bottom of console.h and negate the need to add them into the console seperately.
08/15/2001 (12:21 pm)
Though you can add console commands through addCommand, that has been depricated (but looks like the word didn't get out too far :) - an easier way to add commands to the console is through the ConsoleFunction and ConsoleMethod macros:ConsoleFunction(name,returnType,minArgs,maxArgs,usage)
ConsoleMethod(className,name,returnType,minArgs,maxArgs,usage)
example:
ConsoleFunction(HelloWorld,void,1,1,"HelloWorld();")
{
Con::printf("Hello World!");
}
or
ConsoleMethod(SimObject,HelloWorld,void,1,1,"obj.HelloWorld();")
{
Con::printf("Hello World!");
Con::printf("Called on object %d", object->getId());
}
These macros are declared at the bottom of console.h and negate the need to add them into the console seperately.
#11
console/scripts, it _has_ to be a static C function?
08/15/2001 (2:35 pm)
So in order to make a function callable from theconsole/scripts, it _has_ to be a static C function?
#12
ConsoleMethod(SimSet, getCount, S32, 2, 2, "set.getCount()")
{
argc; argv;
return ((SimSet *) object)->size();
}
08/15/2001 (6:44 pm)
Yes and no... a simple C wrapper around the function could work to dispatch to the proper class method... example:ConsoleMethod(SimSet, getCount, S32, 2, 2, "set.getCount()")
{
argc; argv;
return ((SimSet *) object)->size();
}
Torque Owner Josh Albrecht