I know just enough about programming to be dangerous, so please be gentle. I've created and released few simple apps for android and do some powershell scripting at work, but that's about the extent of my programming experience. I'll be using c# and XNA if that makes a difference.

If I need to keep track of ten thousand to several hundred thousand of the same object with a handful of variables each, what's a good way to handle that in a turn based strategy game? The game will need to loop through each object on each turn to check a few things.

  • 1
    \$\begingroup\$ The keyword you are looking for is 'scenegraph' \$\endgroup\$ – Roy T. Feb 15 '13 at 21:28
  • \$\begingroup\$ What's wrong with using an array of structures or classes which contain variables? This question doesn't really seem to be game-development related, specifically; it's just a "how do I store a lot of data" question. \$\endgroup\$ – Trevor Powell Feb 15 '13 at 22:24
  • \$\begingroup\$ You may be right Trevor, but if so, I lack the vocabulary for a proper google search because I've had a lot of trouble finding solutions to this kind of programming issue. Can you suggest some proper search terms? \$\endgroup\$ – user548369 Feb 15 '13 at 23:03
  • \$\begingroup\$ Good development practice suggests that you just store them in a List and wait until you actually have something running before optimising. That way you'll know exactly what data you need and how it will be used. It's a bit hard to give good optimisation advice without that information. You'll also have a better idea if you actually need to optimise it at all - and whether your optimisations are actually working. \$\endgroup\$ – Andrew Russell Feb 16 '13 at 6:43

First create any solution you want and profile. I won't be surprised if your 100k objects will work OK using any container (unless of course you're doing mobile development for windows phone).

If it wont work, I see 2 mayor sources of optimization:

  1. Do you really need to loop through every object in every turn? Can you possibly limit your loop to the objects currently on-screen? If yes, you'll benefit a lot from some spatial-aware container, i.e. the container that can quickly give you the objects within a boundary box (there're several methods how to implement such container, all have pros and cons).

  2. If you do need to access that many objects every turn, your objects have ~20 variables each, each turn you only need to access ~2 variables of that 20 - you could benefit from optimizing your memory layout, see this article. It's for C++ but the idea is mostly language-agnostic: regardless on the programming language, you still have the CPU with 3 levels of on-die cache that can only access system RAM by 64 bytes blocks.

| improve this answer | |
  • \$\begingroup\$ Thanks, and I'm not doing mobile development.#1 sounds like what I'm looking for. \$\endgroup\$ – user548369 Feb 15 '13 at 22:07
  • \$\begingroup\$ Whoops, still getting used to the interface. What I think would work is storing some random rolls in an array and then as as the player or the game itself needs updated stats for some reason, it could loop through the code and update that specific object to the current game turn. Maybe put each object on it's own rotation schedule so that no object was ever more than say, 20 turns behind the actual count? \$\endgroup\$ – user548369 Feb 15 '13 at 22:07
  • \$\begingroup\$ Sorry, I don't know nothing 'bout your gameplay, and I'm afraid I can't answer your precise question. The general answer - if you usually only need to access the subset of your objects, think about the container for them, that already aware of that subsets, and can return you the subset you need without iterating through all of them. It could be some complex spacial-aware container, or array sorted by some criteria, or list ordered by some criteria, or hashmap with some key, or some (e.g. Red-Black) tree with some key, etc - whichever fits best.. \$\endgroup\$ – Soonts Feb 15 '13 at 23:27
  • \$\begingroup\$ Thanks Soonts, that gives me some ideas and a place to start. \$\endgroup\$ – user548369 Feb 16 '13 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.