- Home /
What are the built-in array syntax rules in this example...
I'm trying to understand the individual pieces of a tetris clone I found (to the original maker, thank you! I can't find the original anymore...) I'm trying to understand the individual parts so I can recreate something similar. I've removed various parts. What's left works together with a different script which is attached to an empty. If I press spacebar in that script, it instantiates another empty, which has this script attached.
This script is supposed to gather a number of cube shaped primitives (which it successfully does), in the shape that the block string array designates, trough a table of 1s and 0s in the inspector. As a tetrimino it's like this: oooo 1111 oooo oooo
What I don't understand, as I seem unable to find documentation for the syntax, is how, in the "if" statement", the information in the parenthesis is interpreted. How do the temporary variables x and y, correspond to the grid of 1s and 0s in the built-in array in the inspector? What are the syntax rules for making built-in arrays in the code example?
If I'm missing some vital information, please let me know :)
And just to make sure I'm clear: I didn't make this script, I'm just trying to understand it. Not trying to steal or take credit from anyone.
var block : String [];
private var size : int;
private var halfSize : int;
private var halfSizeFloat : float;
function Start () {
// Sanity checking.
size = block.Length;
halfSize = size/2;
halfSizeFloat = size*.5; // halfSize is an integer for the array, but we need a float for positioning the on-screen cubes (for odd sizes)
// Convert block string array from the inspector to a boolean 2D array for easier usage
for (y = 0; y < size; y++){
for (x = 0; x < size; x++){
if (block[y][x] == "1"){
var block = Instantiate(Button.use.cube, Vector3(x-halfSizeFloat, (size-y)+halfSizeFloat-size, 0.0), Quaternion.identity) as Transform;
block.parent = transform;
}
}
}
}
Answer by flaviusxvii · Feb 11, 2013 at 05:47 PM
block is an array of strings. Arrays and strings are both indexable with the square bracket syntax.
The expression
block[y][x]
Will give you the xth letter of the yth string..
That went a little above my head. Y and x seem to be just temporary variables, which are undefined in relation to block, so how do they relate to the elements in my array?
Sorry, I'm such a pain.
over the course of the nested loops, y and x will have the values (0, 0), (0, 1), (0, 2), ...(0, size-1), (1, 0), (1, 1), (1, 2)... and so on.
If block was 3 strings, ["Apple", "$$anonymous$$oney", "Larch"], at the point of the loop when when y = 1 and x = 3, then block[1][3] would be 'e'. Arrays offset at zero, so it's the second string and the fourth character.
Your answer
Follow this Question
Related Questions
My custom function for Updating a data string in an array causes deletion of all other data 0 Answers
Array of Array 1 Answer
Removeat with Strin[] 2 Answers
Issues with string array taken from a .txt file 2 Answers
ios cant access variables in array 1 Answer