Post: Nested foreach loop bugged?
02-13-2016, 07:41 PM #1
BullyWiiPlaza
Climbing up the ladder
(adsbygoogle = window.adsbygoogle || []).push({}); So I wrote a code with a foreach loop that again contains another foreach loop but it's not working correctly. Why?

    getBuildablePiece(modelName)
{
self iPrintln("Size: " + level.zombie_include_buildables.size);
foreach(buildableStub in level.zombie_include_buildables)
{
self iPrintln("Stub name: " + buildableStub.name);
wait 1;
foreach(piece in buildableStub.buildablePieces)
{
self iPrintln("Model: " + piece.modelName);
if(piece.modelName == modelName)
{
self iPrintln("Found!");
return piece;
}

wait 0.3;
}
}

self iPrintln("Undefined");
return undefined;
}

The output is as follows:
    Size: 12
Stub name: riotshield_zm
Model: t6_wpn_zmb_shield_dolly
Model: t6_wpn_zmb_shield_door
GSC

Note how the size of
    level.zombie_include_buildables
is 12 but the loop only runs for the first element. Also what the fuck is this
     GSC
output? It should say
    Undefined
unless some corruption has occurred. Is the compiler in GSC Studio broken?

EDIT:
The following code works now.

    getBuildablePiece(pieceModelName)
{
keys = getArrayKeys(level.zombie_include_buildables);

foreach(key in keys)
{
buildableStruct = level.zombie_include_buildables[key];
buildablePieces = buildableStruct.buildablePieces;

for(piecesIndex = 0; piecesIndex < buildablePieces.size; piecesIndex++)
{
piece = buildablePieces[piecesIndex];

if(piece.modelName == pieceModelName)
{
return piece;
}
}
}

return undefined;
}
(adsbygoogle = window.adsbygoogle || []).push({});
02-13-2016, 08:13 PM #2
jwm614
NextGenUpdate Elite
Originally posted by BullyWiiPlaza View Post
So I wrote a code with a foreach loop that again contains another foreach loop but it's not working correctly. Why?

    getBuildablePiece(modelName)
{
self iPrintln("Size: " + level.zombie_include_buildables.size);
foreach(buildableStub in level.zombie_include_buildables)
{
self iPrintln("Stub name: " + buildableStub.name);
wait 1;
foreach(piece in buildableStub.buildablePieces)
{
self iPrintln("Model: " + piece.modelName);
if(piece.modelName == modelName)
{
self iPrintln("Found!");
return piece;
}

wait 0.3;
}
}

self iPrintln("Undefined");
return undefined;
}

The output is as follows:
    Size: 12
Stub name: riotshield_zm
Model: t6_wpn_zmb_shield_dolly
Model: t6_wpn_zmb_shield_door
GSC

Note how the size of
    level.zombie_include_buildables
is 12 but the loop only runs for the first element. Also what the fuck is this
     GSC
output? It should say
    Undefined
unless some corruption has occurred. Is the compiler in GSC Studio broken?

EDIT:
The following code works now.

    getBuildablePiece(pieceModelName)
{
keys = getArrayKeys(level.zombie_include_buildables);

foreach(key in keys)
{
buildableStruct = level.zombie_include_buildables[key];
buildablePieces = buildableStruct.buildablePieces;

for(piecesIndex = 0; piecesIndex < buildablePieces.size; piecesIndex++)
{
piece = buildablePieces[piecesIndex];

if(piece.modelName == pieceModelName)
{
return piece;
}
}
}

return undefined;
}


it should work

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo