# chek if list index exists

This topic is 2601 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey i have a list that contains data on fixed positions. Example [0] is ground, [1] to [2] are walls, [3] < are objects.

Now i want to check if [1] or [2] exist to delete them when they do. A count would not work as the list could have [0] [3] and [4] set and then removing [1] would give me a exception.

To be a little more clear what i want exactly is something like this:

 for (i = 1; i < totalLayers; i++) { if (tileMap.MapTiles Has something in it) { tileMap.MapTiles.RemoveAt(i); } } 

##### Share on other sites

Hey i have a list that contains data on fixed positions. Example [0] is ground, [1] to [2] are walls, [3] < are objects.

Now i want to check if [1] or [2] exist to delete them when they do. A count would not work as the list could have [0] [3] and [4] set and then removing [1] would give me a exception.

To be a little more clear what i want exactly is something like this:

 for (i = 1; i < totalLayers; i++) { if (tileMap.MapTiles Has something in it) { tileMap.MapTiles.RemoveAt(i); } } 

You could do two things either do

 for (i = 1; i < totalLayers; i++) { if (tileMap.MapTiles Has something in it) { tileMap.MapTiles.RemoveAt(i); break; } } 

or

foreach(TileMap tileMap in whateverTheListIsCalled){ if(tileMap Has Something in it){ tileMap.Remove(); break; } }

The trick is to break the loop or you will go out of bounds of the index count, as if you got seven elements, you go through the entire list and the third element you remove. Now the fourth element is the third etc. so the seventh is now the sixth, so when you come to the seventh element that ofc. does not exist anymore you get an error.

Hope it helps

##### Share on other sites
The problem is in the if statement.

If i do:

if (tileMap is WallTile) // <-- here it gets an error if there is nothing in this index.
{
do something
}

Also, is there anyway to remove a item and keep the list as is? I would like to have each index have specific values, [0] for ground, [1]&[2] for walls, [3]+ for objects so if i remove a wall i don't want the object in [3] moved to index [2].

##### Share on other sites
Make a data structure out of the information:
 struct Tile { Ground ground; List<Wall> walls; List<GameObject> gameObjects; } 
Then use a List<> of these structures.

##### Share on other sites

The problem is in the if statement.

If i do:

if (tileMap is WallTile) // <-- here it gets an error if there is nothing in this index.
{
do something
}

Also, is there anyway to remove a item and keep the list as is? I would like to have each index have specific values, [0] for ground, [1]&[2] for walls, [3]+ for objects so if i remove a wall i don't want the object in [3] moved to index [2].

Hmm you could make a bool in your tileMap class that is true if it is a WallTile or another sensible work around, and check if tileMap.YourBool == true Then that should take care of that.

If you has made it as a list I believe it will always shift the elements if you remove one, you should be able to remove an element from an Array though without messing with the rest of the order, but keep in mind you then would have an empty element in your Array (Not sure it if works like that but its worth trying using arrays instead )

##### Share on other sites
Well the problem is those walls and objects are derived from a base map tile. When i write to XML i save a lot of space if the tile has just a ground tile as it would not write the entry for the wall or object if it does not exist, this saves a lot of space in my xml file.

I did this which seems to work:
 try { tileMap.MapTiles.RemoveAt(i); } 

Still the problem remains with removing an item the list entries get replaced by -1.

I have a simple work around though, removing everything except the 1st entry which is the ground tile and build the tile from scratch again. Instead of having a clear walls and clear objects button i just make one button that clears all. I will only use this myself for building and editing maps so it doesn't need to be too practical and user friendly as long i can work with it fast enough.

• 11
• 20
• 12
• 10
• 37
• ### Forum Statistics

• Total Topics
631400
• Total Posts
2999860
×