Is there some examples of javascript?


tiled.log("I'm loading.");

function onConnectHandler(asset) {
    // Do something...
        "===================================" + 
        "action: connect\n" + 
        "fileName: " + asset.fileName +
        "modified: " + asset.modified + 
        "isTileMap: " + asset.isTileMap + 
        "isTileset: " + asset.isTileset +
        "caller: " + onConnectHandler.caller.toString() + 

function onDisConnectHandler(asset) {
    // Do something...
        "===================================" + 
        "action: disconnect\n" + 
        "fileName: " + asset.fileName +
        "modified: " + asset.modified + 
        "isTileMap: " + asset.isTileMap + 
        "isTileset: " + asset.isTileset +
        "caller: " + onDisConnectHandler.caller.toString() + 

//A new asset has been created.

//An asset has been opened.

//An asset is about to be saved. Can be used to make last-minute changes.

//An asset has been saved.

//An asset is about to be closed.

//The currently active asset has changed.

var customMapFormat = {
    name: "Custom map format",
    extension: "custom",

    write: function(map, fileName) {
        var m = {
            width: map.width,
            height: map.height,
            layers: []

        for (var i = 0; i < map.layerCount; ++i) {
            var layer = map.layerAt(i);
            if (layer.isTileLayer) {
                var rows = [];
                for (y = 0; y < layer.height; ++y) {
                    var row = [];
                    for (x = 0; x < layer.width; ++x)
                        row.push(layer.cellAt(x, y).tileId);

        return JSON.stringify(m);

tiled.registerMapFormat("custom", customMapFormat);

tiled.log("I'm loaded.");

When I open Tiled, the console show:

Evaluating '~/Library/Preferences/Tiled/extensions/plugin.js'
I'm loading.
I'm loaded.

Then, I creat a new map or open an exists tmx file, but other nothing happen.
Maybe I used it wrong.

The code of “customMapFormat” is in effect.

See the documentation on how to connect to signals. In particular, the disconnect function is to disconnect a function from a signal, rather than to connect a “disconnect handler”.

For your particular snippet, I’m getting the following error when doing anything that emits a signal:

[...]/tiled/extensions/plugin.js:5: TypeError: Type error

This appears to be related to trying to access onConnectHandler.caller. Where did you find this property and why do you need it?

Edit: I realized just now, that this error is printing to the terminal rather than being visible in the Console or Issues views in Tiled. I’ll look into fixing that up.

When I comment the line containing ‘caller’, the code run correct.

Hmm, it appears that there is no API for me to improve the error reporting in this case. It’s because I just emit the signal from C++ and at that point don’t even know whether a JavaScript function is connected to it. The calling of the function is handled by Qt, and there seems to be no way for me to check if an error happened and to report this error in Tiled user interface. :-/

Maybe it’s the problem you’re facing.

Other way, Can u redirect stderr to a log file?

That Qt bug is about adding the throwError function, which is available in the meantime (since Qt 5.12) and I’m already using it. But, that’s when I throw an error, whereas the problem here is reporting an error thrown elsewhere, without having access to the error object.

Redirecting stderr to the Console window might work, but I’m not sure how to achieve that. Since this is a Qt message though, we can use qInstallMessageHandler to intercept it. I’ve done this with the following change: