Associating Enemies with polylines

Hi All Im trying to associate a polyline with one of my map Enemies so my game can use the polyline as a patrol path for the enemy. Whats the best way to do this in tiled. specifically Is there anyway of directly associating the points to the enemy object in tiled ? as searching through the array in my game for each enemy and then searching for each polyline which goes with that enemy object is too processor intensive for my game.

Can anyone help with this ? Just to clarify, the current xml output for my enemy is something like this

object id=“272” gid=“516” x=“864” y=“384” width=“128” height=“128”

And the current xml output for my polyline is

object id=“275” x=“96” y=“1536”
** polyline points=“0,96 1152,96”**
** object**

What Im trying to achieve is something link this

object id=“272” gid=“516” x=“864” y=“384” width=“128” height=“128”
** polyline points=“0,96 1152,96”**
** object**

Where the polyline is a property of the enemy object, so I can read it in my game in one clean sweep, instead of loading two map objects the enemy and the polyline and then searching through the newly created array of map Objects and somehow pairing the two. I was thinking of something like shift clicking the enemy and the poly line and right clicking and selecting something like make polyline property of enemy object, but no such options exists. is there a way to do something similar ? I cant figure out how to pair them in my app without a lot of overhead and memory wastage any help greatly appreciated thanks

Multi-part objects like “tile and polygon” objects are currently not supported. It may be something to change in the future since I’d like to move towards a component-driven structure, but that’s thinking a long time ahead.

Alternatively, you’d want to link the two objects. While there is an issue open for this, the basics you need are already there. Each object gets assigned a unique ID, and you can create a custom property on one of the objects and refer to the other object using this ID. You could do the same based on the name you give the object, which will be easier to work with in the editor.

You would avoid processor-intensive searching by using a map data structure, for example a std::unordered_map<std::string, Object*> would allow quickly looking up an object by its name. Also, of course you need to do this lookup only once after loading the map.

Thanks for the clarification Bjorn, I thought I would have to use the custom properties to associate the two, Im coding in objective c and not using dictionaries if thats what you mean by a map data structure, Im loading the xml data into custom map objects in a level data class, each mapObject extends NSObject and holds the object type, points array, custom properties dictionary, and the x and y coordinates, which is then placed in a 2D array. I then have a spawn class which runs through the array spawning the map objects the mapObject data and a pointer to the map object itself is passed to reusable sprites contained in pools for example if(mapObject.type == CAT) get sprite, set cat animation. So I guess Im going to have to figure out the best way of associating the two objects myself as its all custom code on my end. Thanks for your help anyway mate, at least I now know what is and isn’t possible so I don’t waste any time going down any blind allays :slightly_smiling_face:

Yeah I guess in Objective C this is called a Dictionary. I suggested you use one to quickly look up other objects referenced by custom properties. Why wouldn’t you?

Hi Bjorn but wouldn’t that mean I would have to manually name each object and polygon pair ?
I understand how to edit the tileset properties, so each added instance of an object already has the property ready to give it a value, but I don’t know how to do that for polygons / polylines. Also most of my polygons are scenery over the tiles and not paired with objects so it would be a be a bit of a waste to add that custom property to ALL polygons.
Not to mention the inevitable misnamed pair errors I would get, or am I not understanding what your saying correctly ?

Right, you could name the object and then add a custom property to the polyline that tells you what object it is associated with.

For polylines you could give them a type like “path”, and based on this type you can predefine a string “object” property in the Object Types Editor. Of course, you would only give the ones that are actually paths this type. You can also specify a custom color for this type so that these polyline paths are easier to recognize.

For this you should probably add some log / error message to your game so that you will notice when a certain polyline specifies an enemy name (or ID) that isn’t found, as well as when it detects multiple polylines are pointing to the same enemy.

Thanks for all your help I didn’t know about the custom type editor which is really handy especially the ability to colour code my points. The solution I eventually came up with was relatively simple

First I created an objectGroup in Tiled called “paths” which I placed on the layer second from bottom above my tile Layer, so it would be read first before the objects which might use it.

I then had a string _currentObjectGroup which kept track of the current object group, if the current object group is “paths” then convert the current objects id into a string and use that as the dictionary entry name, and placed it in a separate dictionary called “paths” else place the current object in the 2d spawn array.

Then in Tiled I placed a custom string property on objects which followed paths called “path_id” if an object has such a property it uses it to look up the relevant path object in the “paths” dictionary and sets the map objects points property to equal the points property of the paths object.

Thanks once again Tiled is a great tool and I really appreciate all your hard word.

1 Like