Game Development Community

Engine source code question

by Yujiao Guo · in General Discussion · 11/19/2007 (11:47 am) · 6 replies

# define ConsoleFunction(name,returnType,minArgs,maxArgs,usage1) \
static returnType c##name(SimObject *, S32, const char **argv); \
static ConsoleConstructor g##name##obj(NULL,#name,c##name,usage1,minArgs,maxArgs); \
static returnType c##name(SimObject *, S32 argc, const char **argv)

Where does const char **argv comes from and what does it mean?

Thanks!!!

#1
11/19/2007 (11:56 am)
Argv is a list of arguments in the format of a char *. The idea is that you give your script function some parameters, then you use the appropriate conversion function to convert from char to whatever type you need ( e.g., dAtoi for int (S32), dAtob for bool, dAtof for float (F32)) for your C++ function, and call that.

So, for instance:
ConsoleFunction( foo, void, 2, 2, "foo - takes a number and prints the number + 1 to the console" )
{
 // It's the [1] of the argv array because the first is the function name.
 S32 bar = dAtoi( argv[1] );
 bar++;
 Con::printf( "Foo gave you %d", bar );
}
// If you called foo( 1 ); from the console, it would output "Foo gave you 2".

Hope that helps.
#2
11/19/2007 (12:12 pm)
Thanks a lot. it is very helpful.

However, i still have a few questions(sorry i only know the basic features of C++):

1.
how comes you can use one define to define 3 versions of fuctions. How does the compiler determine which definition to use. For example: when will "static ConsoleConstructor g##name##obj(NULL,#name,c##name,usage1,minArgs,maxArgs);" be used?
hum...actually I think one #define defines 3 versions of functions all the time. Am I right?

2.
when you define foo as:
ConsoleFunction( foo, void, 2, 2, "foo - takes a number and prints the number + 1 to the console" )
How would it be defined?
Is it like this? static void cfoo(SimObject *, S32, const char **argv);
But then you are not passing in parameter values like SimObject *, S32.

Thanks!!!

I am trying to learn every bits of the engine. :}
#3
11/19/2007 (12:17 pm)
Uh, not sure about that ;) I just know how to use them, really the guts of the ConsoleFunction/Method stuff isn't something I need to mess with much myself. Someone else might know more about it.
#4
11/19/2007 (12:22 pm)
Thank you all the same.
#5
11/19/2007 (1:55 pm)
No problem. Also, just a quick note on the use of ConsoleMethod since you'll probably want to look at that next, when you define a ConsoleMethod, the min arguments includes the class name, so it's like this:

ConsoleMethod( className, functionName, returnType, minArgs, maxArgs, usageString )

So for minArgs, you'd need 2 (one for class name plus one for function name) plus however many actual arguments you have.

Also, when inside the ConsoleMethod, you access your class object by using the object variable. So for a class named foo with a member function bar, it'd be like object->bar().
#6
11/20/2007 (4:51 pm)
Thanks. That's informative. :}