Journal

Godot 4 Migration: Tile Set Scripting

Posted by:

Godot 4 Migration: Tile Set Scripting

This post is the latest in a series that covers our migration from Godot 3 to Godot 4.

The last post ended with a screenshot of the town of Ashwood, shown above. But what’s actually going on here? Well, “sprites” like the trees and rooftops seem to be mostly in the right place. But the Tile Map data is basically all gone. In fact, both Tile Maps and Tile Sets actually exist in the Scene data, but they have essentially had their critical properties wiped. Why?

Godot 4 drastically updated how Tile Maps work. After working with both systems, I can say that I much prefer the new approach –but it takes some time to learn:

https://docs.godotengine.org/en/stable/tutorials/2d/using_tilemaps.html

…and the Godot 4 upgrade tool doesn’t even try to convert the old Tile Set/Map data.

It would have been too much work to re-add every Tile Set and Tile Map by hand, so we decided to script the process. But how do you “script” editing Godot project resources? Why, just use Godot itself! Here’s our “Export Tileset” tool, still in Godot 3:

Basically, just a big “Export” button that scans for all Tile Set resources, loads them dynamically, then dumps their properties to a “tmap_export.json” file. Doing this in Godot (versus, say, Python) means we can just call “load()”, then “instance()”, and then we are done –no need to parse Godot’s weird text format manually! To put it in perspective, this script took about an hour to write.

Next, we have a similar “Import” tool (in Godot 4) that loads the exported json file, loads the equivalent Tile Set in Godot 4, and then programmatically sets the properties that are needed to make this work properly. After running this tool, we now have proper auto-tiling bitmasks! These are actually called “terrains” in Godot 4, but they look basically the same:

In addition, we also have our “physics” collision shapes now! Here’s an example of a Table:

This gets even more complicated: now, if you want to flip a tile horizontally, you’ll need an “Alt Tile”, which means that our Tile Set exporter actually needs to scan Tile Maps too, so that it knows which Alt Tiles to generate. Overall, it’s a little fiddly, but it wasn’t too hard to add.

Anyway, the upshot of all this scripting is, first, that it saves hours and hours of manual work, and, second, that you can now use the Terrain feature to “paint” paths onto Tile Maps, as shown below. (This was possible in Godot 3 as well, but it’s much more flexible in Godot 4.)

Our script for exporting and importing Tile Sets worked quite well –we were able to convert them all at once and then do a little bit of manual touch-up work. Next time, we’ll see how scriptable Tile Maps are.