So the pieces are finally coming together for me with regards into how to get a city builder going and I’m currently poking through the set of tutorials I posted up on another thread. However I have questions about various things like how to do stuff more efficiently and I think I’ve managed to screw up just following this basic tutorial, it must be that I’ve put a misplaced comma somewhere because it’s all brand new code and I was just getting it into my head.
using UnityEngine;
using System.Collections;
public class TileMap : MonoBehaviour {
public TileType[] tileTypes;
int[,] tiles;
int mapSizeX = 10;
int mapSizeY = 10;
void Start(){
tiles = new int [mapSizeX, mapSizeY];
// This code places the walkable tiles
for (int x = 0; x < mapSizeX; x++) {
for (int y = 0; y < mapSizeX; y++) {
tiles [x, y] = 0;
}
}
// This places the slowed tiles
tiles [4, 4] = 1;
tiles [5, 4] = 1;
tiles [6, 4] = 1;
tiles [7, 4] = 1;
tiles [8, 4] = 1;
tiles [8, 5] = 1;
tiles [8, 6] = 1;
tiles [8, 5] = 1;
tiles [8, 6] = 1;
GenerateMapVisual ();
}
void GenerateMapVisual ();{
for (int x = 0; x < mapSizeX; x++) {
for (int y = 0; y < mapSizeX; y++) {
TileType tt = tileTypes [tiles [x, y]];
Instantiate ( tt.tileVisualPrefab, new Vector3 (x, y, 0), Quaternion.identity);
}
}
}
}
Arrays by themselves seem simple enough, they’re just places where you store lots and lots of content, in the example of a tiled grid like in the tutorial I’m following you’re placing all of the tiles at one location across a set of coordinates. What I’m curious about is the guy on the tutorial mentioned that with grids quite often games developers delete everything ones it goes off screen and then reloads it when the camera comes back. To me though that seems incredibly innefficient and not something that you’d perhaps do with modern computers? Since they’d usually be able to load things up unless the maps is ridiculously detailed like the total war games made the mistakes made.
What other techniques would you use to keep things efficient? To me the simplest thing even now I’m learning programming would be to keep the map as basic as possible so as to use less resources. Especially if you’re going turn based because what I always find is the developers load up so much detail without even thinking about it that they end up making turns last ridiculously long ( Oh god Rome 2 ;_; ) and I want to make sure to avoid that especially if you have a hybrid mix of real time and turn based gaming anyway you can focus on making the real time stuff nice and detailed.
If you could have a look at my code as well I’d be grateful, like I said, I think it’s just I’m new to arrays and don’t know where everything is supposed to go. I’ve also been experimenting with raycasting and will move on to instantiate, I get what each one of them is about, but it’s just a matter of making sure I get the right code and know how to combine everything.
For those who don’t know, here’s the tutorial I found.