- Home /
RTS Grid Initialisation
Hello. I am attempting to create a RTS game for my final year project at university.
I have managed to implement the majority of the game, but I have been using a lackluster pathfinding algorithm (go straight to the destination, no actual algorithm).
I have been attempting to ammend this and after speaking with my prof, he told me that I should be using an Array system to initialise the grid points. I've been at this for some time but I really don't understand how this should work.
The two main reasons behind this change are to reduce load times (they are pretty hefty) and to appropriately implement a pathfinding algorithm.
I do understand how an array will work, but I don't understand how it would have the various triggers I would need.
Currently, I have this as my implementation:
void Start ()
{
//Initialise Grid Points
//-----------------------
for (int i=1; i <= MapDimension; i++)
for (int j=1; j <=MapDimension; j++)
Instantiate(gridPoint, new Vector3(transform.position.x+i*5, transform.position.y, transform.position.z+j*5), Quaternion.identity);
foreach (GameObject Grid in GameObject.FindGameObjectsWithTag("Grid"))
{
GridScript Script = Grid.GetComponent<GridScript>();
Script.landtype = "forest";
// Resource Generation
//---------------------
// Mountains
if (Random.Range(0,1000)<1)
{
float scale = 10;
Instantiate(mountain, new Vector3(Grid.transform.position.x+25, -13, Grid.transform.position.z), Quaternion.identity);
mountain.transform.localScale = new Vector3 (scale,scale,scale);
}
foreach(GameObject Mountain in GameObject.FindGameObjectsWithTag("mountain"))
{
if (Grid.transform.position.x > Mountain.transform.position.x - 35 && Grid.transform.position.x < Mountain.transform.position.x + 25 && Grid.transform.position.z > Mountain.transform.position.z - 25 && Grid.transform.position.z < Mountain.transform.position.z + 25)
{
if (Grid.transform.position.x != Mountain.transform.position.x -25)
{
if ((Grid.transform.position.x == Mountain.transform.position.x +20 && Grid.transform.position.z == Mountain.transform.position.z-20)||(Grid.transform.position.x == Mountain.transform.position.x +20 && Grid.transform.position.z == Mountain.transform.position.z-15)||(Grid.transform.position.x == Mountain.transform.position.x +15 && Grid.transform.position.z == Mountain.transform.position.z-20)||
(Grid.transform.position.x == Mountain.transform.position.x -20 && Grid.transform.position.z == Mountain.transform.position.z-20)||(Grid.transform.position.x == Mountain.transform.position.x -15 && Grid.transform.position.z == Mountain.transform.position.z-20)||(Grid.transform.position.x == Mountain.transform.position.x -20 && Grid.transform.position.z == Mountain.transform.position.z-15)||
(Grid.transform.position.x == Mountain.transform.position.x +20 && Grid.transform.position.z == Mountain.transform.position.z+20)||(Grid.transform.position.x == Mountain.transform.position.x +20 && Grid.transform.position.z == Mountain.transform.position.z+15)||(Grid.transform.position.x == Mountain.transform.position.x +15 && Grid.transform.position.z == Mountain.transform.position.z+20)||
(Grid.transform.position.x == Mountain.transform.position.x -20 && Grid.transform.position.z == Mountain.transform.position.z+20)||(Grid.transform.position.x == Mountain.transform.position.x -20 && Grid.transform.position.z == Mountain.transform.position.z+15)||(Grid.transform.position.x == Mountain.transform.position.x -15 && Grid.transform.position.z == Mountain.transform.position.z+20)
){}
else
{
Script.active = false;
Script.landtype = "mountain";
}
}
}
}
// Trees
if (Random.Range(0,100) <50)
for (int i=1; i< Random.Range(0,3); i++)
if (Script.landtype == "forest")
{
GameObject t = (GameObject)Instantiate(tree, new Vector3(Grid.transform.position.x + Random.Range(-1.5f,1.5f), 0, Grid.transform.position.z + Random.Range(-1.5f,1.5f)), Quaternion.identity);
Script.ClutterList.Add(tree);
t.GetComponent<Tree>().thisPoint = Grid;
t.GetComponent<Tree>().grid = Grid.GetComponent<GridScript>();
Script.active=false;
}
// Stones
for (int i=6; i< Random.Range(0,8); i++)
{
Instantiate(stones, new Vector3(Grid.transform.position.x + Random.Range(0,0), 0, Grid.transform.position.z + Random.Range(0,0)), Quaternion.identity);
Script.ClutterList.Add(stones);
}
}
}
This uses a prefab of a cube to act as each grid point. It generates items on top of each point and adds those items to its "ClutterList", which will stop any building being done whilst this list is populated.
All of this coding does work, only I want to increase my efficiency and add a method for pathfinding with an array system.
Cuirrently, these grid points have the tags:
"active" (bool)
"highlighted" (bool)
"road" (bool)
"roadplaced" (bool)
"landtype" (string)
"ClutterList" (ArrayList)
These are all logical flags to allow me to implement everything I need and I need to be able to reference these variables within the grid list.
My initial thought was to create an arraylist of these same GameObjects, but I realised that this wouldn't make the slightest difference. I was told that I should be using a 2D int array of (1,1), (1,2), (2,2) etc, but I wouldn't know how to reference the variables I need without simply using the exact same system I already have.
Any help would be fantastic as I am very confused with the whole thing, thanks.
Your answer
Follow this Question
Related Questions
How to Save an multidimensional array trough editor script 1 Answer
A* Pathfinding Project Problem with Obstacles 2 Answers
Astar Pathfinding not scanning graph 0 Answers
Multiple Cars not working 1 Answer
Pathfinding through pairs of connections 2 Answers