How to handle many updating objects efficiently in C#?

后端 未结 6 1142
感情败类
感情败类 2021-01-05 00:34

I\'m developing a 2D overhead shooter game using C# and XNA. I have a class that I\'ll call \"bullet\" and need to update many of these instances every fraction of a second.

6条回答
  •  醉梦人生
    2021-01-05 01:10

    I will admit that I don't have any experience in this per se, but I would consider using a traditional array. Initialize the array to a size that is more then you need, and would be the theoretical maximum number of bullets, say 100. Then starting at 0 assign the bullets at the beginning of the array, leaving the last element as a null. So if you had four active bullets your array would look like:

    0 B 1 B 2 B 3 B 4 null ... 99 null

    The benefit is that the array would always be allocated and therefore you are not dealing with the overhead of a more complex data structure. This is actually fairly similar to how strings work, since they are actually char[] with a null terminator.

    Might be worth a shot. One downside, you'll have to do some manual manipulation when removing a bullet, probably move everything after that bullet up a slot. But you are just moving pointers at that point, so I don't think it would have a high penalty like allocating memory or a GC.

提交回复
热议问题