Game Development Community

console functions

by Nick Carnevalino · in Torque Game Engine · 05/05/2002 (1:29 pm) · 2 replies

ok this has been driving me nutz for a few days now .. i know it's probly somthing simple and i've read the docs about 5 times now ...

here is what i want to do ... i have a class defined in the engine .. and that class has a simple function ( this is a simple fictonal example ) lets say the function called Math::function(int a,int b,int c) looks like this:
if(!c){
a+b=q}
else
{
a+b+c=q;
}

return q;

now i would like to call it on the console like
Addit(1,4); and get the result 5 or Addit(1,4,1); and get 6 .... i cant get the thing to work ... this is the cloesest it have been able to get
ConsoleFunction(Addit, int, 2, 3,"Addit(a, b, c);")
{	
	Addit Math;
	int argc;
	Math.a = *argv[1];
	Math.b = *argv[2];
	Math.c = *argv[3];
	int result=Math.function();
	Con::printf("test");
	Con::printf("test2 %d",result);
}

i have tried Math.a = argv[1] and the compiler spits that out saying "array type not defined" and i have tried argc ( like the docs say ) and i get compiler arrors about re-defining a predefined type.

i'm just trying to debug some math functions that do work ( i.e. they are properly defined classes even though the example above my not be ) but i cant seem to figure out how to pass console int's to the class then get back the result.

can anyone see anything simple i am missing? or if my assumption that this can even be done is wrong then please let me know.

#1
05/05/2002 (4:02 pm)
From what I can see I believe that you have things wrong on a couple of parts. First you have 2 , 3 which means that you have to pass at least 1 arguments but can pass 2 arguments...I assume by the text after that that you wanted it at least 2 and can be 3. you need to change those to 3 , 4. Second of all you have int in the console function definition. That is what type will be returned from the function. Since I do not see a return statement I assume you thought that was what would be passed in. That needs to be changed to void unless you want to add a return statement. Third, even though you passed it ints in the console, it converted them to strings so you have to convert these strings back into ints inside your consolefunction.

you need to do:

Math.a = dAtoi(argv[1]);
Math.b = dAtoi(argv[2]);
if (argc > 3)
Math.c = dAtoi(argv[3]);

you also just need to define argc like this:

argc;

no int before it.

you should also use S32 instead of int.

Here is your function rewritten.
ConsoleFunction(Addit, S32, 3, 4,"Addit(a, b [, c] );")
{	
	Addit Math;
	argc;
        Math.a = dAtoi(argv[1]);
        Math.b = dAtoi(argv[2]);
        if (argc > 3)
             Math.c = dAtoi(argv[3]);
	
	S32 result = Math.function();
        
        return result;	
}
Now you can call in the console:

echo(Addit(4,1));
-> 5

-Tim aka Spock
#2
05/05/2002 (5:50 pm)
thank you :-) ... nothing feels better than bangin your head on a problem then finnaly get it working :-) !!