Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defold export Z value from layer order #3214

Closed
thejustinwalsh opened this issue Dec 17, 2021 · 12 comments
Closed

Defold export Z value from layer order #3214

thejustinwalsh opened this issue Dec 17, 2021 · 12 comments
Labels
missing feature It's not just a feature, it's a feature that really should be there!

Comments

@thejustinwalsh
Copy link

After exporting to a defold tilemap the user must open the tilemap in defold and assign z values to each layer to have it match the order that was defined in the Tiled project to ensure the tilemap renders correctly.

The z value that a user would use here is somewhat specific to their project but generally Defold defaults to -1 to 1 for z range with -1 being far and 1 being near.

What would be the best approach to allow for the user to export directly from Tiled without the need to reset the z values of each layer on each export?

Considerations that come to mind are:

  • Any sprite objects that need to render between tiled layers could effect the desired z value of each layer
  • Defining a range for each layer... ie.e 0.1, 0.2 0.3 vs 0.0001 0.0002 0.0003

Would CustomProperties be a good solution here to override a z value and just have each layer default to some sane export z value based on it's order ad listed in the project layers?

@bjorn
Copy link
Member

bjorn commented Dec 17, 2021

Would CustomProperties be a good solution here to override a z value and just have each layer default to some sane export z value based on it's order ad listed in the project layers?

Ideally this is what we could implement. We just need to know what are sane defaults, as well as what happens to those defaults if one or more of the layers is manually assigned a Z value. It could either have no effect at all, or the ones that are left undefined somehow automatically sit between the others. The latter is what is implemented for the GameMaker export:

static void autoAssignDepth(const std::vector<std::unique_ptr<GMRLayer>> &layers)
{
std::vector<GMRLayer*> flattenedLayers;
collectLayers(layers, flattenedLayers);
const auto end = flattenedLayers.cend();
auto current = flattenedLayers.cbegin();
auto findNext = [&] (std::vector<GMRLayer*>::const_iterator start) {
return std::find_if(start, end,
[] (GMRLayer *layer) { return layer->userdefinedDepth; });
};
auto next = findNext(current);
int depth = 0;
int depthIncrement = 100;
if (next != end)
depth = (*next)->depth - std::distance(current, next) * depthIncrement;
for (; current != end; ++current) {
if (current == next) {
next = findNext(current + 1);
depth = (*current)->depth;
if (next == end) {
depthIncrement = 100;
} else {
if ((*next)->depth < depth)
Tiled::WARNING(QStringLiteral("YY plugin: User defined layer depths are not adequately spaced, results in game are undefined."));
const int diff = (*next)->depth - (*current)->depth;
const int dist = std::distance(current, next);
depthIncrement = diff / dist;
}
} else {
(*current)->depth = depth;
}
depth += depthIncrement;
}
}

Since I don't have much experience with Defold, hopefully you can help determine what would be a good approach to assigning defaults. :-)

@bjorn bjorn added the missing feature It's not just a feature, it's a feature that really should be there! label Dec 17, 2021
@thejustinwalsh
Copy link
Author

thejustinwalsh commented Dec 17, 2021

Good question! :)

The War Battles Tutorial tutorial for Defold mentions setting your character to 0.1 to render above the tilemap, so leading with that I would recommend a range between [-1.0, 0.1).

Would it be counter-intuitive to start in the negative range counting up toward 0?

If the user can set a custom depth value on their lowest layer then we count up to determine the next value as in the implementation of the GameMaker exporter, it doesn't immediately seem like a bad idea.

Some ideas:
Something like -0.9 + 1000/1000 or x + y/1000 where x = starting custom depth, y = increment by 1 or 10?
If counting by 1 that is 1000 layers between -0.9 and 0.1 and if counting by 10 that is 100 layers.

Open to additional lines of thinking here though, not 100% confident in my suggestion.

@bjorn
Copy link
Member

bjorn commented May 24, 2022

Just a note, I noticed the "defoldcollection" plugin does already set some Z value by default. Maybe you could just switch to that plugin? Or is there a reason you need (or needed) to export only a .tilemap asset?

@thejustinwalsh
Copy link
Author

Just a note, I noticed the "defoldcollection" plugin does already set some Z value by default. Maybe you could just switch to that plugin? Or is there a reason you need (or needed) to export only a .tilemap asset?

I will check out the plugin and see if it works for my needs. TBH, I forgot why I was exporting the .tilemap asset directly, and need to revisit the project. Thanks for the heads up!

@bjorn bjorn closed this as completed in 54bb216 Sep 16, 2022
@bjorn
Copy link
Member

bjorn commented Sep 16, 2022

Ok, I've just added a simple assigning of incrementing z values to the Defold plugin, plus a way to override it with a custom property.

I've also extended both the Defold and the Defold Collection plugin with a way to set the tile_set property of the exported tilemaps. The Defold plugin only supports a single tileset per map, and you can specify it using a custom tile_set property on the map. The Defold Collection plugin supports multiple tilesets, and the tilesource matching each tileset can be set using a custom tilesource property on the tileset.

@thejustinwalsh
Copy link
Author

Amazing! Thanks for all your hard work!

@Insality
Copy link

Insality commented Sep 21, 2022

Hello, @bjorn !
Thanks for the plugin, but there is one issue with Z layers:

As said in docs:

A custom float property named “z” can be added to set the z value for each tile layer. By default, the layers will be exported with incrementing z values, so you only need to set this property in case you need to customize the rendering order.

But this code only in defold plugin, not defoldcollection. On --export-map it uses defoldcollection. And the custom property "z" for layer is not working now

@bjorn
Copy link
Member

bjorn commented Sep 21, 2022

But this code only in defold plugin, not defoldcollection. On --export-map it uses defoldcollection. And the custom property "z" for layer is not working now

This change didn't affect defoldcollection, because the original author of the defoldcollection plugin had already coded a rather more advanced approach to automatically assigning z values. If needed I can of course add a way to override the z values also for that plugin, though I'm unsure how one overridden z value should affect other automatically assigned ones.

On --export-map it uses defoldcollection.

Which plugin is used on --export-map depends on the file extension. If you export to a .collection file, it should pick defoldcollection and if you export to .tilemap, it should pick defold. Assuming you have both plugins enabled.

@Insality
Copy link

@bjorn Oh, thanks for your explanation! Yea it little bit more clearly now.

Yeah, i'm expected I'm able to override z value of top layer layers in defoldcollection like it's done in defold plugin.
My case was: make tilelayer for example on layer 10 to draw this layer over all my game objects (Defold renderer and Z ranges can be adjusted inside Defold, not only [-1..1])

Do I need make another issue for that?

@bjorn
Copy link
Member

bjorn commented Sep 22, 2022

Do I need make another issue for that?

No, I've just added the ability to override the automatically determined z value per-layer (using a custom "z" property). But I didn't test it yet, so it would be great if you could give the resulting builds a try: https://github.com/mapeditor/tiled/actions/runs/3108223130. I'll still need to adjust the documentation before pushing to master.

@Insality
Copy link

@bjorn thanks, it works now! ❤️

It's work for tileset layers. But I need correct myself - in Defold for tilesets Z property is available only in [-1..1]
There are some cases in Groups in *.defolcollection but it's can be handled by user export script now as I think

@bjorn
Copy link
Member

bjorn commented Sep 27, 2022

@Insality I've pushed that change to master, thanks for testing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
missing feature It's not just a feature, it's a feature that really should be there!
Projects
None yet
Development

No branches or pull requests

3 participants