Infinite Loop?
by Richard_H · in Torque Game Engine · 01/15/2007 (12:52 pm) · 7 replies
I have some code here that basicly measures the distance between a trigger, and the next in the current "path".
For some reason it seems to be getting stuck in an inifinite loop. I'm not getting any output, so it seems my printf isn't being called either.
For some reason it seems to be getting stuck in an inifinite loop. I'm not getting any output, so it seems my printf isn't being called either.
ConsoleMethod( Trigger, distToNext, S32, 3, 3, "(%path)")
{
S32 dist = 0;
const char *fieldName = StringTable->insert( argv[2] );
SimObject* nextObj;
S32 cDist = 1;
S32 ID;
S32 nID;
Trigger* lastTrigger = object;
while(cDist > 0)
{
cDist = -1;
nextObj = Sim::findObject(dAtoi(lastTrigger->getDataField( fieldName, NULL)));
ID = lastTrigger->getId();
nID = nextObj->getId();
Con::printf("%d.%s: %d", ID, fieldName, nID);
const char* nextCName = nextObj->getClassName();
if(nextCName == "Trigger")
{
Trigger* nextTrigger = static_cast<Trigger *>( nextObj );
VectorF nextPos = nextTrigger->getBoxCenter();
VectorF thisPos = lastTrigger->getBoxCenter();
VectorF v = thisPos - nextPos;
cDist = mSqrt((v.x * v.x) + (v.y * v.y) + (v.z * v.z));
}
else
{
Con::printf("Class name isn't Trigger it's: %s", nextCName);
break;
}
dist =+ cDist;
lastTrigger = static_cast<Trigger *>( nextObj );
}
Con::printf("The distance is %d", dist);
return dist;
}About the author
#2
01/15/2007 (1:47 pm)
Well, when it finds a non-trigger it never changes cDist from -1, so the loop continues until it gets a non-trigger. But I think that when it gets to the last trigger, it gets null as nextObj, so it gets messed up at getClassName.
#3
In any case, I suspect you may be accidentally doing something silly, like checking the same object over and over again.
01/15/2007 (2:08 pm)
Stepping through the code with a debugger would pretty much reveal the problem in a matter of moments.In any case, I suspect you may be accidentally doing something silly, like checking the same object over and over again.
#4
01/15/2007 (5:23 pm)
Never mind, I discovered that I was eventualy nextObj was null so getClassName was causing problems.
#5
couple things -
first, in C++ you can't compare two simple strings with the "==" operator.
as you can see, nextCName is of type "const char *" - the "*" means it's a pointer to a memory location.
"==" is simply going to compare that point (aka memory location) to the memory location of the fixed string "Trigger" - and they'll never be equal.
to compare strings in TGE, use dStricmp(). - check google for the usage of stricmp().
second,
if you want to check the class of an object, there's a much better way to do it than string-comparing on GetClassName(). - it's dynamic_cast.
rewrite that part of the code to this:
C++ is unfortunately a bit tricky.
if you've got a friend who can be imposed upon to proof-read your code, you'll probably save a lot of time and frustration. (that or a decent C++ book or course)
01/15/2007 (7:50 pm)
Hey richard -couple things -
first, in C++ you can't compare two simple strings with the "==" operator.
as you can see, nextCName is of type "const char *" - the "*" means it's a pointer to a memory location.
"==" is simply going to compare that point (aka memory location) to the memory location of the fixed string "Trigger" - and they'll never be equal.
to compare strings in TGE, use dStricmp(). - check google for the usage of stricmp().
second,
if you want to check the class of an object, there's a much better way to do it than string-comparing on GetClassName(). - it's dynamic_cast.
rewrite that part of the code to this:
Trigger* nextTrigger;
nextTrigger = dynamic_cast <Trigger*> (nextObj);
if (nextTrigger != NULL)
{
VectorF nextPos = nextTrigger->getBoxCenter();
// etc etc
}
else
{
// etc etc
}C++ is unfortunately a bit tricky.
if you've got a friend who can be imposed upon to proof-read your code, you'll probably save a lot of time and frustration. (that or a decent C++ book or course)
#6
01/15/2007 (8:20 pm)
Minor correction: dStrcmp, without the i.
#7
richard - "dStrcmp()" compares strings case-sensitively, while "dStricmp()" does it insensitively.
01/15/2007 (8:47 pm)
Hey stefan - it's a small point, but i recommend dStricmp with the i because the vast majority of strings in TGE are handled case-insensitively, and consistency is good. - is there a reason not to use the "i" in this case ?richard - "dStrcmp()" compares strings case-sensitively, while "dStricmp()" does it insensitively.
Torque Owner Stefan Lundmark