- Home /
How can I make my for loop "reset" if my array contains an value?
So having this loop below I have a simple problem... how can I make my loop to "try again" assigning gameObject to same place if an criteria is meet? (I've put return and break but i don't really know how to they work in general...)
if (PlayerShipLoaded == true && ShipsSelected < 7)
{
var EnemyAIshipPlaces = GameObject.FindGameObjectsWithTag("EnemyShipPlace");
for (place in EnemyAIshipPlaces)
{
RandomSelector = Random.Range (1,8);
RandomSelectorString = RandomSelector.ToString();
if (ArrayUtility.Contains (LoadedAIshipsBuiltin, RandomSelectorString)){
return;
}
else
{
Instantiate (RandSelectorModelEquivalent, place.transform.position, place.transform.rotation);
LoadedAIships.Add (RandomSelectorString);
ShipsSelected ++;
break;
}
}
}
Is your intention to allocate all of your ships in 1 of 8 random positions? As in there will be up to 8 ships and you want them to be in one of 8 different places after this code executes?
Allocate 8 ships randomly on 8 places without repeating them :)
Answer by Berenger · May 13, 2012 at 04:14 PM
Return will stop the function. Break will stop the loop. If you want to restart the loop, you need to use the iterator form of for : for( var i = 0; i < .... ) and reset i when you need to. That's a good way to have an infite loop though, so be carefull.
When you fond a solution, post it. It might help later generations.
Answer by Drakestar · May 13, 2012 at 04:22 PM
'Break' stops iteration over the loop. Any additional statements in the function block after this for loop are still executed. 'Return' ends the function call - no additional statements are executed, and the for loop iteration is automatically ended as a result. If there is no other code after the loop, both are accomplishing the same goal.
The exact behavior that you're trying to accomplish is a bit unclear to me. There's several variables in the code that aren't defined anywhere. Generally, you "try again" in a loop by either decrementing the loop counter, by breaking out and calling the same function again from the originating function, or by recursively calling the same function from within the loop.
What I want to do is to make this loop load for every found "EnemyShipPlace" a random gameObject which wasn't loaded earlier. btw. They are not defined because it is just part of my script as it is quiet big...
Answer by whydoidoit · May 13, 2012 at 07:04 PM
Firstly - I can't see your code clearly for creating the ships, so I've written this answer using what I can see - which is the positions you want to allocate. You could easily reverse the logic if you can create a list or array of the ships you want to allocate.
The principle is that you create a set of the unique items, choose a random one and then remove it from the set.
Ok to allocate your ships in a unique list of positions, make sure that you have imported Systems.Collections.Generic into your script.
Then
var allocationList = new List.<GameObject>( EnemyAIShipPlaces );
This gives you a set of the location GameObjects, in a list which you can remove them from one at a time in random order.
Then loop for each of the ships you want to allocate and do this:
Get a random position:
var positionIndex = Random.Range(0,allocationList.Count);
Then get the game object position:
var position = allocationList[positionIndex].transform.position;
And then remove the position selected from the list
allocationList.RemoveAt(positionIndex);
That will have the benefit that it always runs only once for each position.
Answer by Piflik · May 13, 2012 at 04:13 PM
I am not sure I understand what you want to do, but if you want to run the same loop again inside the loop, you should make it a function, so you can call it recursively.
Answer by zero3growlithe · May 14, 2012 at 11:58 AM
Ok, so I've decided to avoid "for" loop as I couldn't check what is happening in it as Unity freezed after I've added one thing and pressed play xD Here's my self answer that works perfectly as i needed :P
EDIT: Because of ArrayUtility to be an editor function I had to make my own one so here's updated version of code:
var AIshipsLoaded : boolean = false;
private var RandomSelector : float = 0;
var ShipsSelected : float = 0;
var EnemyShipPlace : float = 0;
var LoadedAIships = Array();
private var RandSelectorModelEquivalent : GameObject;
function Update(){
if (PlayerShipLoaded == true && ShipsSelected < 7){
var EnemyAIshipPlaces = GameObject.FindGameObjectsWithTag("EnemyShipPlace");
RandomSelector = Random.Range (0,7);
RandomSelectorString = RandomSelector.ToString();
// Enemy Ships Loader=================================================================================================================
if (AIshipsLoaded == false && PlayerShipLoaded == true){
switch (RandomSelector){
case(0):
RandSelectorModelEquivalent = GXF362_Enemy;
break;
case(1):
RandSelectorModelEquivalent = CDistra_Enemy;
break;
case(2):
RandSelectorModelEquivalent = CERN_Enemy;
break;
case(3):
RandSelectorModelEquivalent = Amphithere_Enemy;
break;
case(4):
RandSelectorModelEquivalent = Solaris_Enemy;
break;
case(5):
RandSelectorModelEquivalent = Zepher_Enemy;
break;
case(6):
RandSelectorModelEquivalent = Interceptor_Enemy;
break;
case(7):
RandSelectorModelEquivalent = Helios_Enemy;
break;
}
}
if (ArrayUtility() == false){
Instantiate (RandSelectorModelEquivalent, EnemyAIshipPlaces[EnemyShipPlace].transform.position, EnemyAIshipPlaces[EnemyShipPlace].transform.rotation);
LoadedAIships.Add (RandomSelectorString);
EnemyShipPlace ++;
ShipsSelected ++;
}
print (LoadedAIships);
}
}
function ArrayUtility (){
for (i = 0; i <= LoadedAIships.length - 1; i++){
var LoadedAIshipsBuiltin : String[] = LoadedAIships.ToBuiltin(String);
RandomSelectorString = RandomSelector.ToString();
if (LoadedAIshipsBuiltin[i] == RandomSelectorString){
return true;
}
}
}
Your answer
Follow this Question
Related Questions
Stopping A Loop - Return or Break? 2 Answers
Is having a "break" in a "foreach" loop that is itself inside a "for" loop breaking both loops? 3 Answers
C# List For Loop Only Returns Last Element 0 Answers
list.contains isn`t working 1 Answer
Is there a way to break out of a function and return control to another script's function? 1 Answer