Game Development Community

Exposing events to TorqueScript

by John Doppler Schiff · in Torque Game Engine · 07/31/2006 (7:21 pm) · 5 replies

Hi guys,
I need to trigger a TorqueScript function when SceneObject::inspectPostApply() executes. Unfortunately, I'm a newbie to C++ and I'm not sure of the best way to implement this.

My first thought was that Con::execute is the way to go. The Torque docs give the following example usage:

const char * execute (S32 argc, const char *argv[])
Call a script function from C/C++ code.

Parameters
argc Number of elements in the argv parameter

argv A character string array containing the name of the function to call followed by the arguments to that function.

/// // Call a Torque script function called mAbs, having one parameter.
/// char* argv[] = {"abs", "-9"};
/// char* result = execute(2, argv);
/// [/code]

I've added console.h to the includes, and specified the Con:: namespace...
char* argv[] = {"someFunc", "foo"};
char* result = Con::execute(2, argv);

The compiler doesn't like that, however:

error C2665: 'execute' : none of the 2 overloads can convert parameter 2 from type 'char *[2]'

In console.h, execute takes the following arguments: S32 argc, const char *argv[]

Can someone throw me a bone and (a) tell me where I booched, or (b) if I'm on the wrong track completely? The newbie help is tremendously appreciated!

-- JohnDopp

#1
08/01/2006 (8:43 am)
Quote:error C2665: 'execute' : none of the 2 overloads can convert parameter 2 from type 'char *[2]'

What is it saying it's trying to convert TO?
#2
08/01/2006 (12:27 pm)
That's the problem -- it doesn't! That's the error message in its entirety. I have no clue what it's looking for. =7
#3
08/01/2006 (12:47 pm)
You're probably on the wrong track completely.

Honestly, I don't understand what's so hard with looking at how it's done already, there are a hundred examples of this in the engine already. :) Would had saved you some time.

Anyway.

Use Con::executef().

You cannot pass an array as a argument.

Con::executef ( this,  2, "testFunction" ); // would equal [b]this::testFunction ( %this )[/b] in script.

Con::executef ( 3, "testFunction", arg1, arg2 ); // would equal [b]testFunction ( %arg1, %arg2 )[/b] in script.

Examples in the engine:

Con::executef(this, 2, "onURL", url);
Con::executef(2, "exec", settings);
Con::executef(mDataBlock,2,"onAdd",scriptThis());
Con::executef( 4, "onServerQueryStatus", "done", "No master servers found.", "0" );

Edit: Corrections.
#4
08/01/2006 (12:53 pm)
Thanks much, Stefan! I never searched for examples, foolishly assuming that the documentation on Con::execute() was correct... Silly me. =)

Let me digest this and see how things go. Appreciate the help!

-- JohnDopp
#5
08/01/2006 (1:08 pm)
I'm actually not sure what Con::execute does, but I have never found a need for it directly.
Seems like the quoted part of the documentation is old.

What's so cool about all this is that you can return whatever you want in the scripted function you're calling, and then use it in C++. Very slick.

Have fun!