- Home /
Better code for initiating gameobjects in order
Hi All,
I'm new to Unity en programming and I'm trying to program my first Game. The most important thing for me is learn about how everything works. I'm trying to make a match-3 game, and found some nice tutorials online. My game is working fine but i'm still have some questions about the code en the methods used. Hope you guys can help me out.
To save some memory the game reuses the objects instead of destroying them. In the start we initialise a bank of object and put them in a list tileBank. After we have created the list we shuffle it and place the tiles on the screen using following code:
for (int r = 0; r < rows;r++){
for (int c = 0;c<cols;c++){
Vector3 tilePos = new Vector3 (c, r, 0);
for (int n = 0; n < tileBank.Count; n++) {
GameObject o = tileBank [n];
if (!o.activeSelf) {
o.transform.position = new Vector3 (tilePos.x, tilePos.y, tilePos.z);
o.SetActive (true);
tiles [c, r] = new Tile (o, o.name);
n = tileBank.Count + 1;
}
}
}
}
}
Can I delete the creation of the tilePos Vector en directly code it with the transformation of the tile like this:
o.transform.position = new Vector3 (c,r,0);
If I understand the code correctly the placement of tiles start everytime with the first tile in the bank and if it's not used check the next. If it's not used, use it en go to the next tile.
I would have coded it like this
for (int r = 0; r < rows;r++){
for (int c = 0;c<cols;c++){
GameObject o = tileBank [r*c+c];
o.transform.position = new Vector3 (c, r, 0);
o.SetActive (true);
tiles [c, r] = new Tile (o, o.name);
}}
My code seem to do the same job. But me being a novice programmer I ask myself. Why is the more complex code better?
The complex code has some really redundant things written inside. One of those things - putting the vector coordinates inside of a vector. That's really weird.
Vector3 tilePos = new Vector3 (c, r, 0);
o.transform.position = new Vector3 (tilePos.x, tilePos.y, tilePos.z);
is exactly the same thing as:
Vector3 tilePos = new Vector3 (c, r, 0);
o.transform.position = tilePos ;
or even
o.transform.position = new Vector3 (c, r, 0);
It's redundant and unneccecary. I understand why you'd want to place it inside of a variable, for example: if after getting the position, you would want to manipulate it a little, before assigning the position to an object? But used in this context - uneccessary.
The loop that goes through the tileBank is a little weird as well and the way to "optimize" it, would be to find the index of the tile like this col + row * rowWidth
. You wouldn't get a tile that is already active this way, so the loop is redundant and can be omitted.
n = tileBank.Count + 1;
this line is used to get out of that inner loop, but you can use break
as it only stops the loop that you are currently on and would proceed with the loops that are 'higher up'. So yeah, the way you wrote the code is short and to the point, where the more complex code has some redundency.
Thanks for the quick reply and the clear explanation
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OnCollisionEnter not working 1 Answer
Alarm lights not changing 1 Answer