- Home /
Is there a more elegant way to get different values (types) from a function
Hi,
I'm scripting some kind procedural level builder acting like a worm going through a 3Darray of cells. In some parts of the code, I just want to check how many cells surrounding the worm's actual position have a certain status (eg. empty or filled), in other ones I'll want to have a precise status of each cells in a certain order.
As Javascript doesn't allow for multiple refs/out, I'm going like this : to check how many cells are empty I call a function sending a Vector3 of the worm's current position and waiting for an int in return:
var zeroes : int = CheckNeighbors(currentWormPos);
To check the status of each cell counterclockwise I call the same function but this time sending the Vector3 as three ints and waiting for a string in return:
var neighbours : String = CheckNeighbors(currentWormPos.x, currentWormPos.x, currentWormPos.x);
And so here's my called function and it's overloaded sister : function CheckNeighbors(coord:Vector3){ var up : int = my3DArray[coord.x,coord.y,coord.z+1]; var left : int = my3DArray[coord.x-1,coord.y,coord.z]; var down : int = my3DArray[coord.x,coord.y,coord.z-1]; var right : int = my3DArray[coord.x+1,coord.y,coord.z];
var zeroes : int = 0;
if (up == 0){ zeroes++; }
if (left == 0){ zeroes++; }
if (down == 0){ zeroes++; }
if (right == 0){ zeroes++; }
return zeroes;
}
function CheckNeighbors(x:int,y:int,z:int){ var up : int = my3DArray[x,y,z+1]; var left : int = my3DArray[x-1,y,z]; var down : int = my3DArray[x,y,z-1]; var right : int = my3DArray[x+1,y,z];
var neighborhood : String = up.ToString()+left.ToString()+down.ToString()+right.ToString();
return neighborhood;
}
As I assume it'll work as is, I just wanted to know if there's any way more elegant to achieve this.