List or Array of lists

I have a list of LevelPieces to make up a game level.
public List sourcepieces = new List ();
containing however many pieces for a single level.

I now want to add levels, so I need either an array of sourcepieces or a list of sourcepieces.
I can then have a variable for level, and produce my list of levelpieces based on which level I am on.

I’ve searched a little but am struggling to get my head around a) whether an array or list is better and b) how it is done syntactically.

I may need to save these lists out at some point, and there seems to be a suggestion that lists of lists are not serializable/savable - is that true?

Anyway, I’ve tried doing the following:

        public  List<SourceLevelPiece> sourcepieces = new List<SourceLevelPiece> ();
        public List<sourcepieces>  sourcepieceslist = new List<sourcepieces>();

which would give me a list of my list of pieces (sourcelevelpieces) but it doesn’t like the last line.

I can get the following to compile, but not sure that is right.

        public  List<List<SourceLevelPiece>> sourcepieceslist = new List<List<SourceLevelPiece>>();

As I presumably want a list of sourcelevelpieces rather than a list of SourceLevelPieces (which I already have)

And once I have my list of lists, how do I go about adding a) a new main level to the list and b) adding the subobjects for the main level list.

Sorry, I probably haven’t got a lot of the terminology right. I’ve tried using the latter syntax above but I’m a bit stuck now working out what syntax to use to even access them.

I basically need to replace:

            SourceLevelPiece sourcepiece = Instantiate(SourceLevelPiece);
            sourcelevelpieces.Add (levelpiece);

with something along the lines of

SourceLevelPiece sourcepiece = Instantiate(SourceLevelPiece);         
sourcepieceslist[0]. Add (sourceLevelPiece);

but again the syntax completely alludes me.

This is all working with a single List. I just need to amend my code to convert the single list to a list of lists and reference the one I want with a index int.

Thanks in advance.

You can definitely get it done.
Here’s a very simple example:

List<List<int>> numbers = new List<List<int>>(); // List of List<int>
numbers.Add(new List<int>());  // Adding a new List<int> to the List.
numbers[0].Add(2);  // Add the integer '2' to the List<int> at index '0' of numbers.

If the size is fixed/known, arrays can work just as well for you as a list. Then it’s a matter of choice.
If you are modifying their sizes, a List is more convenient, as the internal array will be resized for you. :slight_smile:

Just for saying so’s sake, you could also have an array of lists or a list of arrays. lol :slight_smile:

1 Like

Thanks.
I was getting confused as I’m not using a built-in type.

I now have it working with:

        public  List<List<SourceLevelPiece>> sourcepieceslist = new List<List<SourceLevelPiece>>();

instantiating a new level with (and this is the bit I’d forgotton earlier:()

sourcepieceslist[levelNumber].Add (sourcepiece);

and accessing variables with:

sourcepieceslist[levelNumber][currentpiece].varname

Which is I think what I should be doing. Seems to be working anyway.
Thanks for your help.

1 Like

Yep, that looks right. I figure you have a :

sourcespieceslist.Add(new List<SourceLevelPiece>());

somewhere, too, yes? That should be for as many level numbers as you need.

Anyways, you’re welcome. Glad you got it working :slight_smile:

1 Like