Log In  

To use Tiled to create a PICO-8 map export from Tiled to a LUA file called "tiled.lua".

NB: You may need to make sure it's LF and not CRLF as current PICO-8 has issues including files using CRLF.

The following code will export the map and any spites in the cart to "tiled.p8".

function parsetiled()
#include tiled.lua
end
tiled=parsetiled()
layer,i=tiled.layers[1],1
for y=0,layer.height-1 do
 for x=0,layer.width-1 do
  mset(x,y,layer.data[i])
  i+=1
 end
end
cstore(0x0000,0x0000,0x3000,'tiled.p8')
P#73611 2020-03-02 16:15

NB: To just write to the executing p8 file use cstore(0x0000,0x0000,0x3000)

P#73612 2020-03-02 16:55

I don’t know why pico8 has a built-in command to export/import sprites but not map data! It could help to see the whole map in a picture viewer, and of course use other tools to edit the map.

Would you mind adding some instructions to your post, or maybe screenshots? I haven’t used Tiled much yet, so I don’t know what «tiled.lua» should contain (if it needs to exist before running your code) and what to do with it after exporting.

P#75011 2020-04-19 20:31

Hey @merwok

DISCLAIMER: I'm very new to games programming, and am not really familiar with Tiled.

All you need to do is created your map in Tiled, and then select "File" > "Export As..." (or press Ctrl+Shift+E) and make sure to select "Lua files (*.lua)" as your "Save as type".

You don't really need to worry about the format of the LUA file that is exported, but of course open it up and have a look, out of interest. It is essentially just some code to return a large table containing all the data about your map.

In the code above (line 2) we assume that you exported to a file called "tiled.lua". This file should be placed in the same folder as the p8 file you are running.

The code assumes that your map only has one layer. If you have many layers to store multiple maps you will need to amend "layers[1]" in the line:

layer,i=tiled.layers[1],1

... to the number of the layer that you want to import, e.g: if you want the third layer instead use:

layer,i=tiled.layers[3],1

I think, to be honest, if you:

  • Create a p8 file and add the code above
  • Make your map in Tiled
  • Export it in LUA format to a file called "tiled.lua" in the same folder as your p8 file
  • Run the p8 file in PICO-8
  • Open the newly created file "tiled.p8" in PICO-8

.. you'll quickly see how the process works, and what you may want to tweak to get it to do exactly what you want.

I'm also happy to help further and answer any questions I can.

P#75033 2020-04-20 10:03 ( Edited 2020-04-20 10:10)

NB: The new PICO 8 version 0.2.0c should have fixed the issue with including files with CRLF endings. If you're using 0.2.0c then all should be good.

If you're still using 0.1.12c you'll need to convert the line endings on your exported file from CRLF (Windows format) to LF (Unix format) as 0.1.12c has an issue with #including files with CRLF endings.

P#75036 2020-04-20 10:09 ( Edited 2020-04-20 10:20)

Thank you, the instructions are clear now!

Do you have the reverse code to export cartridge map data to a lua file Tiled can import? Then we’d have the equivalent of import/export spritesheet!

P#75046 2020-04-20 16:24

@merwok You're welcome. I hope it's of some help.

It looks like Tiler won't actually open LUA files, but it would probably be pretty simple to use PICO-8's printh function to export to Tiler's native TMX format.

P#75103 2020-04-21 09:32

OK, this code will actually dump the map in TMX format.

I've added some comments to try to explain what I'm doing.

If you add that code to a P8 file with sprites and a map and run it it will dump the map in TMX format to a file called "YYMMDDHHMMSS.tmx.p8l" in the current working directory (as well as the clipboard, in case you'd rather just paste into a new file and amend in one go).

Remove the ".p8l" extension from the file to leave "YYMMDDHHMMSS.tmx". This will happily open in Tiler.

Unfortunately there is still a little work to do to get it working properly in Tiler.

You'll need to export the sprites from your P8 file using:

export myfile.png

You should then edit the TMX file and amend the image's source attribute from "YYMMDDHHMMSS.png" to the name of the file you've just exported.

If you then open that file you should see your map correctly displayed.

P#75105 2020-04-21 10:22

I've been playing about with writing a Javascript extension for Tiled, to both read and write map data from a P8 file.

Writing/exporting the data is simple, but unfortunately it looks like the Javascript API is still a little lacking, so unfortunately I'm banging my head against a wall trying to get reading to work properly.

While trying to find a resolve I've just discovered this issue from a mere 2 days ago, which refers to this extension written in Python.

You may want to give that a try.

As far as my extension, which currently works for saving data to a p8 file:

  • See Scripted Extensions to find where you should store your .js file (Or, in Tiled, go to "Edit" > "Preferences" > "Plugins" > "Open..").
  • Copy the following into a file called pico8.js in that folder
  • Restart Tiled (if it is open)
  • You will now be able to use "File" > "Export As..." and select "PICO-8 Map Format".
var customMapFormat = {
    name: "PICO-8 Map Format",
    extension: "p8",
    write: function(map, fileName) {
        var layer = map.layerAt(0);
         var m = 'pico-8 cartridge // http://www.pico-8.com\n'
            + 'version 21\n'
            + '__lua__\n'
            + '-- Exported from Tiled\n\n'
            + '__gfx__\n\n'
            + '__map__\n';
        for (y = 0; y < layer.height; ++y) {
            for (x = 0; x < layer.width; ++x) {
                m += (layer.cellAt(x, y).tileId > 0)
                    ? layer.cellAt(x, y).tileId.toString(16).padStart(2, 0)
                    : '00';
            }
            m += '\n'
        }
        var file = new TextFile(fileName, TextFile.WriteOnly);
        file.write(m);
        file.commit();
    },
}

tiled.registerMapFormat("pico8", customMapFormat)
P#75111 2020-04-21 14:59 ( Edited 2020-04-21 15:11)

Sadly my version of Tiled is missing Python support.

Thanks for the scripts! Will be quite useful for a bigger adventure game.

P#75174 2020-04-22 22:11

Yes, I updated and have also been seeing problems. I think I have multiple versions installed - in one the python.dll complains in the other it doesn't...

You can still use my Javascript extension if you want. It will at least make exporting easier than the method in the initial post.

P#75214 2020-04-23 14:29

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-29 13:30:57 | 0.020s | Q:21