Martin Lukas
Martin Lukas

Reputation: 314

2D array consisting of 2D arrays

I know similar question have been asked but, I'm crating a game with C# and Unity and I'm trying to create chunk system, I've created an array

bool[,] chunkData = new bool[chunkWidth, chunkHeight];

now this array represents area of 32x32 tiles - one chunk, now I'd like to have another 2D array which will store all the chunks The reason I've posted this question is because I'd like to ask if you can think of better idea of doing it, since I've never done such thing before.

Upvotes: 0

Views: 61

Answers (1)

Tigerpaws
Tigerpaws

Reputation: 66

A simple way for this is to wrap up the chunkData into a class, and then you can use any container to store a large collection of them. e.g.

public class Chunk
{
    private bool[,] data;
    public Chunk(int width, int height)
    {
        data = new bool[width, height];
    }

    public bool GetChunkDataAt(int x, int y)
    {
        return data[x, y];
    }

    public void SetChunkDataAt(int x, int y, bool value)
    {
        data[x, y] = value;
    }
}

Some error handling is omitted; the encapsulation makes it impossible to re-assign chunk data array from the outside, but if that should be it, keep it public is not that bad.

And then you can something like this:

Chunk[] chunkArray;
List<Chunk> chunkList;
// ...

Upvotes: 2

Related Questions