Search Unity

Resolved Get a tile index? Why is this so difficult???

Discussion in '2D' started by CrimsonLace, May 5, 2023.

  1. CrimsonLace

    CrimsonLace

    Joined:
    Sep 7, 2019
    Posts:
    8
    I would figure this would be a braindead easy thing to get but for some reason, I am finding absolutely no option for this anywhere. All I want to do is get the index of the tile in the palette for any given tilemap position. Why is there no option for this? No, I don't care about any of the super fancy extra features, just gimme the hot dog... err I mean tile index... I code my own physics and coldet and am about to just roll my own tile mapping functionality too if I can't figure this out.

    I've tried using
    Code (CSharp):
    1. GetTile
    and it returns a string that is almost useful, but using
    Code (CSharp):
    1. Parse
    throws an exception because of all the excess nonsense in the result, and
    Code (CSharp):
    1. TryParse
    always fails. Always. Using
    Code (CSharp):
    1. Substring
    to try to get the tile index partially works, but only until it bumps into a character in the result that causes it to fail. I was able to get the desired result as long as the tile range was 10-99 because I limited the substring to 2 places and knew where the index number in the result started (started at 6, since the tileset was called "tiles" and Unity adds the _ before the number).

    This is such a simple little detail and I have no idea why it's not easier to figure out. The documentation has been of no help at all, google is of no help at all, youtube is of no help at all. WHAT AM I MISSING...

    For reference, I'm using 2021.3.5f1.

    EDIT: OK so I figured I should put in the exact code I'm using thus far. So to get the tile I'm looking at, I've got this code:
    Code (CSharp):
    1. sGetVal = GameWorld.GetTile<Tile>(GameWorld.WorldToCell(CheckPosition)).ToString();
    where "GameWorld" is the tilemap and "CheckPosition" is a Vector3. The result I get is
    Code (CSharp):
    1. tiles_34 (UnityEngine.Tilemaps.Tile)
    and there's the 34 in there, which is the value I need, but all of the parsing functions fail because of the extra baggage, and I should not have to go to such extremes to simply get this index number. Just. Give. Me. The. Index. Number. Already.
     
    Last edited: May 5, 2023
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    You're blaming the engine, docs, google here but your question is far from clear. Maybe you can be more concise on what you require.

    What does this mean? What's an index of the tile palette?

    GetTile does not return a string, it returns a Tilebase.
     
  3. CrimsonLace

    CrimsonLace

    Joined:
    Sep 7, 2019
    Posts:
    8
    I am not sure how I could make my question any more clear. I am being as concise as humanly possible. I don't post questions like this unless I am completely stumped on something. This should be something very easy to figure out and yet it isn't. :(

    The index of the tile in the tile palette is... the index of the tile in the tile palette. In this case, it's 34. It's tile 34. I am not sure how this could be made any more clear. The tile palette window doesn't even show you the index of the tile you've selected, which also doesn't help, nor does the grid show you the cell number you're plotting. I really wish these were implemented features... they would be nice quality-of-life improvements.

    ...which does not help at all. It gives me nothing to work with. That's why I did ToString() to try to parse it to get the index. The value's right in there because of how the tile palette is set up. Without the ToString() there is nothing I see that helps.

    I honestly have no idea how this could be made any more clear. I stated it literally as clearly as possible. I want to check for the index of a tile on the grid. The numeric value of the tile index inside the tile palette. That's all.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    Right, so you've edited your post since my post to state what you want. That extra information wasn't there so look at the posting times. Anyway, thanks.

    Your question isn't related to 2D nor tilemaps but is scripting and asking how to parse a string that contains a number and turn that number into an integer.

    You can use regular expressions (RegEx) to do this.
     
  5. CrimsonLace

    CrimsonLace

    Joined:
    Sep 7, 2019
    Posts:
    8
    No, this is literally related to the tilemap functionality. I only tried parsing the string because I could not figure out how to get the index.
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    Why are you arguing? This is the name of the object. It could be a GameObject or any random string. My point is this isn't a Tilemap specific thing, it's the name of an asset but it's just a string that you choose. To be very, very clear though: there is no index. Those could be named anything.

    This kind of question would normally be asked on the scripting forum asking about parsing strings. My answer is above btw, it's how you parse strings.
     
  7. CrimsonLace

    CrimsonLace

    Joined:
    Sep 7, 2019
    Posts:
    8
    I'm not trying to argue, I'm trying to figure out basic functionality that should be super easy. So I DO have to look at the name of the tile? Ugh, that's gonna be inefficient. :(

    EDIT: Again, this is not a scripting issue. My intention was never to have to parse a value. I should not have to do that. That's just plain ridiculous. My intention was simple to get an index value from the tilemap. That has nothing to do with string parsing... or at least it shouldn't. There's clearly some crossed wires here.
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    So your question actually is: is there an index for any tile asset I create? The answer is no.

    Could you explain why you need it? Maybe there's another solution for what you want.
     
  9. CrimsonLace

    CrimsonLace

    Joined:
    Sep 7, 2019
    Posts:
    8
    Because I code my own physics and coldet, so I need numeric values from the tilemap data that refer to an index in the tile palette. If this isn't possible then fine, but it's very wasteful to have to process a string to do what should be able to be done with numeric data directly. And again, I'm not trying to be argumentative, but I think there's some miscommunication here. Maybe I'm asking for something that simply doesn't exist in Unity.
     
    MelvMay likes this.
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    It doesn't exist because it's not something that has any use-case we've ever known. A tilemap isn't set-up with logical indexes or anything.

    In theory you can setup your own tiledata which includes your own index/id if you wanted.

    All objects use a numeric InstanceID you can retrieve that may be suitable depending on what you're doing specifically but that's like the name, it's a stock Unity object thing.
     
  11. CrimsonLace

    CrimsonLace

    Joined:
    Sep 7, 2019
    Posts:
    8
    ...ugh... ok. Well, thanks for your time, it's much appreciated. I wonder why no one's ever heard of accessing map data directly though... it's pretty standard procedure for 2D development. Oh well. :(
     
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,492
    But it isn't map data. It simply doesn't exist.

    I've heard of it, it's a way of accessing stuff such as an old-school color palette or a tile index but it simply doesn't apply here. It's not an array of tiles.

    Again, you seem to want a numeric value that applies to each tile of that asset type in your tilemap, I thought I gave it to you above (maybe) so not sure what else there is in your requirement you might need.
     
  13. CrimsonLace

    CrimsonLace

    Joined:
    Sep 7, 2019
    Posts:
    8
    Well, sort of... InstanceID isn't quite the same thing as what I was looking for, but maybe I could roll a system together to emulate what I need... but it looks more like I'm going to have to just go old school manual style on this one. Well, at least now I know that what I was looking for simply doesn't exist. I can deal with that. It's the "not knowing" part that's the challenge. Thanks again for your help.
     
    MelvMay likes this.