- Home /
Int List won't let me add an Int Variable
Quick summary:
I am randomizing a variety of variables, and then storing the ID of these values in an Int List in the script as they are generated. It generates 10 in a row, and checks against the Int List to see if the ID has been taken already. If the Int List contains the ID already, then a new ID is generated, checks against the Int List, and continues. When the ID is not found inside the Int List it .Add()'s to the Int List and continues generating other information.
I am having a problem with the .Add(). For some really confusing reason, every .Add() I call works perfectly except when the Int has been randomized more than once. If this is confusing, here is my code:
//KEEP A LOG OF ALL DATABASE ENTRIES USED, TO NOT ALLOW DUPLICATES
private var nameLog: List.<int> = new List.<int>();
//TESTING VALUES
private var tripleRandom: int = -1;
function Start() {
for (var i=0;i<10;i++) {
generateInfo(); //GENERATE 10
};
}
function reRandomizeFeature(totalAmount:int) {
var rerandomize: int = Random.Range(0, totalAmount); //RANDOMIZE A NEW NUMBER
Debug.Log('RANDOMIZED A '+rerandomize);
if (nameLog.Contains(rerandomize)) { //IF THE LIST CONTAINS IT, RE-RANDOMIZE IT AGAIN
Debug.Log('Contains '+rerandomize+'. Rerandomize it again!');
reRandomizeFeature(totalAmount);
} else { //IF IT DOESN'T, ALLOW IT THROUGH
Debug.Log('Randomized '+rerandomize+' and nameLog does not contain it. Allow it to go through!');
tripleRandom = rerandomize;
}
}
function generateInfo() {
var char_nameDBLength: int = 15; //15 TOTAL
var nameSelectedByRandom: int = Random.Range(0, char_nameDBLength); //GENERATE THE FIRST ATTEMPT
Debug.Log('------------------------------');
if (nameLog.Contains(nameSelectedByRandom)) { //IF LIST CONTAINS THE RANDOMED ID
Debug.Log('Contains '+nameSelectedByRandom+'. Randomizing a new ID.');
reRandomizeFeature(char_nameDBLength); //RANDOMIZE A NEW ID
} else {
Debug.Log('Does not contain '+nameSelectedByRandom+'. No need to randomize');
}
Debug.Log('Saving ID '+nameSelectedByRandom+' ('+tripleRandom+') to the NameLog.');
if (tripleRandom <= -1) { //IF THE ID DID NOT HAVE TO BE RANDOMIZED AGAIN
nameLog.Add(nameSelectedByRandom); //ADD TO THE LIST
Debug.Log('Do not add Triple Random, add: '+nameSelectedByRandom);
} else { //IF THE ID HAD TO BE RANDOMIZED (AND THE TEMP VAR IS ABOVE -1), ADD IT TO THE LIST
Debug.Log('Add Triple random: '+tripleRandom);
//THIS IS WHERE I GET THE PROBLEMS:
nameLog.Add(tripleRandom); // IF THIS LINE IS HERE, UNITY COMPLETELY FREEZES AND I HAVE TO CLOSE AND REOPEN.
//IF THIS LINE IS COMMENTED OUT, THE CODE WORKS BUT NOTHING IS ADDED TO THE INT LIST.
}
Debug.Log(nameLog[nameLog.Count-1]); //PRINT THIS ID
tripleRandom = -1; //RESET THE TEMP ID BACK TO -1 TO BE RESET
}
The commented line is where the problem arises. I have been fighting to try and figure this out for hours now, and I am just hung up on this tiny little line of code. The annoying thing is if I comment out the nameLog.Add(tripleRandom), and add in "nameLog.Add(5)" or anything like that, the code works perfectly fine. It seems it's the damn variable that is the problem, but I don't understand what's wrong.
Any help would be amazing, thank you.
Doesn't seem to be a problem there. Works fine at my end.
Well that's weird... For me it freezes Unity and forces me to Force Quit and reopen. I mean it works perfectly without the nameLog.Add(tripleRandom) line, but as soon as I add that it freezes. Very aggravating...
Answer by fafase · Jun 03, 2014 at 06:23 AM
If I get it right, you want a list of values that are unique and in your process you generate a value and if that value already exists, you go for creating a new one.
Your problem lies in your recursive process that you do not control the iteration. This is common problem that you either need to define a sentinel that will tell after x amount of iteration, the process stops but you may end up with no result.
See, you can get 15 values and you want to get 10 out of that, if on the five first your get 7,10,5,6,3 for instance, you already have 33% chances to go on a endless loop and this value will increase every time you manage to find a non existing one. For the last value you have a 60%(9/15) chance to get screwed on each trial to find a new value.
Now for a solution:
Store all possible values in a list
Get a random value between 0 and list.Count (list.Count will be excluded)
Get the value from the list at the index of value got in step2
Put it in your valueList and remove it from list
Repeat process until you have 10 numbers
Since you remove the chosen value when you get it, you won't get it again.
I had a feeling it was the way I was looping, even when it did work it was very slow to start. This method is much quicker and (Obviously) not prone to any infinite loops. Thank you so much!