Newbie here. How do I import a CSV file into Tiled

Hi!

Sorry for the complete newbieness, but I wondered how do I do this?
I have a text file, all comma seperated and I wondered how to import to Tiled.

Any help on this would be amazing
Many thanks

1 Like

Since a CSV does not by itself have enough information to make a map (tile size and tileset to use, among others), Tiled has no built-in way to import it. You will need to write your own custom map format that fills in that missing information somehow (e.g. by hard-coding it into the importer), but fills in the layer data from the CSV(s).

You can learn more about scripting here (JavaScript) and here (Python). JavaScript is the preferred way to script Tiled.

You can find example code for a custom map format in the API docs here. It only deals with writing (exporting). You can probably skip the write function and just focus on read. Here’s some example code for reading a CSV file in a custom format read() method:

let csvMapFormat = {
    name: "CSV with GIDs",
    extension: "csv",

    read: function(filename) {
        let file = new TextFile(filename, TextFile.ReadOnly);
        if(!file) return null; //Maybe show an error xP

        let map = new TileMap();
        map.tileWidth = 16; //or whatever it should be
        map.tileHeight = 16; //or whatever it should be
        map.orientation = TileMap.Isometric; //if your map is orthogonal, you don't need to set the orientation, ortho is default
        map.width = 0; //We'll set it later, once we know
        map.height = 0; //We'll set it once we know

        let layer = new TileLayer(); //assuming each CSV encodes a single-layer map...
        let tileset = tiled.open("path/to/tileset.tsx"); //assume the tiles all come from this tileset
        if(!tileset || !tileset.isTileset) return null; //show an error maybe xP

        while(!file.atEof()) { //while the file still has lines...
            let line = file.readLine();
            line = line.split(","); //line is now an array of values
            if(line.length > 0) { //if the line has some values in it
                map.width = Math.max(map.width, line.length);
                for(x = 0; x < line.length; x++) {
                    let tileID = line[x];
                    //process the tileID as needed... This example assumes it's a local tileset ID, which is probably wrong.
                    let tile = tileset.tile(tileID); //get the Tile from the tileset that has this ID
                    layer.setTile(x, map.height, tile);
                }
                map.height = map.height + 1;
            }
        }
        map.addLayer(layer); //Don't forget to put the layer into the map :]
        file.close();
        return map; //Don't forget to return the map so Tiled can show it to the user
    }
}

tiled.registerMapFormat("customCSV", csvMapFormat);
1 Like

Many many thanks for your superb reply.

I have much to learn, but at least it’s fun! :smiley:

Wayne

1 Like

can you explain more about that code? i mean what should i do about that? i am confused how to edit tiled map from file csv

1 Like

You need to edit that code with your desired map grid size (map.tileWidth, map.tileHeight), orientation (if orthogonal, remove the map.orientation line, otherwise set to the one you want), and with the absolute path to the tileset you want to use for the map. You’ll probably also want to update the format’s name near the top of the script, and the shortname at the bottom, so that you can recognise it more easily.
(I also just noticed I accidentally left the “CSV with GIDs” name from another script I wrote, this script does not handle GIDs.)

If your CSV format is anything other than comma-separated integers with one row of tile ID per line, then you’ll need to edit more of the code, and explaining how to parse arbitrary text files is beyond the scope of this thread.

After that, put it somewhere Tiled can see it, either in your global extensions directory, or in your Project’s extensions directory. After that, the format should appear as an option in the Open dialog, and you should be able to open these CSV files in Tiled. You will not, however, be able to Save them directly, since this format does not include a write(). You should save in another, more appropriate format, such as TMX or TMJ.

Edit: I can’t update the code in my old post anymore, but if you’re using Tiled 1.9.2+, you may want to replace tileset.tile(tileID) with tileset.findTile(tileID), which will return null instead of throwing an error when you pass in an invalid tile ID, which will make it easier to handle empty tiles.

1 Like