Animated tiles question

Hi, a question about how Tiled plays animated tiles on a map.
Tiled goes through the entire map and checks if each one of the tiles is an animated tile, it also checks if the tile should advance in the animation.

pseudo code
walk through the tilemap
check if the tile ID is an animated tile
check if the tile should advance in the animation

This is how it works?

What is your goal in asking? Are you looking to debug something, or are you looking for guidance on implementing tile animations in your own code?

The actual code is here:

As you can see, it does just scan over the whole map and call advanceAnimation(time) for every tile, which does nothing for non-animated tiles. advanceAnimation() returns a boolean in case it was animated, and if more than one time was animated in total, Tiled refreshes the view.

As you can also see in the comments, this code could be improved by keeping a list of all the animated tiles and only looping through that. In my game engine, that’s what I do. When the map is loaded, I scan through it once to find all the instances of each animated tile. I end up with a list of different animations, each of those has a list of instances using that animation. Then, when I update the animation, I only need to update the timers for those general animations, calculate what frame(s) they’re on, and update all their instances, which is much more efficient than scanning the entire map and updating potentially thousands of independent timers.

I made an implementation for my game but it is different from Tiled and the one you mention.
I only asked to see if I could improve my implementation but I think it is good enough after seeing what Tiled does and the improvement it proposes in the comments.

It’s worth mentioning that Tiled’s implementation is quite poor and causes lag/poor responsiveness on maps with many animated tiles, it’s definitely not an approach that a game should take.
My method does well even on maps made up entirely of animated tiles, though if there were a very large number of unique animations, it would probably struggle too. My game’s bottlenecks lie elsewhere.

If your method performs well, then it’s good and you don’t need to worry about optimizing it, even if you’re not confident in the approach :] Don’t optimize before you need to. That way, when you do find you need to make some optimizations, you’ll know where to start, you’ll be able to actually compare performance, and you’ll be looking at simple, easy-to-read code instead of something needlessly complex due to premature optimizations.

Hmm, Tiled only iterates the tiles in each tileset and not all tiles on the map. So unless you have a huge amount of tiles, there shouldn’t be a big performance issue. The more likely cause of lag in Tiled’s case is that it uses software rendering (at least by default, and its hardware rendering is going through a very thick abstraction layer so it doesn’t help much), and any change as a result of a tile animation triggers a complete repaint of the map view.

Usually Tiled manages to feel fast because it limits the repainted areas when something changes as well as when scrolling, but such an optimization is not made for animated tiles.

1 Like