0

So this is for something that is similar at least to what you might run into in a minecraft game.

Basically I have a game where you objects and I want to create and destroy objects based on there position in the world.

I feel like I should use an array because I can sort the array and search it very quickly by doing

array[spawnposition] = gameobject 

when I create an object to store that object and its instantly in the correctly sorted spot for that position

and then of course use the exact spawn position to modify and delete.

The issue is that an even 100 X 100 X 100 area is a very large array.

So i'm not sure how best to alleviate this. I think its largely the cubing thats the issue so I think If I just restricted one dimension I could get a lot further but i'm curious how things like minecraft deal with massive worlds.

I'm sure partly they chunk the arrays and perhaps create arrays of arrays to navigate to the correct array.

But i'm wondering if there is a better way, something that makes it so for example null values don't take up space maybe. So only once I assign a value to the array it finds room for it making it so a very large array size is actually only as big as the amount of actual items in it, not potentional but still allows for rapid access like an actual array does.

basically i'd use List for example for the dynamic but I would lose the indexer access of fixed arrays that lets me access and retrieve stuff really fast without doing a search.

3
  • 1
    Minecraft indeed uses chunks. Commented Dec 5, 2013 at 2:51
  • look into jagged arrays. C# has lots of optimizations related to them that can outperform native code if used correctly. Commented Dec 5, 2013 at 2:57
  • 1
    Do not use ArrayList - it's obsolete. Commented Dec 5, 2013 at 3:12

1 Answer 1

8

Short version:

  • Use Arrays if you want a fixed number of items
  • Use List if you want a dynamic number of items
  • Use ArrayLists if you want to make everyone bleed from their eyes

I always suggest using Lists. There is no real noticeable performance difference from arrays, you have the full power of IEnumerable, and they are strongly typed (hence why you don't use ArrayList).

You can access an individual item using a List with an index number:

List<string> items = new List<string>();
string someItem = items[4]; //assuming these items already exist
items[5] = "asdf";

However, the choice of which to use is entirely up to you. You can read up on Arrays, Lists, and ArrayLists, then design your approch from there.

Sign up to request clarification or add additional context in comments.

1 Comment

For this particular problem though, a 2D list (list of lists) of chunks (3D array) would be closer to the way minecraft was implemented. A solution to chunk loading/unloading could involve using a 2D bidirectional queue as your lists. Using lists to store individual blocks would become grossly inefficient when doing any block look-ups.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.