Clean-up on aisle code
It's more succinct than it was, but this code is still a nightmare to read and update. Let's try to refactor it a little more.
There's a lot of GetComponent.<Square>().player
happening throughout the line. What if we made one function that would do that job, just to shorten up our code and make it a little more readable?
By setting up a function that accepts x
and y
as arguments, and spits out the value of a square's player variable, we'll have a handy and reusable piece of code for all kinds of situations. You should start writing the code into your project again.
function GetPlayer(x:int, y:int):int { return aGrid[x,y].GetComponent.<Square>().player; }
What just happened – vending machine
Just like our vending machine analogy from an earlier chapter, the GetPlayer
function accepts two integers called x
and y
as inputs (these are like quarters and nickels). It uses these values to look up a Square in the grid and, using the return
keyword, the function...