Skip to content

Commit 631680b

Browse files
authored
Merge pull request #9 from konsumer/feature-7
allow relative or blank projectRoot, for #7
2 parents fc10605 + 19ac422 commit 631680b

4 files changed

+27
-14
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ If you prefer watching check the video in YouTube:
7070

7171
[How to export from Tiled To Godot 3.2](https://youtu.be/4jSFAXIa_Lo)
7272

73-
After you open a Tilemap or Tileset you need to add this custom property:
74-
`"projectRoot" : string` , then set the value to the root project folder of your Godot project.
75-
For example: `D:/work/GodotProjects/game_one/`
73+
### setting `res://`
74+
75+
The exporter needs to know where `res://` is. By default, it's in the same directory where the Tiled files are being saved. You can override it with a tile/map custom property `projectRoot : string` that is either relative to the file you are exporting (starting with a `.`), or absolute (not recommended.)
7676

7777
**_!!! Pay attention to the "/" instead of standard windows "\\".
7878
Single Backslash "\\" is used for escaping special symbols._**

export_to_godot_tilemap.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ class GodotTilemapExporter {
66
this.map = map;
77
this.fileName = fileName;
88
// noinspection JSUnresolvedFunction
9-
this.projectRoot = this.map.property("projectRoot");
10-
if (!this.projectRoot) {
11-
throw new Error("Missing mandatory custom property: projectRoot!");
12-
}
13-
this.projectRoot = this.projectRoot.replace('\\', '/');
9+
this.projectRoot = getResPath(this.map.property("projectRoot"), fileName);
1410
this.tileOffset = 65536;
1511
this.tileMapsString = "";
1612
this.tilesetsString = "";

export_to_godot_tileset.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ class GodotTilesetExporter {
66
this.tileset = tileset;
77
this.fileName = fileName;
88
// noinspection JSUnresolvedFunction
9-
this.projectRoot = this.tileset.property("projectRoot");
10-
if (!this.projectRoot) {
11-
throw new Error("Missing mandatory custom property: projectRoot!");
12-
}
13-
this.projectRoot = this.projectRoot.replace('\\', '/');
9+
this.projectRoot = getResPath(this.tileset.property("projectRoot"), fileName);
1410
this.spriteImagePath = this.tileset.image.replace(this.projectRoot, "");
1511
this.shapesResources = "";
1612
this.shapes = "";

utils.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,25 @@ function logf(data){
88
}
99
function logk(data){
1010
console.log(Object.keys(data));
11-
}
11+
}
12+
13+
function getResPath (projectRoot, outputPath) {
14+
const p = outputPath.split('/').slice(0, -1)
15+
// check for projectRoot
16+
// If projectRoot is not set, set it to current file's location
17+
if (!projectRoot) {
18+
projectRoot = p.join('/')
19+
}
20+
projectRoot = projectRoot.replace(/\\/g, '/')
21+
// Use it as absolute, if it doesn't start with ".", relative if it does
22+
if (projectRoot[0] === '.') {
23+
const out = p
24+
projectRoot.split('/').forEach((segment, i) => {
25+
if (segment === '..') {
26+
out.pop()
27+
}
28+
})
29+
projectRoot = out.join('/')
30+
}
31+
return projectRoot
32+
}

0 commit comments

Comments
 (0)