Closed MikeMnD closed 4 years ago
The problem with a console.log or JSON.stringify is the circular reference: tileset.tiles[0].tileset.tiles[0].tileset… and so on… More on that: https://stackoverflow.com/questions/4816099/chrome-sendrequest-error-typeerror-converting-circular-structure-to-json
and the workaround is using this small package: https://www.npmjs.com/package/flatted
Then you can use it like:
console.log("--- tileset start ---");
console.log(Flatted.stringify(tileset));
console.log("--- tileset end ---");
That way the console in the Editor shows a text representation of the whole object.
// This crashes the editor // console.log(JSON.stringify(map));
In my tests this does not crash the editor. I'm consistently getting:
TypeError: Type error
Stack traceback:
write@file:///home/bjorn/.config/tiled/extensions/tiled-extensions/maptestformat.js:11
Though, I don't think I've tested this with the Tiled release yet, so it maybe it does crash when using Qt 5.12.6. In that case it would be fixed when we build against Qt 5.12.7.
// This also crashes the editor // console.log(Object.keys(map));
For me, this outputs:
qml: [objectName,asset,readOnly,fileName,modified,isTileMap,isTileset,width,height,size,tileWidth,tileHeight,infinite,hexSideLength,staggerAxis,staggerIndex,orientation,renderOrder,backgroundColor,layerDataFormat,layerCount,tilesets,selectedArea,currentLayer,selectedLayers,selectedObjects,objectNameChanged,property,setProperty,properties,setProperties,removeProperty,modifiedChanged,fileNameChanged,undo,redo,macro,sizeChanged,tileWidthChanged,tileHeightChanged,currentLayerChanged,selectedLayersChanged,selectedObjectsChanged,layerAt,removeLayerAt,removeLayer,insertLayerAt,addLayer,addTileset,replaceTileset,removeTileset,usedTilesets,merge,merge,resize,resize,resize,autoMap,autoMap,autoMap,autoMap,autoMap,autoMap,autoMap,autoMap,setSize,setTileSize]
// Both return empty string console.log(map.asset.fileName); console.log(map.fileName);
This may be surprising, but indeed this object does not have a file name. In this case, the map object is a temporary script wrapper around the map's data and map.asset
just points back to itself. Note that even if this object would have the file name, it may be the current file name rather than the file name you're saving as, which is available as the fileName
parameter.
// This works as intended - and returns the full path to the .tsx file console.log(tileset.asset.fileName);
Yes, though you don't need the .asset
part, which again points back at itself for tilesets.
console.log("--- tileset start ---"); console.log(Flatted.stringify(tileset)); console.log("--- tileset end ---");
That's a nice way to serialize! Just curious, but so far I didn't manage to get includes / requires to work. Did you find a way or did you just copy source code to your script file?
Just curious, but so far I didn't manage to get includes / requires to work. Did you find a way or did you just copy source code to your script file?
I've pasted the source code in a utils.js and used it from there after Tiled autoloaded the utils.js
var Flatted=function(a,l) ...
That's what I found about the topic: https://stackoverflow.com/questions/38057170/qt-qjsengine-import-js-files
The .import statement does not work with QJSEngine. QJSEngine is just a bare interpreter if you want to have some "import" functionality you might switch to QQmlEngine, which is built on top of QJSEngine: http://doc.qt.io/qt-5/qtqml-syntax-imports.html#qml-import-path
With QJSEngine you basically need to manually populate the js global object (and QtCreator sintax checker will not recognize the statements you use across the different files).
So maybe we can close this issue.
I've pasted the source code in a utils.js and used it from there after Tiled autoloaded the utils.js
Ah yes, that works of course!
QJSEngine is just a bare interpreter if you want to have some "import" functionality you might switch to QQmlEngine, which is built on top of QJSEngine
Hmm, I'm already using QQmlEngine
so just using this subclass does not appear to suffice for getting imports to work.
Anyway, yeah, let's close this issue.
Here a simple example script for making custom tilemap export: You can check the comments and try different ways of crashing the editor:
export_to_cusotm_tilemap.js
This is the log from the windows event log: Windows 10, Tiled Version 1.3.2