Strange problem using "If"
by Marge Van Der Straaten · in Torque Game Builder · 01/30/2012 (11:40 pm) · 2 replies
Hey,
I've been encountering a strange problem with a function when using "if".
A bit of context: I'm using this to move objectA to another object in the level(that's stored in "locationSet" SimSet). Everything works fine, except the objectA will only move to the first object in the locationSet SimSet and then the loop breaks:
The problem is this line:
I've echoed the "locationSet.getObject(%locationIndex).ID" and "%area@%i" and both values are "North1" and "South2" respectively, but the game assumes both values are equal. I'm sure it's user error on my part, so can anyone help point out the mistake?
Many thanks!
I've been encountering a strange problem with a function when using "if".
A bit of context: I'm using this to move objectA to another object in the level(that's stored in "locationSet" SimSet). Everything works fine, except the objectA will only move to the first object in the locationSet SimSet and then the loop breaks:
function addStuff(%area, %item)
{
switch$(%item)
{
case "Bear":
for(%i = 1; %i <= 2; %i++)
{
//scan all locations to find specific location
for(%locationIndex = 0; %locationIndex < locationSet.getCount(); %locationIndex++)
{
if(locationSet.getObject(%locationIndex).ID == %area@%i)
{
%that.moveTo(locationSet.getObject(%locationIndex).Position, 20, true, true);
break;
}
else
{
echo("Could not find the location");
}
}
//... rest of codeThe problem is this line:
if(locationSet.getObject(%locationIndex).ID == %area@%i)
I've echoed the "locationSet.getObject(%locationIndex).ID" and "%area@%i" and both values are "North1" and "South2" respectively, but the game assumes both values are equal. I'm sure it's user error on my part, so can anyone help point out the mistake?
Many thanks!
About the author
Associate Charlie Patterson
Default Studio Name
compare strings with $= instead of ==. The latter which is for numbers.
If you compare two strings with ==, it will convert them to numbers first as best as it can. And most strings, unless they actually *are* a number, will convert to the number 0. So "North1" == "South2" turns into 0 == 0 which is true!