Storing general game data/2D generic list question

I’m working on a fairly simple Space Harrier style game and I have two things that I need some advice on.

Firstly I’m working on setting up enemy wave data. To do this I’m using a big list of arrays but I’ve read that generic lists are better and I should use them instead. I’ve already used them in some parts of my work, but when it comes to storing enemy wave data I’m having trouble as I need to store lists inside lists (and in some cases lists inside lists inside lists!) It’s not too messy and I’m the only one who’ll ever see it but I’ve no idea how to populate the nested lists.

The data I’m refering to is the whole level of waves. I’d like to feed the data by index to each enemy wave container gameObject that I create, so I can move through eeach one by one until the end of the list. A small example of some of it (two waves…):

// Enemy container #				0						1
	isEnemyDebugData				= 	[[true,false], 			[false,false]];
	enemyTypeData 					= 	[[0,	0],				[0,		0]];
	enemyXData						=	[[-1.0,	1.0],			[-1.0,	1.0]];
	enemyYData						= 	[[0.5,	1.0], 			[0.5,	0.5]];
	enemyZData						= 	[[0.0,	0.0], 			[0.0,	0.0]];

Currently I’ve [[0,0,0], [0,0,0]] type code, as everything is being converted from array to list, but listname.Add() and listname.AddRange() cause errors when I try to fill them this way. What is the correct way to fill a nested list?

I’m not a programmer but I’d still like to keep things in best practice where I can. I’ve read that classes are a better way of doing things and I’m currently in the process of writing a small EnemyWave class that will store the wave data, but I’m seeing a lot of similarities with just adding a enemyWave script to a gameObject as a component, something I’ve already done. I suppose what I’m asking is “is there a need to use classes rather than scripts as components in this instance?”

I’m relatively new to programming, as normally I only do art asset stuff, animating, rigging, etc., so please forgive me if my terms aren’t quite correct or this is a very newbie question. :sweat_smile:

edit: In case it’s not clear (no idea if it would be) I’m working in Javascript.

Quick note: Scripts = Classes. (Unless you have multiple classes in a script, but you generally don’t do such things.)

Thought I’d repost with a really specific list in list example, as I’m going back through my code, undoing some of the wackier attempts to get a list in list working and returning to my original plan. What I have

@HideInInspector var isEnemyDebugData : List.< List.<boolean> > = new List.< List.<boolean> >();	// Should the enemy return debug values on its activity

Then later, set the values…

isEnemyDebugData.AddRange(			[true,false], 			[false,false]);

Basically a little bit of code to allow me to check on specific enemy values to see how it works in runtime. The Unity console gives me this hefty error:

I had a feeling that was how it worked, as from what little I know of classes I could see all that happening in my component scripts, all the properties and methods and whatnot. It’s nice to have some clarity on that though from someone who knows, so thanks. :slight_smile:

A List is not the same as bool[ ] - which is why you’re getting errors. I would strongly suggest creating a wrapper class for all this data and storing a List or array of those instead. You’ll be endlessly tearing your hair out with the method you have now.

I’ll do some reading on that then. If nothing else it makes sense to keep my level enemy data away from the main game controller code. I’m just a little wary of using arrays as I’d read something somewhere about #pragma strict not playi
edit: I may be confusing Array with stuff like int[ ] though.

You are. :slight_smile:

edit: I know my lower section of code is incorrect/bad syntax, as I’ve left and [i,j] off most of the variables. You can see what I’m going for though.
Experimenting with different types of array, I know I’m going to have to use a 2D array at some point, as I have multiple enemies, each with their own list of movements. I’m looking at something like this:
* *var enemyType : int[]; // picks an enemy from the list /ar enemyAnim : int[,]; // sets the animations that the enemy will do in sequence, for example [animation 1, animation 3, animation 6...]* *
The Unity Answers page here helps somewhat, but having a [0,0] = … [0,1] = … type solution is going to be both difficult to write up and a nightmare to read later. It’ll be simple enough to pull data from the multidimensional arrays one they’re full, but how can I fill them easily? There has to be a simpler way than what I’m looking at currently:
```
*var enemiesInStage : int;

enemiesInStage = 2;
for (var i = 0; i < enemiesInStage; i++)
{
	switch(i)
	{
	case 0:
		enemyType[i] 				= 	0;
		animationStackLength[i]		=	2;
		for (var j = 0; j < animationStackLength[i]; j++)
		{
			switch(j)
			{
			case 0:
				enemyMovemetType			=	0;
				enemyMovementSpeedMod		=	1;
				break;
			case 1:
				enemyMovemetType			=	1;
				enemyMovementSpeedMod		=	1;
				break;
			}
		}
		break;
	case 1:
		enemyType[i] 				= 	1;
		animationStackLength[i]		=	2;
		for (var j = 0; j < animationStackLength[i]; j++)
		{
			switch(j)
			{
			case 0:
				enemyMovemetType			=	0;
				enemyMovementSpeedMod		=	1;
				break;
			case 1:
				enemyMovemetType			=	1;
				enemyMovementSpeedMod		=	1;
				break;
			}
		}
		break;
	}*

```
It doesn’t look too bad there, but I’ve trimmed off 90% of the data that I need. Worse, I’m having to create new variables to facilitate the for loops, as there’s no way for the code to know how many times it can iterate. It seems like a clumsy, sloppy way to do this. Can anyone advise me on how best to add large amounts of data to a data tyle[,]?

Just a quick reply to my own post. I’ve reformatting the text in my code to make it more readable, as I’m probably going to have to do things this way. I hadn’t realised that arrays of this type, while fast as heck, don’t have .Add() or similar methods and can’t be resized. Unless there’s some simple one-line solution for declaring series of array entries then I think I’ll just stick with the method above. It’s not very pretty but (I think) it’ll do the job.