Game Development Community

Turning Script to C

by Richard_H · in Torque Game Engine · 01/10/2007 (2:20 am) · 6 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 (6:50 pm)
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 (7:18 pm)
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 (11: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 (11: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/11/2007 (12:04 am)
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/11/2007 (12:28 am)
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?