I have been following a tutorial on creating a minecraft like world, but want to change an array to a list to allow for infinite generation.
I want to be able to pass a “chunks[x, y, z].Init” or something to that effect from the world but I’m struggling. using the array it works but when changing to either of the two lists I just cant get it to work. I have the following;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class World : MonoBehaviour
{
/*List<Chunk[,,]> chunks = new List<Chunk[,,]>();*/
List<Chunk> chunks = new List<Chunk>();
/*Chunk[,,] chunks = new Chunk[20, 20, 20];*/
List<ChunkCoord> chunksToCreate = new List<ChunkCoord>();
private bool isCreatingChunks;
private void Start()
{
GenerateWorld();
}
private void Update()
{
CreateChunks();
}
void GenerateWorld()
{
for (int x = 0; x < 2; x++)
{
for (int y = 0; y < 2; y++)
{
for (int z = 0; z < 2; z++)
{
chunks.Add(new Chunk(new ChunkCoord(x, y, z), this, false));
chunksToCreate.Add(new ChunkCoord(x, y, z));
}
}
}
}
IEnumerator CreateChunks()
{
isCreatingChunks = true;
while (chunksToCreate.Count > 0)
{
chunks.Init();
chunksToCreate.RemoveAt(0);
yield return null;
}
isCreatingChunks = false;
}
}
and
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chunk
{
public ChunkCoord coord;
World world;
GameObject chunkObject;
public Chunk(ChunkCoord _coord, World _world, bool generateOnLoad)
{
coord = _coord;
world = _world;
if (generateOnLoad)
Init();
}
public void Init()
{
chunkObject = new GameObject();
chunkObject.transform.SetParent(world.transform);
chunkObject.transform.position = new Vector3(coord.x * Settings.ChunkSize, coord.y * Settings.ChunkSize, coord.z * Settings.ChunkSize);
chunkObject.name = coord.x + ", " + coord.y + ", " + coord.z;
Debug.Log("Chunk: " + coord.x + ", " + coord.y + ", " + coord.z + " generated.");
}
}
public class ChunkCoord
{
public int x;
public int y;
public int z;
public ChunkCoord()
{
x = 0;
y = 0;
z = 0;
}
public ChunkCoord(int _x, int _y, int _z)
{
x = _x;
y = _y;
z = _z;
}
public bool Equals(ChunkCoord other)
{
if (other == null)
return false;
else if (other.x == x && other.y == y && other.z == z)
return true;
else
return false;
}
}