Call .cs script function from .mm code [SOLVED]
by Brian Szatkowski · in iTorque 2D · 05/16/2011 (6:18 pm) · 6 replies
Hi All;
I am trying to figure out a way to call a .cs script function (not associated with a SimObject) from within a class method that resides in a .mm file. After some reading, it seemed as if the "Con::execute" method is what I am after, but I am running into roadblocks trying to implement that solution.
Can anyone verify if this is the correct strategy and, if so, post a snippet on how to properly use this approach? Otherwise, if this is not the correct solution, and you know of a better/proper way, could you please post that?
So, basically, I need an action (such as a native XCode GUI button press) that takes place in Objective-C to make a call to a function that exists in one of my .cs script files.
Thank you in advance for all of the helpful suggestions/solutions.
Find the solution HERE.
I am trying to figure out a way to call a .cs script function (not associated with a SimObject) from within a class method that resides in a .mm file. After some reading, it seemed as if the "Con::execute" method is what I am after, but I am running into roadblocks trying to implement that solution.
Can anyone verify if this is the correct strategy and, if so, post a snippet on how to properly use this approach? Otherwise, if this is not the correct solution, and you know of a better/proper way, could you please post that?
So, basically, I need an action (such as a native XCode GUI button press) that takes place in Objective-C to make a call to a function that exists in one of my .cs script files.
Thank you in advance for all of the helpful suggestions/solutions.
Find the solution HERE.
About the author
#2
Sorry for the vagueness. The code is rudimentary right now as I just want to get it working before I start adding in details.
In my .cs file
...and in my .mm file (inside a class call)
1.) I'm not sure what to put inside the call to get "helper()" to...well, get called. The examples I found all had some named SimObject that had a method associated with it as the first argument. Since my function is standalone, I'm not sure how the syntax works.
2.) Even though I do not have the correct syntax implemented yet, XCode gives an error stating "No matching function for call to executef".
Thoughts? Glaring oversights on my part?
Thanks!
05/16/2011 (7:03 pm)
Hi Michael;Sorry for the vagueness. The code is rudimentary right now as I just want to get it working before I start adding in details.
In my .cs file
function helper()
{
echo ("Showing help...");
}...and in my .mm file (inside a class call)
Con::executef(????) // Herein lies my confusion
1.) I'm not sure what to put inside the call to get "helper()" to...well, get called. The examples I found all had some named SimObject that had a method associated with it as the first argument. Since my function is standalone, I'm not sure how the syntax works.
2.) Even though I do not have the correct syntax implemented yet, XCode gives an error stating "No matching function for call to executef".
Thoughts? Glaring oversights on my part?
Thanks!
#3
#include "console/console.h"
05/17/2011 (2:15 am)
I'm guessing you just need to include the file that has the function:#include "console/console.h"
#4
Thank you both for your input.
Richard, although including "console.h" was not necessary (as it is already included earlier in the native Torque engine code), looking at that file did provide the reference to the syntax that I needed to make this happen. So, for that, I thank you.
For others interested, here is the correct solution:
In my .cs file...
...and in my .mm file:
Where:
Thanks!
05/17/2011 (4:51 am)
Hi Michael & Richard;Thank you both for your input.
Richard, although including "console.h" was not necessary (as it is already included earlier in the native Torque engine code), looking at that file did provide the reference to the syntax that I needed to make this happen. So, for that, I thank you.
For others interested, here is the correct solution:
In my .cs file...
function helper()
{
echo ("Showing help...");
}...and in my .mm file:
...
const char* callMe[] = {"nameOfFunction"}
Con::execute(1, callMe);
...Where:
- the array, callMe[], holds the name of the function/method to call along with any arguments being passed (none in my case)
- the call to Con::execute(1, arrayName) holds the number of indices/entries in the aforementioned array, followed by the array name
Thanks!
#5
If you want to start passing parameters:
05/17/2011 (6:13 am)
You can actually reduce the code to one line:Con::executef( 1, "nameOfFunction" );
If you want to start passing parameters:
int myVariable = 3; Con::executef( 2, "oniPhoneChangeOrientation", Con::getIntArg(myVariable) );
#6
05/17/2011 (6:38 am)
Nice! Thanks for the elaboration and simplification, Michael.
Community Manager Michael Perry
ZombieShortbus