Hi there,
I have a problem with a “for() { for() { } }” statement.
It seems that once I run thru the second for () cycle, and reset back to the first for(), once I go back into the second for() the var i : int, is not reset, obviously, throwing off the second array range. So, how do I reset it?
function Start()
for(var ca : int; ca < pathCamITween.length; ca++)
{
var pathCamData : iTweenPath;
pathCamData = pathCamITween[ca].GetComponent(iTweenPath);
for(var ci : int; ci < pathCamData.nodeCount; ci ++)
{
pathCamNodeTemp = pathCamData.nodes[ci];
setCamNode();
}
}
}
You are incrementing nodeCamCounter to a value of one before setting pathCam for the first time so element zero will never be set.
Try changing
function setCamNode()
{
nodeCamCounter++;
print("nodeCamCounter = " + nodeCamCounter);
print("pathCam[] " + pathCam[nodeCamCounter]);
pathCam[nodeCamCounter] = pathCamNodeTemp;
}
function setCamNode()
{
print("nodeCamCounter = " + nodeCamCounter);
print("pathCam[] " + pathCam[nodeCamCounter]);
pathCam[nodeCamCounter] = pathCamNodeTemp;
nodeCamCounter++;
}
Thanks you are right.
However, I am getting an index out of range at array[0] for this…
function Start()
{
////retrieve all node positions from PAPath
for(var i : int; i < pathBrainITween.length; i++)
{
var pathBrainData : iTweenPath;
pathBrainData = pathBrainITween[i].GetComponent(iTweenPath);
var pathBrainNodeCount : int = pathBrainData.nodeCount;
for(var ib : int = 0; ib < pathBrainNodeCount; ib++)
{
pathNodeTemp = pathBrainData.nodes[ib];
setBrainNode();
if(pathBrainNodeCount == ib)
{
ib = 0;
}
}
}
}
function setBrainNode()
{
pathBrain[nodeCounter] = pathNodeTemp;
nodeCounter++;
}
@ Line 25, I am told the index is out of range when nodeCounter == 0. PathBrain:Vector3[ ] is set pre-hand correctly to accept the correct size. Completely unsure as I am using an identical procedure for a separate path which works.
??
Without the line number where the error is happening I can only guess which of the three arrays are causing it. If I had to guess I’d say you are hitting the error on line 35 with the pathBrain array since there is no error handling to ensure that nodeCounter is within the bounds of the array.
Also
if(pathBrainNodeCount == ib)
{
print("pathBrainNodeRedux == ib. Reset ib");
ib = 0;
}
is useless since the containing for loop will terminate at ib < pathBrainNodeCount and if for some reason it ever did execute it would lock up your program since it would reset your counter thus putting it into an infinite loop.
Edit:
Well seems I guessed correctly but you changed you original code so now line 35 is line 25 and since you are getting an index out of range with nodeCounter at zero that means you are probably missing the array initializer for pathBrain. So somewhere in your code before you get to this point there needs to be a pathBrain = new Array([sizeOfArray]). You should also ensure that nodeCounter < pathBrain.Length before trying to set it.
Solved it.
Thanks.
That code you mentioned is necessary since I want ib to be running. I do not want it to reset on the second entrance to the main for(). But thanks!
I’m not a Javascript guy but doesn’t the way you’ve defined your inner loop for(var ib : int = 0; ib < pathBrainNodeCount; ib++) mean that ib will automatically reinitialized to zero for every iteration of the outer loop and that the loop as defined will terminate at ib == pathBrainNodeCount - 1 so your if (ib == pathBrainNodeCount) will never evaluate as true?
Yah maybe you re right now that I look at it. I must have placed that in there while I kept getting out of range index’s.