Efficiency Questions with Python

Actually, I have a few questions:

First, as far as tile-based games are concerned, what is likely the most effective way to ‘build’ maps of this type? It occured to me that I wouldn’t need to store much in terms of information about the architecture itself as once I’ve placed it into the Scene Graph, it will get handled for me.

Which leads me to my next question… Knowing that I can work with lists (pretty much the same as one dimensional arrays), I can’t seem to be able to ‘emulate’ a multidimensional array… it would seem that no matter which way I try to access data, I’m just not pointing to the right location in the list.

Essentially, I’m interested in working with a 2D array - this way I can keep track of moving objects… Which leads me to the third question:

Is it even neccessary to keep track of moving objects by creating an array? Or can I simply iterate through a Scene Graph NodePath? Seeing as the Scene Graph stores information about all objects attached to the scene, it would seem easy enough to create 3 NodePaths; the ‘map’ NodePath which contains all of the level geometry (walls, floor tiles, etc), ‘non-moving objects’ containing generally stationary objects (e.g., potted plants, benches, trach bins, etc.) and then the ‘moving objects’ such as people, rats, etc.

I guess this post is mostly to help get my thoughts clearer as well as to see if my thoughts about traversing nodepaths to work with objects is a better way of working with a scene than keeping a copy of positional information in an ‘emulated’ array. I hope it all makes sense.

We usually find it easiest to store our own tables of data in Python, even if some of this data is duplicated in the scene graph. This is because piggybacking auxiliary data onto the scene graph, while possible, is a little bit clumsy. If all you need to store is the nodes themselves, then fine, but usually you have additional data to associate with each node.

There are lots of ways to achieve a multi-dimensional array in Python. One sneaky way is to use a map of tuples, like this:

myArray = {}
myArray[(0, 0)] = 'a'
myArray[(0, 1)] = 'b'
myArray[(1, 0)] = 'c'
myArray[(1, 1)] = 'd'

for i in range(2):
  for j in range(2):
    print 'i = %s, j = %s, myArray[(i, j)] = %s' % (i, j, myArray[(i, j)])


Awesome. Thanks! That’s exactly what I needed to know.