Multi-tile objects

Hi folks. I’m evaluating Tiled for a game I’m working on. One of the things my game needs is to be able to associate tiles together for Y sorting, as my game is top-down. When a sprite is higher on the screen than another sprite, it should be sorted behind it. When a sprite is lower on the screen than another sprite, it should be sorted in front of it.

A problem occurs when this approach is taken with tiles. When the player character’s Y position is compared against the Y position of a wall, it should compare against the entire wall, not against all the individual tiles that make up the wall. If the player is compared against each tile in the wall, he may end up being sorted between the wall’s tiles.

What I need is some way to create an object that associates multiple tiles. I was wondering if this could be achieved by creating a tile object that holds multiple tiles, like a saved stamp. I could design my wall out of multiple tiles in a tilesheet, then create one tile object that encapsulates that stamp design. Is something like this possible in Tiled?

Thanks for your help.

1 Like

Alas, Tile Objects can only contain one tile. The Tile Stamp tool just places regular tiles, so while it can do the cosmetic part of what you need, it can’t automatically associate data with them. To do what you need in Tiled, you’d need to create a second tileset that’s an Image Set and has your wall sections already built in it (if your main tileset is structured appropriately, you could even reuse the same image), and use Tile Objects with that, with each “tile” being an entire wall section.

An alternative approach could be an Automap rule that detects wall segments and creates Objects with the relevant properties, but then you’d need to modify your importer or game code to treat tiles connected with these objects as something special rather than just regular tiles.

Hmm, regardless of whether Tiled could support this feature, which indeed it currently doesn’t, I heavily doubt this problem will actually occur. Did you already run into it or is it just a theory that it might happen?

If you’re placing your wall parts as tile objects and they all have the same Y position, then the only way it could happen that some are in front and some are behind the player is if the player also has the exact same Y position. In this case, the drawing order isn’t well defined. Normally, the player can’t walk into the wall, so the situation that it shares its Y position with the wall while also touching it is usually avoided.

Alternatively, you could place your wall on a tile layer rather than an object layer. If the wall is made up of parts this usually works quite well. In Tiled, tiles can extend beyond the tile grid so even higher walls can be done this way. And in this case, you only need to compare the Y position of the entire row of tiles against the player’s position, in order to know what to draw first.

It is happening in my project. In my code, all sprites are dynamically sorted based on their Y position. If my character is lower on the screen than a tile, they are rendered on top of it. If they are higher on the screen than a tile, they are rendered behind it. Problems happen if the player’s sprite is bigger than their collision volume, allowing parts of their texture to overlap with tiles in the tilemap even if the collision prevents them from entering that space. We end up with weird edge cases, where a player is beside a wall, and their Y position is between two of the tiles that make up the wall. If this happens, and the player’s sprite is overlapping with them, the lower tile can actually be drawn on top of the player sprite, breaking the illusion that the wall is one big object and not multiple tiles.

The reason I wanted a way to associate tiles is so that my engine can group the tiles into one bigger object and Y-sort with that object instead of its individual tiles, which would prevent this issue.

If I’ve not explained this well enough, I can grab some screenshots later and draw a wee diagram. It’s much easier to explain visually.

Your suggestion of using the tile layer to define individual objects is interesting, though it would result in an explosion in the number of layers being used. That might work for what I need, I will have to give that a try.

A screenshot may certainly help, but I think I know what you mean.

Another thought I had what could be a solution for you, is when you have another aspect to sort sprites on. For example, to sort all “static objects” before or after “player sprites” when their Y positions are the same.