0
\$\begingroup\$

I have a MapLayer with MapObjects and i want to know the ID of the MapObjects in this Layer, any idea how to get the ID?

\$\endgroup\$
0
\$\begingroup\$

ID is stored as a property on the MapObject. You may be able to pass in Integer.class to properties.get() instead.

MapLayer objectLayer = model.Map.getLayers().get("Objects");

if(objectLayer != null){
    MapObjects objects = objectLayer.getObjects();

    for(MapObject mapObject : objects){

        MapProperties properties = mapObject.getProperties();

        String id = properties.get("id",String.class); 
    }       
}

That being said, I recommend you not use the ID value to identify objects on your map since Tiled does not give you control over this value. If the ID's ever change, then you will need to either edit your code or manually edit the .tmx file to correct this. Instead, you could add a custom property to the objects called "Tag" that will help you identify them.

\$\endgroup\$
  • 2
    \$\begingroup\$ Right, the ID isn't usually that useful and is more planned to be used by future versions of Tiled to make references between objects. Apart from adding custom properties, you can also use the built-in "name" and "type" properties of course. \$\endgroup\$ – Thorbjørn Lindeijer Aug 30 '16 at 22:20
  • \$\begingroup\$ Thanks! I was confused because the name and type and everything is available over getter but no ID. In my case its not so bad when the ids should change, i only need an integer that represents the object \$\endgroup\$ – Liquidz Aug 31 '16 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.