Hello, I’m trying to get the “Class” attribute of my objects (in python with the pytmx library) if I understood the documentation correctly it must be called with an object.type, but this attribute is always “None”. When I was debugging my script I noticed that my objects had the class attribute where the value I was looking for was stored, but fortunately I can’t access it (by making it object.class) because in python class is a protected name. I was able to retrieve it with object.type before, but since a while it doesn’t work anymore. Does anyone have a solution?
Translated with DeepL Translate: The world's most accurate translator (free version)
An easy solution would be to set up a Tiled project file (if you haven’t done so already) and then going into Project → Project Properties to change the Compatibility version to “Tiled 1.8”. This way, Tiled will still use the “type” field and you should be able to keep using the currently version of pytmx.
The long-term solution should be for pytmx to get updated to also support loading the “class” field. In C++,
class is also a keyword, so in my implementation this field is called
className instead. This may be a solution for pytmx as well.
Thank you I am going to try this solution.
I don’t use PyTMX myself so I can’t vouch for this, but recently someone on the Tiled Discord said they were using PyTMX and were able to get the class property with
getattr(object, "class"), so it’s possible that PyTMX reads in the properties generically (
object.class is not useable because
class is a reserved keyword). The downside to this approach is that you’d have to make sure your code can deal with both
type, instead of the library handling it for you, but it may be a more robust solution than relying on the Project compatibility setting.