I’m working on a game that, during startup, generates a random board made up of a grid of tiles (using scene.addObject()), and have come across several oddities that I was hoping someone could clarify for me.
- I was under the impression that game properties could be called via tile.fred, tile.wilma etc, as attributes in an object. This provides an error: “AttributeError: ‘KX_GameObject’ object has no attribute ‘fred’” even when said attribute is listed when getPropertyNames() is called. The temporary solution I’ve found is to call them instead as an associative array (Dictionary?) tile[‘fred’]. Is there something I’m missing here?
- Even with the afore mentioned solution, when objects are added inside a function, and then returned, the returned object pointers refuse access to their properties/attrubutes via the same error message as above.
def newTile(type, x, y): cursor.worldPosition = (x, y, 0); tile = scene.addObject(type, cursor, 0); tile.type = type; #error #or: tile['type'] = type; #works return tile; tile = newTile('grass', 0, 0); print(tile.type); #error #or: print(tile['type']); #Still error
- When calling the list of objects in the scene, I was surprised to see that all of the objects I had added had the exact same name, no .001 on the end or anything. Is this usual/an indication that objects added inside the game are treated differently than objects that exist before the game starts?
- Is there a way to change the name of an object? tile.name and tile.setName() are the two answers to this question that I’ve seen previously, however they both fail, possibly due to the questions above.
- Failing renaming the tiles (and calling ‘tile3-2’, ‘tile5-7’ etc), what would be the best way of calling a specific tile by x/y coord on the grid? Is there a way to return a list of objects whose centers are currently at global(x,y,z)? Should I give each tile an x/y property, and assign it it’s location on the board, and then loop through all the tiles until I find the right one? (seems like the awkwardly long way 'round.)
I am using Blender 2.57b
I am fairly new to Python, but do have some coding experience with other languages.
Thanks in advance! =)