Hey,
I have a bunch of variables within two arrays.
character_Wait_Time[0] upwards and enemy_Wait_Time[0] upwards. I’m trying to find which variable has the lowest value and assign it’s index number to say - var t
Another thing I’m trying to do is find out whether it was a character or an enemy that had the lowest value.
I can’t seem to figure out how to do this. Does anyone have any suggestions?
Thanks
Use BubbleSort to know the lower value in indices.
void BubbleSortAlgoritm()
{
for (int i = 0; i < character_Wait_Time.Lenght - 1; i++)
{
for (int j = i + 1; j < character_Wait_Time.Lenght; j++)
{
if (character_Wait_Time[i] > character_Wait_Time[j])
{
int aux = character_Wait_Time[i];
character_Wait_Time[i] = character_Wait_Time[j];
character_Wait_Time[j] = aux;
}
}
}
}
Then, the “character_Wait_Time[0]” will be the lower value in Array.
Sorting will find the lowest value, but it will also scramble the ordering of the array. You can search for the lowest value in an array with a function like this:-
function Lowest(f: float[]) {
var lowestVal: float = f[0];
var lowestIndex: int = 0;
for (i = 1; i < f.Length; i++) {
if (f[i] < lowestVal) {
lowestVal = f[i];
lowestIndex = i;
}
}
return lowestIndex;
}
Run this function on both arrays and then just compare the two values to see which is lower.
Thanks for the replies. I kinda already came up with my own solution
if ( num_Of_Enemies > 1 )
{
for ( var p = 0; p < num_Of_Enemies; p++){
if ( !enemy_Dead[p] )
{
o = enemy_Wait_Time[p];
}
}
for ( var j = 0; j < num_Of_Enemies; j++ ){
if ( enemy_Wait_Time[j] < o !enemy_Dead)
{
o = enemy_Wait_Time[j];
}
}
for ( var k = 0; k < num_Of_Enemies; k++ ){
if ( enemy_Wait_Time[k] == o )
{
enemy_To_Process = k;
break;
}
}
}
This compares the enemy wait times against each other and assigns the index of the lowest value to enemy_To_Process.
I’ll look into your two examples to see if I can improve my script. 