- Home /
How to create endless board game path
So I'm making an endless RPG type board game, but I'm having trouble with setting the route for player to move.
On "Start" function I instantiate custom amount of "Road" prefabs ("Road" is a 10x10 green shape which has custom "road cut out" filled with 1x1 brown road tiles), which have different directions. When these prefabs are instantiated I parent them to the GameObject that has Route script attached to it and put only brown tiles into a list that makes up the road. Then using OnDrawGizmos I pick every single tile and try to plot the road with green line, but in the picture it's clear that green line is all messed up and doesn't follow a path (By the way the start is bottom right). In list I can see that tiles are put in random order, but I can't seem to figure out how to sort or put them in a list so that they create a normal path.
public class Route : MonoBehaviour
{
GameObject[] childObjects;
public List<Transform> nodeList = new List<Transform>();
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
FillNodes();
for (int i = 0; i < nodeList.Count; i++)
{
Vector3 currentPos = nodeList[i].position;
if(i > 0)
{
Vector3 prevPos = nodeList[i - 1].position;
Gizmos.DrawLine(prevPos, currentPos);
}
}
}
void FillNodes()
{
nodeList.Clear();
childObjects = GameObject.FindGameObjectsWithTag("Tile");
foreach (GameObject child in childObjects)
{
if(child != transform)
{
nodeList.Add(child.transform);
}
}
}
}
Answer by unity_ek98vnTRplGj8Q · Mar 01, 2021 at 10:23 PM
@Vollmondum This will keep track of the order of the big tiles, but the small tiles are instantiated in with the big tile prefab it sounds like, so he never actually instantiates the tiles he needs to keep track of. You have a few options here when it comes to sorting your small tiles but here are the first that came to mind.
Instead you can sort the order of the tiles in your transform hierarchy inside your prefab according to the actual order of the tiles in game. Then you can look at their Transform.GetSiblingIndex() in order to read what order they are in inside the large tile prefabs. This requires the least manual work but is a little hacky.
You could also just attach a script to the large tile prefabs that keeps track of all its children tiles in an ordered list. The downside here is that you have to manually drag and drop all of the tiles into this list for each prefab that you have, but the upside is that you can just append these lists to the main one when you generate your overall path list.
You could always write some kind of algorithm that starts at the beginning of the path and has each tile check for the closest tile using something like Physics.CircleCast and just build your list one node at a time, but that sounds like a lot of excessive coding and debugging.
Excuse me I'm fairly new to program$$anonymous$$g, could explain how can I append all lists into one ? I did as you said, I made a script and attached it to every large tile prefab and added tiles to it in order.
public class PrefabScript : MonoBehaviour
{
public List<Transform> tiles = new List<Transform>();
}
Then I loop through children and pick that script component, but when I try to nodeList.Add(prefabScript.tiles); I get an error
void FillNodes()
{
nodeList.Clear();
for (int i = 0; i < childObjects.Length; i++)
{
prefabScript = childObjects[i].GetComponentInChildren<PrefabScript>();
nodeList.Add(prefabScript.tiles);
}
}
Severity Code Description Project File Line Suppression State Error CS1503 Argument 1: cannot convert from 'System.Collections.Generic.List ' to 'UnityEngine.Transform'
Ok I think this fixed it, not sure if its the correct way but it owrks. Prefab script:
public class PrefabScript : MonoBehaviour
{
public List<Transform> tiles;
}
Get route tiles:
void FillNodes()
{
nodeList.Clear();
childObjects = GameObject.FindGameObjectsWithTag("Prefab");
foreach (GameObject child in childObjects)
{
prefabScript = child.GetComponent<PrefabScript>();
Transform[] array = prefabScript.tiles.ToArray();
for (int i = 0; i < array.Length; i++)
{
nodeList.Add(array[i]);
}
}
}
You can also use AddRange() instead of Add() to add the whole list at once:
nodeList.AddRange(prefabScript.tiles)
Answer by Vollmondum · Mar 01, 2021 at 08:39 PM
You don't "fill" your list with Find. You need to fill it while instantiating. var instance = instantiate.... myList.Add(instance);
That will keep track and order.
Your answer
Follow this Question
Related Questions
[C#] Sorting a List of Gameobjects Alphabetically 2 Answers
A node in a childnode? 1 Answer
Sort List by string field 1 Answer
Is it possible to sort an array determined by an external comparison? 1 Answer
Multiple Cars not working 1 Answer