Pages

Sunday, October 17, 2010

Unity.IndexOf(indexOf);... Looking for "indexOf" within Unity's array of functions...

I was currently building a game in Unity, sort of a puzzle-adventure game, which means there will be a lot of array related stuff all through the scripting. It was a huge headache for just making the puzzle itself, and I was following a puzzle making tutorial at the same time, which was in Flash's AS3. So I stumble across this useful but impossible to found on Unity script reference, function called "indexOf", which sole purpose was to search for a number or string variable within an array. The way of using it in Flash was:
var arr : Array = [1,2,3];
arr.indexOf(1);

Clueless, depress, and desperate (for finishing the game prototype, as I promise my composer I would show him that in 2 weeks time... now's already entering the third) I look for my own solution by using tons of FOR statement to re-create the IndexOf function. Not exactly tons of them, but just two lines:
function IndexOf(arr, posX, posZ) {
  for (var i : int = 0; i
  for (var j : int = 0; j
  if (arr[i][j] == allTiles[posX][posZ]) {
  return 0;
  }
  else {
  return -1;
  }
    }
  }
}


Two lines of FOR statement was used because my array here is a 2 dimensional array (array within an array, which explains arr[i][j]). In some way, the function above still have a lot of mistakes and incomplete, not enough to satisfy me. I was like, this was as far as I (my programming skill) could go... 

I was planning to look for a better solution on the Unity forum but my idiotic internet service provider has left me internet-less for about 5 months now.

Thanks to fact that most programmers spend most of their time in front of the PC, as soon as I got my chance to get online and seek for help on the Unity forum, I got my reply from them and found this: 
System.Array.IndexOf(Array, Integer/String);

This goes without saying that... I really should start learning C++~~~

No comments: