Python dictionaries

I managed to get under the delusion that the property was not just read only… Is there any way to actually change a object’s name in-game?

How do you create dictionaries?

i tryed that a few days ago , i wanted some objects to have a random name from a list of names , but its read only.

It used to be read/write, but in 2.49 they made it read only because it was too buggy.

that’s a pain… I needed to name the objects I was spawning!

You could give them a property that holds their name, and/or a global dictionary that holds a name as the index, and the object reference as the content.

that might work… Though I’m adding the objects, so they all have the same name.

The nice thing about the dictionary is that it can hold the object reference, which is unique to each object (even added instances have their own distinct reference so the engine can tell them apart) and if you give each one a number as they’re added (tree1, tree2 etc) then you can tell each instance apart too. If you name the dictionary entry with the numberd object name, and then on each object you give them a property with that same numbered name, you can go either way when you try to find the object- if you know the object’s name you can use GameLogic.dictionaryname[‘tree1’] and it will return the reference to the object, or if you have the object (say, you hit it with a ray sensor) you can use object[‘nameProp’] and it will return the object’s name.

If at any point you want to change the name, just remember to change both the property, and add a new dictionary entry (and delete the old one)

hm, Didn’t know you could tell instances apart like that. How would I go about adding the object references to a dictionary? I haven’t used that kind of stuff much.

[edit] double-post, sorry

When you use an add object actuator, it keeps a reference to the last object it added, with actuator.lastObjectcreated

So you could do something like


To make sure no two objects have the same name, use numbering- just keep a global variable, and each time you add an object, increase this by one. Then, you can use


…or something like that.

how did you make a double-post 7 minutes apart? ) O.o

Thanks! Now I can continue on my “game” (more of a demo) that will blow multiple people away… hehe…

well, I seem to be having trouble with the dictionaries now… Can’t seem to make one!

My double post was because firefox loaded for a while (about 7 minutes I’d say), and then said the post wasn’t, well, posted… so I tried again. It actually said the second one didn’t work either, and I gave up, but it turns out both of them went up eventually.

To make an empty dictionary, do GameLogic.dictionaryName={} (note- make sure to use { and } when initializing dictionaries… after that use [ and ] to read and write to them. Confusing, I know.)

if you want to initialize a dictionary with entries in it, use this format:
dictName={‘keyName1’ : value1, 'keyName2 ': value2, etc} dictionaries should accept all data formats (string, float, object reference, etc) so no worries there.

Here’s the python docs on this, scroll most of the way down (or search for dictionaries on the page) to get to dictionary-specific docs.