New Command token: %objectid

The addition of an %objectid Command token that works similarly to the %objecttype would be very useful to my project.

Another “nice to have” would be %layername

Use-Case explanation:

TL;DR: I need to match up map objects to unique database identifiers in an external game editor program.

I’m working on an RPG where I use Tiled objects to represent doors, loot, enemy placement, scriptable events, etc. All of these objects have a lot of associated data that I felt was too cumbersome to embed in the maps themselves. The Command option works great for invoking my game editor, which is a front-end that validates and writes that associated data to a SQLite database. The problem is linking up the map objects to the unique database identifiers. The only thing I can automatically pass to the game editor right now is the %objecttype, so my workaround was to add a button to the game editor called “Generate unique ID” which copies the newly-generated database ID to the clipboard and urges the user to paste it into back to Tiled’s object type property.

This way when I use the command on that object again, it passes the identifier through the %objecttype and I know to open up the existing object for edit instead of creating a new one. Unfortunately, I’m very forgetful, so this leads to situations where I forget to copy back the db identifier to the object type field, and it becomes a disconnect between the map and the game data.

If my understanding of the documentation is correct, the combination of an object id and the map itself should be unique and would be a good way for me to link up the map and the database. A nice feature of the object id is that it always increments, even when an object is deleted. This way if I delete an object on the map and create a new one, it won’t re-use the identifier and attempt to load up the old data.

There’s always the problem of orphaned data on the database side, but I imagine I could write a program to extract all of the unique object ids from the tmx and remove any data from the db that doesn’t belong to those objects.

The %layername could save the step of figuring out which type of object I’m creating, as long as I put each object type on its own layer with a defined name. Not a big deal to have that, but it would be neat.

Thanks for your consideration!

A perfectly valid request. My only gripe is the current implementation for these variables and the fact that only such a limited subset is available (the ones that were requested, really). In fact, this is yet another feature that could essentially be replaced as soon as Tiled would be scriptable.

For now, I’ll do a patch to add the ones you requested. Making Tiled scriptable will be something for later this year!

I’m very grateful for your response! Thank you for providing such a great tool for game development.

You’re welcome and thanks for even becoming a patron! Here’s the addition:

https://github.com/bjorn/tiled/commit/e0d867fd80cfaf7b26d8ef2719734df8993d7039

The snapshot builds should update soon.