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

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);

Many many thanks for your superb reply.

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

Wayne