Game Development Community

Turning Script to C

by Richard_H · in Torque Game Engine · 01/09/2007 (6:20 pm) · 8 replies

Hi,

For my game I have a complex script which I use the measure the distance between triggers. It has a variable which tells it what the next trigger is going to be. It calls itself on the next trigger and continues this until it reaches something other than a PathTrigger (special TriggerDatablock that works sort of like a path node).
I need help translating this to C++.
Here is the script code (please don't steal it):
function pathTrigger::distToNext(%this, %trigger, %path)
{
	if($debugMode)
		echo("%trigger is " @ %trigger @ " and %this is " @ %this);
	%evalString = "return " @ %trigger @ "." @ %path @ ".getID();";
	if($debugMode)
		echo("Eval: " @ %evalString);
	%theObject = eval(%evalString);
	if($debugMode)
		echo("OUTPUT: " @ %theObject);
	%evalString = "return getBoxCenter(" @ %theObject @ ".getWorldBox());";
	if($debugMode)
		echo("-Eval: " @ %evalString);
	%destPos = eval(%evalString);
	if($debugMode)
		echo("-OUTPUT: " @ %destPos);
	%ourPos = %trigger.getPosition();
	%dist = VectorDist(%destPos, %ourPos);
	if($debugMode)
		echo("%dist = " @ %dist);
	%evalString = "return " @ %theObject @ ".getDatablock();";
	if($debugMode)
		echo("--Eval: " @ %evalString);
	%objectDatablock = eval(%evalString);
	if($debugMode)
		echo("--OUTPUT: " @ %objectDatablock @ " (" @ %objectDatablock.getName() @ ")");
	%otherDist = 0;
	if(%objectDatablock.getName() $= "pathTrigger")
	{
		%evalString =  "return " @ %this @ ".distToNext(" @ %theObject @ ", " @ %path @ ");";
		if($debugMode)
			echo("---Eval: " @ %evalString);
		%otherDist = eval(%evalString);
		if($debugMode)
			echo("---OUTPUT: " @ %otherDist);
	}
	%totalDist = %dist + %otherDist;
	return %totalDist;
}
Any help in translating this would be apreciated (espesialy with comments).
Thanks in advance!

#1
01/10/2007 (10:50 am)
I'm having particular trouble with a the dynamic variable name. Does anyone know to get a variable with a dynamic name?
#2
01/10/2007 (11:18 am)
The above script is not very advanced so should be pretty easy to port.
To get a dynamic variable name (global) you use:

get - Float / Int / Bool - Variable ()

ie:
getFloatVariable ( "$blabla" );

or in your case:
getBoolVariable ( "$debugMode" );
#3
01/10/2007 (3:12 pm)
Well, I'm trying to get the variable in %trigger which has a name %path. This is the code I have now which is riddled with errors already. Can you help me weed it out?
//TriggerData::distToNext (returns a string) - takes the ID of a trigger and the name of a variable in that trigger
ConsoleMethod( TriggerData, distToNext, (const char*), 4, 4, "Gets the distance until the next the next trigger. (%trigger, %path)")
{
	SimObject trigger = argv[2];
	SimObject nextObject = Sim::findObject(trigger.getIntVariable(argv[3]));
	S32 dist = 0;
	return dist;
}
It seems to be complaining about everything!
Quote:
/Users/alex/Code/Torque/SilentKiller/Torque SDK/engine/game/trigger.cc:20: error: parse error before 'const'
/Users/alex/Code/Torque/SilentKiller/Torque SDK/engine/game/trigger.cc:20: error: parse error before 'const'
/Users/alex/Code/Torque/SilentKiller/Torque SDK/engine/game/trigger.cc:20:132: pasting "conmethod_return_" and "(" does not give a valid preprocessing token
/Users/alex/Code/Torque/SilentKiller/Torque SDK/engine/game/trigger.cc: In function 'int conmethod_return_(const char*)':
/Users/alex/Code/Torque/SilentKiller/Torque SDK/engine/game/trigger.cc:20: error: parse error before ')' token
/Users/alex/Code/Torque/SilentKiller/Torque SDK/engine/game/trigger.cc:20: error: 'cTriggerDatadistToNextcaster' undeclared (first use this function)
/Users/alex/Code/Torque/SilentKiller/Torque SDK/engine/game/trigger.cc:20: error: parse error before 'const'
/Users/alex/Code/Torque/SilentKiller/Torque SDK/engine/game/trigger.cc:23: error: 'trigger' undeclared (first use this function)
/Users/alex/Code/Torque/SilentKiller/Torque SDK/engine/game/trigger.cc:23: error: 'argv' undeclared (first use this function)
/Users/alex/Code/Torque/SilentKiller/Torque SDK/engine/game/trigger.cc:25: error: parse error before 'return'
/Users/alex/Code/Torque/SilentKiller/Torque SDK/engine/game/trigger.cc:28: error: cannot declare static function inside another function
/Users/alex/Code/Torque/SilentKiller/Torque SDK/engine/game/trigger.cc:28: error: cannot declare static function inside another function
/Users/alex/Code/Torque/SilentKiller/Torque SDK/engine/game/trigger.cc:28: confused by earlier errors, bailing out
Can someone explain what I'm doing wrong and what I should be doing?
#4
01/10/2007 (3:32 pm)
Okay, I thought you wanted to grab global variables from the scriptside. Ignore my previous advice. The above code should look along the lines of this:

//TriggerData::distToNext (returns a string) - takes the ID of a trigger and the name of a variable in that trigger
ConsoleMethod( TriggerData, distToNext, (const char*), 4, 4, "Gets the distance until the next the next trigger. (%trigger, %path)")
{	
        // findObject returns a SimObject, so we need to convert it.
        // If you want to be 100% safe, do a dynamic_cast instead. It's slower
        // but if you were to pass an incorrect object ID, it will save you and not
        //  do odd stuff to your application.
        Trigger * trigger = static_cast<Trigger *> (Sim::findObject ( argv[2] ) );

        // Not sure what type of path you are looking for here, SimPath perhaps?
        Path * path = static_cast<Path *> (Sim::findObject ( argv[3] ) );

        S32 dist = 0;

        return dist;
}

Why are you using TriggerData? That's the datablock, not the actual trigger object. Are you sure you want to do this?
#5
01/10/2007 (4:04 pm)
Thanks Stefan!
This isn't exactly what I wanted, but it's a big help.

My code works a bit wierd.
%path is the name of a variable in the trigger object. Each trigger is like a path node with a bunch of variables that lead to the next trigger in that path. Like the "w1" path leads to water source 1, and what I'm trying to do is get the ID of whatever the next object in the path is. So once I have the trigger, the next thing I want to do is get the next trigger in the series.

P.S. My script was calling the datablock so I just assumed I should use it. Now that I look back at it, I think I will use the trigger. That means I can replace trigger with the supplied object variable, right?

Edit: Changed content a bunch
#6
01/10/2007 (4:28 pm)
I now have this
ConsoleMethod( TriggerData, distToNext, U32, 3, 3, "(%path)")
{
	SimObject nextObject = Sim::findObject(object.getIntVariable(argv[2]));
	U32 dist = 0;
	return dist;
}
All my errors appear focused on that first line.
Is there anything obviously wrong with it?
#7
06/07/2011 (7:54 am)
argv[2] points to the return type parameter (U32) - is that what you want? Remember engine side arrays are zero-based, not one-based, so argv[2] points to the third argument.

Whoa - sorry - thread necromancy. I was looking for more info on rigidObjects....
#8
06/07/2011 (8:25 pm)
Quote:argv[2] points to the return type parameter (U32)
You're right that argv[2] points to the third argument, but it's not the third argument of the ConsoleMethod macro - it's the third argument passed to the function when called from script. This function, although it's only intended to take one 'argument' (%path), will actually take 3 (hence the 3, 3 for minargs, maxargs).

The first two arguments are automatic - the first is the name of the function (useful!), and I'm pretty sure the second is the object the function is being called on (in this case, the ID of a TriggerData object). Then after that, additional arguments from script are put into the array (in this case, argv[2] will contain a string representing whatever the scripts put in %path).

Note that minargs and maxargs can be different, and you can determine the actual number of arguments passed with argc. argv is always an array of length argc.