- Home /
Compare indices in an array of Vector2s
Hello everyone! I am in need of some assistance.
I've been attempting to compare indexes inside a vector2 array for about a week now and just cant seem to figure it out. What I am trying to do is both sort an array of vector2's, and then compare one index to the next to check for duplicates, and then delete the duplicate, all in efforts to display all the tiles a character can move to with the given amount of movement points left.
Here is the code:
#pragma strict
var movesLeft: int;
var listOfMoveableTiles = new Vector2[100];
var startTile: Vector2;
function Start ()
{
FindAllMoves(movesLeft,startTile);
}
function Update ()
{
}
function FindAllMoves(movementLeft: int, tile: Vector2)
{
for (var i = 0; i < listOfMoveableTiles.Length; i++)
{
if(movementLeft > 0)
{
listOfMoveableTiles[i] = tile;
movementLeft-=1;
FindAllMoves(movementLeft, Vector2(tile.x, tile.y + 1) );
FindAllMoves(movementLeft, Vector2(tile.x, tile.y - 1) );
FindAllMoves(movementLeft, Vector2(tile.x + 1, tile.y) );
FindAllMoves(movementLeft, Vector2(tile.x - 1, tile.y) );
}
else
{
DeleteDupes();
}
}
}
function DeleteDupes()
{
//insert code to sort array
//Debug.LogError(tilePos);
/ var j: int = 1;
for(var i in listOfMoveableTiles)
{
if(listOfMoveableTiles[i] == listOfMoveableTiles[j])
{
//remove listOfMoveableTiles[j]
i++;
j++;
}
else
{
i++;
j++;
}
}
}
function GetMoveableTilesArray()
{
return listOfMoveableTiles ;
}
I started with javascript arrays but figured it would be much better to use the builtin arrays. been racking my head over it for a bit. Just wondering if anyone can point me in the right direction. Many thanks!!
What you are trying to do is a simili A* algorithm. You need to use a class of cell where the class contains a boolean that informs if it has been used already. I would think your function goes in an endless recursion since you are calling it inside and it will call itself again and again. Also, comparing object of class is easier since you can compare the reference ins$$anonymous$$d of the value (more accurate).
try with a simple
class cell{
var position:Vector2;
var visited : boolean;
}
When starting the search you set them all to false and start iterating checking first if it has been visited already and setting to true.
You may look there for more advanced info: http://unitygems.com/astar-1-journeys-start-single-step/
@Fattie
Damded english language!! :P
"There are no aportrophe's in plural's" does just look wrong
Lol quite right. Also the OP did not know the plural of "index."
Here's a cartoon on incorrect apostophes ... the last frame
Your answer
Follow this Question
Related Questions
Why Does Initialising Built-In Array of Classes Require Constructor? 1 Answer
How do I copy an Array class array into a builtin array? 1 Answer
"InvalidCastException" with MeshFilter from JS Array to builtin array. 2 Answers
Sorting builtin arrays 2 Answers
Is there a way syntactically to assign all the values of a builtin array in one command? 1 Answer