(SOLVED) How to get sprite x,y number of Tiled Sprite

I have a tiled sprite used for a graphic representation of an inventory.
I would love to get the x,y tile coordinates of which tile is clicked/hovered or anything on the tiled sprite.
For instance with a sprite/tile repeating 4x6 (24 tiles) times, the x,y should be in that interval.
So I’m not looking for pixel (x,y) but tile (x,y),

I have some workarounds in mind:

  1. Calculate them myself using CursorX(), CursorY() and knowledge of the sprite location and the repeating-sprites dimensions
  2. Place an invisible sprite on every cell with Object variable(s) I can use.

But maybe there’s an easier way?

I actually played around with cords quite a bit recently. Even tho I would not use it in your case, this would be my approach for your x,y:
x = ceil((CursorX() - Obj.X()) / tileWidth))
y = ceil((CursorY() - Obj.Y()) / tileHeight))

Nevertheless, my first intuition would be to use individual sprite objects (per slot/tile). That way you can easily assign your desired IDs, coordinates and state variables to your slot object. It also makes changing effects on hover/click or any other changes of visuals or data easy, but is less performant than using a singe tiled sprite.
The other approach would be to create a structure variable that acts as your data container. I have started using this more recently and it feels more performant but also more abstract. It requires basicly exactly what you thought of: knowing your coords, calculating the cursor position in relation to your object and so on. Whenever you want to apply graphical changes to a tile you will need to do that with a seperate object (as you already noted), because you cannot change individual tiles of your tiled sprite - just to keep that in mind.

Both approaches will work.

1 Like

Do you mean Tilemaps?

Tiled sprites do not have any internal coordinates.

If you do mean Tiled sprites, then Spite’s suggestion above is probably the best. You’re going to have to know the actual dimensions of your source sprite, then use that divide against the width or height of the tiledsprite object.

1 Like

Went with an overlay sprite on every tile of my tiledSprite, which I can also used for an selected indicator (using two “animations” one invisible one with selected icon). These sprites then have an object variable (index) of the tile they “sit” on.

It’s common for inventory to be made with 1 sprite that contains the animations for all of the items. You can then change the animations of the copies using a blank animation for empty slots.

As always, there are infinite solutions. Go with what you know. Learn. Improve. Enjoy.

2 Likes