question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make use of TiledMapObjects' templates

See original GitHub issue

Tiled 1.1 adds support for object templates. A template is saved as an external file, and you can place templates in your tilemap similar to how you place tiles from a tileset.

In the saved XML, the path to the template file is stored as an attribute of the object element.

Suppose I placed a template object and renamed it. The corresponding XML would look like this:

<object id="6" template="../just/an/example.tx" name="Custom Name" x="168" y="483">

As you can see, many of the usual attributes (such as width, height, and type) are missing. In Tiled, I can still see these properties because it’s reading them from the template file.

I think the libgdx implementation would involve BaseTmxMapLoader.loadObject() getting the template attribute, loading the .tx file it points to, and filling out the missing properties using that template.

As for the .tx file itself, its structure looks pretty simple:

<?xml version="1.0" encoding="UTF-8"?>
<template>
 <object name="Default name" type="Example" height="48">
  <properties>
   <property name="some custom property" value="test"/>
  </properties>
 </object>
</template>

So it’s saved the same way objects are saved in .tmx files.

I can work around this limitation by detaching objects from their templates in my tile map, but it would be nice to be able to take full advantage of the Template functionality in Tiled.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:10
  • Comments:5

github_iconTop GitHub Comments

1reaction
OliPerraulcommented, Mar 4, 2020

Hi, I am making an educational resource using LibGDX, having the template functionality working without exporting with the template detached would help a lot.

1reaction
thomasjahodacommented, Jul 10, 2019

As workaround, the Tiled map editor provides export options to detach objects from the templates. This can be integrated in the project build life-cycle to generate the LibGDX-compatible maps automatically using the CLI.

Example command to export the map via the ‘Tiled’ editor CLI: tiled --export-map --detach-templates src/main/tiledmaps/test1.tmx src/main/resources/maps/test1.tmx The first map file is the source, the second one is the target.
The target file does not reference the template file anymore and has all properties from the template.

Example Gradle task to export all maps on-demand (written in Kotlin Gradle DSL):

task("exportMaps") {
    val inputDir = "$projectDir/src/main/tiledmaps"
    val outputDir = "$projectDir/src/main/resources/maps"
    inputs.dir(inputDir)
    outputs.dir(outputDir)
    doLast {
        logger.info("Exporting 'Tiled' maps from directory $inputDir to $outputDir")
        val tmxFiles = file(inputDir).listFiles { dir, name -> name.endsWith(".tmx") }
        @Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
        tmxFiles.forEach {
            logger.info("Exporting ${it.name}")
            exec {
                val outputFile = file(outputDir).absolutePath + "/" + it.name
                commandLine = listOf("tiled", "--export-map", "--detach-templates", it.absolutePath, outputFile)
            }
        }
    }
}
tasks.clean {
    delete(fileTree("$projectDir/src/main/resources/maps") {
        exclude(".gitignore")
    })
}
tasks.processResources {
    dependsOn("exportMaps")
}

For reference, read https://www.mapeditor.org/2018/09/18/tiled-1-2-0-released.html for Info on the detach-preferences. Search for:

For this reason several Export Options were added to the Preferences:

However… implementing actual object template support into the classes should not be much effort. I just didn’t wanna fork it and use a custom libgdx version for my project while I wait for a pull request to be accepted and the next libgdx version to be released.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Templates — Tiled 1.9.2 documentation
Using Templates . Any created object can be saved as a template. These templates can then be instantiated elsewhere as objects that inherit...
Read more >
Collision and FPS Optimization - Game Building Help
I'm making a platformer and using tiled map objects for the ... But I also use it for solid collision testing. ... FPS...
Read more >
How To Make A Side-Scrolling Beat Em Up Game Like Scott ...
Hit Command-N and create a new file with the iOS\Cocos2D v2.x\CCNode Class template. Make it a subclass of ActionSprite and name it Robot....
Read more >
Game Development for Human Beings - GameDev Academy
In this tutorial we'll create a basic template you can use to make your ... and group properties, those properties must be defined...
Read more >
GDevelop - Create games without programming - Open ...
What makes GDevelop unique and so easy to use are the events. Events are a powerful way ... Make RPG-like maps with the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found