C# XNA 4.0 How to serialize a 2D array?

Started by
44 comments, last by Shawn Naef 6 years, 1 month ago
1 hour ago, Shawn Naef said:

Ok the list is edited. The draw code is edited. I am still encountering errors (35 Total)

About 5 of these errors.

Error    1    'System.Collections.Generic.List<System.Collections.Generic.List<intes not contain a definition for 'GetLength' and no extension method 'GetLength' accepting a first argument of type 'System.Collections.Generic.List<System.Collections.Generic.List<intuld be found (are you missing a using directive or an assembly reference?)    C:\Users\Haswell\documents\visual studio 2010\Projects\Vertical Miner Infinity\Vertical Miner Infinity\Vertical Miner Infinity\Game1.cs    424    44    Vertical Miner Infinity
 

About 30 errors stemming from this

I also need to know how to edit the list from my code:  (Example of one of the sections) Error is ((tileMap[PlayerDepth, CheckSpot] == 9)

Specifically tileMap is now a list not an array.

 



 else if ((tileMap[PlayerDepth, CheckSpot] == 9) && (GoAgain) && (PlayerFuel > 0))
            {
                // Its just air so moving is fine.
                cameraPositionX = cameraPositionX - 64;
                PlayerPosition--;
                PlayerFuel--;
                GoAgain = false;
                TotalMovesMade++;
                TotalFuelUsed++;
            }

 

 

The errors are telling you exactly what is wrong.

"

Error    1    'System.Collections.Generic.List<System.Collections.Generic.List<intes not contain a definition for 'GetLength' and no extension method 'GetLength' accepting a first argument of type 'System.Collections.Generic.List<System.Collections.Generic.List<intuld be found (are you missing a using directive or an assembly reference?)    C:\Users\Haswell\documents\visual studio 2010\Projects\Vertical Miner Infinity\Vertical Miner Infinity\Vertical Miner Infinity\Game1.cs    424    44    Vertical Miner Infinity

"

The first error is telling you that List does not contain .GetLength which is true. When dealing with List you would use .Count to find out how many entries exist. Since columns can have a number of rows you'll have keep this in mind when doing for loops. When using any class in C# that you're unaware of, look online for the reference so you can see all the methods: https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx You'll find that Count does: Gets the number of elements contained in the List<T>. You'll also find that First() does: Returns the first element of a sequence. (The same as doing myMap[0].Count)

In general to see your columns and rows you would just use this: myMap.Count and myMap.First().Count assuming your rows are the same amount per column.

To answer your question about:

"

I also need to know how to edit the list from my code:  (Example of one of the sections) Error is ((tileMap[PlayerDepth, CheckSpot] == 9)

"

The error is correct because you don't use [,] for List you use [][] (Since it's a List of List). So use tileMap[PlayerDepth][CheckSpot] == 9

If you're looking to change values at index just use tileMap[y][x] = VALUE; If you're looking at expanding more, just use .Add(new List<int> { 0, 0, 0, 0, 0 }); If you're expanding to the right, you'll have to loop through each myMap[a] and .Add()

To keep things simple make sure your rows are equal per column, and put a variable to indicate empty such as 0.

Programmer and 3D Artist

Advertisement

Column are always 30 in size. depth is the part that is infinite up to the amount of level design i do. There is no need for 0 because an empty cell (air) (essentially what happens when you mine) has a background texture of its own.

I will never need to add to a list because that would shift everything out of place remember the world map's exact position of a particular tile never changes. Only the contents of it. (int value)

 

the part im concerned about is that it needs the length of the list not the list of lists. This is for calculations.

Cleared all the errors and upon debug this happened:

 

Issue.jpg

I'm not sure if this fixed it but i changed [x] [y]   to [y] [x] and that seemed to have fixed it. The game loaded and this is what i got on the save file after saving!

 

 

 

mysave.xml

Well after starting new game playing, saving, restarting client, loading, saving, restarting client, loading  it seems to be working 100 percent.

 

Here is a screenshot of the save, it is indeed exporting the list as an array.

 

2v2b194.jpg

Thanks for the help.

This topic is closed to new replies.

Advertisement