Game Development Community

Get the Callback function return value

by Gustavo Boni · in Torque Game Engine · 01/15/2007 (9:14 am) · 15 replies

Hey guys,

I've added an callback function to my object. I'm following the Basic Scriptable Object Resource.

So, i added a return value to the callback function
function myObject::onCallback(%this, %var)
{
   echo("myObject::onCallback(" @ %this SPC %var @ ") has been called");

   return true;
}

How can i get the return value of this callback in the c++ code?

void BasicObject::doCallback()
{
   Con::executef(this, 2, "onCallback", "string_variable");
}

Thanks

#1
01/15/2007 (2:04 pm)
As far as I know, you'd just catch it in a const char*:

function myObject::onCallback(%this, %var)
{
   echo("myObject::onCallback(" @ %this SPC %var @ ") has been called");

   return true;
}

void BasicObject::doCallback()
{
   [b]const char* result = [/b]Con::executef(this, 2, "onCallback", "string_variable");

[b]   Con::printf(result);[/b]
}
#2
01/15/2007 (7:06 pm)
Thanks Michael.

The problem now is i'm not getting any return value from the function. Looking the code example, the result var is empty. What am i missing?

Thanks
#3
01/15/2007 (7:13 pm)
First, is your echo statement executing? Are you seeing the print out in the console?
Second, do you have any syntax errors, or console errors occurring?

Last, what IDEs are you using to develop with? For example, I'm using MS Visual Studio 2005 Pro for C++, and Torsion for script. If you happen to be using both, you can jump in and out of script code and engine code using Torsion breakpoints and VS command "Attach to process." I find this very handy.

Let me know if you see any errors, I'll try to get the functionality to work for me.
#4
01/15/2007 (7:55 pm)
Quote:First, is your echo statement executing? Are you seeing the print out in the console?

Yes, i am.

Quote:Second, do you have any syntax errors, or console errors occurring?

None, the console is ok with no console errors.

Quote:Last, what IDEs are you using to develop with?

I'm using MS Visual C++ 2005 Express and Torsion. How can i jump in and out of the script and engine code, is ther any tutorial for that?
#5
01/15/2007 (8:08 pm)
Try this:

1) Load Torsion and VS Express, both prepared to run a debug build
2) In Torsion, put a break point on the first line of function myObject::onCallback
3) In VS Express, put a break point at the beginning of BasicObject::doCallback()
3) Launch a debug game (F5) from Torsion using the debug build you made using VS
4) Before you reach the situation you want to debug, alt+tab into VS Express.
5) In the file menu go to Debug->Attach to process
6) In the dialog box that pops up, select the currently running torque app and hit OK (or attach, whatever)
7) Now, alt+tab back into your game and proceed as normal.

When the callback functions are executed, you should jump into the engine first. From here, I'll assume you know how to debug and which keys to press (F10 to step, F5 to continue).

This process is simpler than it sounds, and is invaluable to debugging Torque. Saves you from putting echo commands and Con::printf()s everywhere.

Let me know how that goes, or if you need more help.
#6
01/15/2007 (8:24 pm)
Thank you very much Michael for the help. I need to go sleep now, tomorrow morning i'll try that and let you know how the things goes.

Thanks for the help
#7
01/16/2007 (7:59 am)
Hey Michael, just to let you know, my debug in torsion doesnt work. i''ll search in the forums how to do that. As soon i got it working, i post here.
#8
01/16/2007 (8:02 am)
Alrighty, good luck
#9
01/16/2007 (9:17 am)
Edit: Ops, now i got it really working.
#10
01/16/2007 (9:54 am)
You got the code working? Or the debugging working?
#11
01/16/2007 (10:08 am)
The debug Michael.

I just realized that if i return something like:

return 10;

I got a value in c++ just "1", maybe cause the return value is a const char *?

I created another class derived from BasicObject and now it works, but i don't get the full value like i said above.
#12
01/16/2007 (10:15 am)
Well, while the value is stored in a const char*, you'll only see the first part of the string.

Try converting it:
void BasicObject::doCallback()
{
   const char* result = Con::executef(this, 2, "onCallback", "string_variable");

[b]   int intResult = dAtoi(result);[/b]

   // if you wanted a one liner, you'd do this:
  // int result = dAtof(Con::executef(this, 2, "onCallback", "string_variable");
}
#13
01/16/2007 (10:22 am)
Yup, thanks Michael. it worked =)

Now, i'm just trying to figure out the reason for my old code doesnt work...both looks the same.

void BasicObject::doCallback()
{
   const char *test = Con::executef(this, 2, "onCallback", "string_variable");
   S32 res = dAtoi(test);
}

S32 State::execute()
{
	const char *rState = Con::executef(this, 2, "onExecuteState", "Executing...");
	S32 returnState = dAtoi(rState);
	
	return returnState;	
}
#14
01/16/2007 (10:33 am)
Well, I can't answer beyond what you've shown me =)
Since you are working off another resource, you could probably find the answers there if you post. Could have something to do with the implementation, or just bad code logic somewhere else.

If your new code works, I'd go ahead and save that and try to resolve the old logic later. If you still need to use that old code, ask around in the resource you are working off of. Sadly, I don't have the time or a fresh code base to implement it =(.

Any other concerns, other than that?
#15
01/16/2007 (10:44 am)
Hey Michael, i think i know now what is happening. ^^

The execute() is called only in c++ code. However if i call that in the script side like a ConsoleMethod:

%myObject.execute();

So, i can get the return argument!!

Crazy thing =P

How can i fix that? or theres no way to have a return argument in this way?