Edit: I should probably clarify that my goal is for both Tiled and my custom editor to be usable with the same files, while preserving data when converting. This is because some people on the team prefer Tiled (and also my editor is missing some important features, such as actually creating maps). The particular issue I’m having is that the object IDs get “normalized” when importing into Tiled, which is quite bad since code may refer to the IDs.
The id is set by Tiled when creating the object and there is no way for extensions to control it. (At least not without getting creative with the object creation order, which would be rather silly.)
You should not be referring to objects by ID in your code, since the IDs will depend on creation order, etc. They exist only to disambiguate different objects in a map, and should not be relied on to be anything but different for different objects. If you want to use Tiled’s IDs internally in your code, you should determine dynamically the IDs that refer to the objects you’re interested in. If you need permanent, consistent identifiers that you can hard-code, you should use custom properties, not the object IDs.
Even if the ID is non-editable, I suggest adding a “Normalize IDs” feature that allows the Tiled to recalculate and rearrange existing IDs, which can eliminate some confusing ID numbers.
+1 for an action to normalise IDs for objects and layers. When deleting and creating things, the IDs get quite high and non-sequential. Though it doesn’t really matter, I really don’t like seeing those high IDs (they can easily get into the thousands for a map with only a few layers or objects) and would much prefer that they be tidy and sequential. Sometimes I go as far as fixing them by editing the files in a text editor, but obviously that’s risky.
So far I had decided to make the IDs read-only because Tiled makes sure there are no conflicting IDs and it couldn’t really do this when scripts can arbitrarily change object IDs. Since IDs are managed by Tiled itself, there is also no user-action to change them nor are they covered by undo history.
However, indeed custom formats should be able to set the IDs. I wonder if I should make the property writable, possibly only while a map is being loaded? For example I could make it possible to change this property as long as no undo history is involved. The reader would be responsible for not creating conflicts in this case (which are no fatal error though, I think the worst that can happen is that object references can become ambiguous).
It’s interesting that you’ve essentially written a special-purpose Lua parser in JS to be able to load back a Lua file. Your Lua format looks very similar to Tiled’s own Lua export, btw, making me wonder why you wouldn’t just use that instead. But I see it features some game or engine-specific customization.