Matrix and lists.

Ok moving on in my game project, I have a mathematical grid for my plane (map) 64 by 64 units. I would like to place units (in a SimCity style) on the grid and record their placement using a matrix or nested lists.

2 questions arise:

1: is this the right approach? i will have a matrix composed of 4092 data entries, parsing, processing this grid through loops will take a while/lag the game maybe?

2: what is the usual process of unit placement in a game? in blender?

I would like to keep track of unit placement so for instance i can’t add a unit in the same place as a previously placed unit.

This is my environment:



Would much appreciate a discussion on different approaches to my problem, if I am not clear to you don’t hesitate if you need more details.

Thanks,

Tim

Hi Tim,

what I generally do in this situation is use a dictionary with coordinate tuples for keys. For instance:

# Fill the map up with default values
object_map = {}
width = 64
height = 64
for x in range(width):
    for y in range(height):
        object_map[ (x, y) ] = None

This allows you to quickly and easily pull up data on a specific coordinate. For instance, placing an building could look like this:

scn = GameLogic.getCurrentScene()
cont = GameLogic.getCurrentController()
own = cont.owner
over = cont.sensors['over']

if over.positive:
    # get tile coordinates
    x = int(round(over.hitPosition[0], 0))
    y = int(round(over.hitPosition[1], 0))
    
    # delete object if one exists in current tile
    if object_map[(x,y)] != None:
        object_map[(x,y)].endObject()

    # add object called 'building' than re-position it
    object_map[(x,y)] = scn.addObject('building', own)
    object_map[(x,y)].worldPosition = [x, y, 0]

Also, I’m not to sure your question about looping over your list and parsing the data. Why would you want to do this and does the use of dictionaries resolve this issue?

Fantastic,

I was going to use embedded lists didn’t know of dictionnaries they seem more versatile for what i want to do.

As for looping through the list or dictionary i would want it for say checking if a building is near or far to another object (in my game for example
would be equivalent of bad or good service time for the planes if the fuel supply truck building is too far away) So first i have to find out if the building even exists or if their are several buildings, but i may use a secondary index dictionary so i can search through a type of building and get its coordinates from the index dictionary instead of looping through whole 64 x 64 dictionary to find out.

I’m sure i’ll find something :slight_smile:

thank you again for that information Andrew

thats interesting, i’m trying the same thing.
I’m thinking around, how to update the buildings or whatever.
For example, you have a building, wich has a range of 5 Units, it has to search in this range fo ressources or anything, if it is connected to a road etc.

Is it better to loop over the whole Dictionary and get all needed informations about the objects, or let every Object change things for itself?

I find it could be better to loop through each object in a list or dictionary than run code in each object - you can get a large performance increase this way, but adapting a previous script / setup to this method may not work as well as building it from the ground up. See this thread about Loop Vs. Each Logic.

Yes, that is quit clear, why it is so!
His example uses a pulse Always-Sensor for each Object, instead of one looping trough a List.

My setup uses a similar way:

Every Object initialises itself as a instance of a Object-Class. State 1
All performance burning functions should be called at initial state. Then changes to state 2.

On state 2 every object can made to call a main function by simply change a property of the object, wich produces an impulse to the Property Sensor wich runs the script.

This way, you have easy access to every object, but without using a pulse sensor on them…

Then, if this is still not perfromant enough, u can try instead of :

for i in List:
   i['property'] += 1

Try something like:


try:
   List[own['lenlist']]['property']+= 1
   own['lenlist']+=1
except:
   own['lenlist']=0

This loops throuhg the List, for every logic-tic one iteration…

@sevi I think in your example, you’re running the script in each object - it’s substantially faster to loop through a list of objects and perform functions on each object (in a single script). Am I reading your script wrong?

Yes, every object has its on logic, but it has no pulse.
I your example, you run 1000 objects with each has a pulse.
My example has only a property sensor per object, wich is faster of course.

Why your example runs faster, is because only one pulse is needed, instead of 1000.

Probably you’re right, that looping is faster. I’m wonderng if this makes a difference, to loop throught the list and change the property, wich than executes a script, instead of directly changing things.

The advantage of my system is, that you can define several classes with functions for the objects itself, wich get runned. It’s probably easier to programm like this, but it could be slower…

I’ ll have try on that, hopefully i get some more performance.

edit:

Is there actually anything else one can use instead of lists or dictionarys?