C# Arrays - Using them to store game level

c#

Solution

Make a `Tile` object that has the information, like `AlphaLevel` and `NumberOfTimesClicked`, and make a jagged array of those. Then, you only need one jagged array to hold all of your information, and it's organized more nicely, since you can easily find all the properties a `Tile` has.

If you're interested in memory optimization, NPSF3000 is correct in saying structs will be marginally better. Make sure you're familiar with how value and reference types differ before using them.

Storing data in a smaller format is always going to be bigger than not storing data at all. In addition to working to compress your current data as much as possible, look at ways of storing old data you won't need to disk, or discarding data you can generate procedurally later.

One last note: are you sure you need to compress it? When I worked on 2D tile systems in the past, I found that I was actually storing some extra data so I could improve speed, because I wasn't actually using that much memory. Run some numbers to make sure you're actually using enough memory for optimizations to be worth it.

Problem

I'm making a 2D tile based game. I use a 2D jagged array to store an integer, and the renderer then draws a certain image depending on what that integer is. I have this part of my code working, however I've come to a point where I'm not sure how to proceed next. As the player goes on through the game, I need to be able to store information about every cell in the 2D array, such as the light level, or the number of times the user has clicked that cell, or the rotation value of the image in that cell, or the alpha level for that image, etc etc. The only way I can think of doing this is to create a 2D array like I have done previously, for each piece of information I'm using. So for example: ``` AlphaLevel[][] NumberOfTimesClicked[][] ``` And then if I needed (for example) to check the number of times the user has clicked the cell 5,5 I can do NumberOfTimesClicked[5][5] or if I needed to set the rotation I could do SetRotation[5][5] = 90. The thing I'm not sure about is how efficient this is, having an array for each piece of information doesn't seem that memory efficient. Is the idea I have a good solution, or is there a better way I haven't thought about? Any advice would be great! Thanks

Original source