Game Development Community

Problem with scope of simset

by Isaac Barbosa · in Torque Game Builder · 07/30/2007 (8:54 am) · 8 replies

Hi,

I have this code working almost as I wish, but sometimes my game crashes.

When my level is loaded I check if player has gems available, then I grant those gems. I believe the problem is in the first line of this code. If I use a number, like 20 for example, there is no a crash, but I'm forced to use a dynamic variable like $spaceforgem because every level has a different number of spaces avalaible for gems. The game doesn't crashes always but I think that it does when random $= 0. I don't know.

But I get this line (several lines) in the console report when crash:

Set::getObject index out of range.

%blueReward = getRandom($spaceforgem); ---I believe the problem is here in the getRandom function
%shape3 = $Shape3Objects.getObject(%blueReward);
if(%shape3.availableForBonusTile $= true && %shape3.isOnline $= false)
{
%shape3.availableForBonusTile = false;
%shape3.jewel = "gold";
$doubleScore--;
$CurrentDoubleScore++;
%color = %shape3.shapeColor;
%shape3.playAnimation("goldJewel" @ %color);
}
else if(%shape3.availableForBonusTile $= false && %shape3.isOnline $= false)
{
%this.giveBlueBonus();
}

I've tried this code below %blueReward = getRandom($spaceforgem);, but the problem is persistent

if(%blueReward <= 0)
{
%blueReward = getRandom(20+1);
}

somebody has an idea of what could be wrong here?

Thanks

#1
07/30/2007 (9:22 am)
You should try echo'ing out the $spaceforgem and %blueReward values before you call getObject so in the console log when it crashes you can see whats causing (or possibly causing) the problem?

Also place multiple echo's with different text in that script before any sort of function call taht might be crashing it so you can check the console to see where exactly it does crash...
#2
07/30/2007 (9:45 am)
What matt said.

also,
when i'm suspicious of the output of a random(), i'll often print the results of 10,000 calls to the random(), then sort them in excel or open office, and make sure the data is what i expect.
#3
07/30/2007 (10:38 am)
Like others have said, check the value that is generated by echoing it out, and then make sure that you have the same number of objects in your set as you think you do. If, for example, the value of $spaceForGem is 22, but you only have 11 objects in the set $Shape3Objects, then you are going to get that error very often.

All that being said, nothing in your code should be crashing if %shape == 0. At worst, you should be getting a series of "unable to find object XXX" lines, and the code not being run. It's very possible that some of the global variables you modify are being used elsewhere that is actually causing the crash.

I've noted that several times you are using the $= comparison operator, and then not actually comparing strings :

if(%shape3.availableForBonusTile $= true && %shape3.isOnline $= false)

Neither the token 'true' or the token 'false' are strings, and you should probably be using the == operator here for safety, or wrap your tokens with double quotes to force them to be treated as strings.
#4
07/30/2007 (11:36 am)
@Thanks stephen for those lines, I will try == instead of $=.

@Matthew and Orion.

Everythinh in the code, I mean variables are working ok and nothing is wrong. I have the same number of objects as $spaceforgem. The trick here is that if I place 40 objects in my first level and $spaceforgem is 40 and I call a getRandom(40) intead of getRandom($spaceforgem) everything is working ok.

I will try to do as you say anyway to see if I can get the error.

Thanks!

And maybe the problem is not here, but after two whole days trying to fix this, this is the only problem I have in mind :(
#5
07/30/2007 (12:18 pm)
Just a follow up on my comment on the $= vs ==. Ultimately, it doesn't matter which you use, as long as you stay consistent.

If somewhere in your code you have something like:

%shape3.availableForBonusTile = "true";

then you should use $= "true" when comparing, but if you have:

%shape3.availableForBonusTile = true;

then you should use == true; when comparing.

Ultimately, it's probably not your root issue here, but "crossing the streams" may lead to hard to troubleshoot errors that are unexpected, and it's good programming practice to make sure you are consistent.
#6
08/02/2007 (7:41 am)
@Stephen,

I have checked all my code and it is consistent with the use of true. I'm not using "true".

So, I don't know why is this problem happening. I will try to fix it, but if can't, I will simply forget that feature in my game.

Thanks
#7
08/02/2007 (9:15 am)
Then make sure you are using == and not $=, as you do in your example above. $= is for "true", == is for true;
#8
08/02/2007 (10:55 am)
Thanks Stephen,

that did the trick. I don't know why because I'm using only true instead of "true" and in other functions $= is working perfectly. But if this will be a problem eventually, I will make some changes to prevent a malfunction of the game :)