How do I get iTilemap to pass by reference?

Hey everyone,

Been trying for a while now to get the following working:

newSetOfTiles.Add(poppedWall.getHexKey(), poppedWall);
InMaze.Add(newSetOfTiles);  
TileData tempData = new TileData();
Vector3Int tempPosition = new Vector3Int(poppedWall.getHexKey().row, poppedWall.getHexKey().column, 0);
ITilemap resourceTileHolder1 = null;
resourceTileHolderOriginal.GetTile(tempPosition).GetTileData(tempPosition, resourceTileHolder1, ref tempData);
tempData.gameObject.SetActive(false);

Now for a little background. I’m attempting to use Kruskal’s algorithm to remove walls in order to build the maze. All the walls have been added to the original tilemap “ResourceTileHolderOriginal”, so I am attempting to remove the unnecessary walls using Kruskal’s.

However, my attempts to use SetTile to remove have failed. I used the coordinates of the tile and attempted to null the tile there, but no tiles were removed.

resourceTileHolder.SetTile(new Vector3Int(poppedWall.getHexKey().row, poppedWall.getHexKey().column, 0), null);

So I found SetActive, which I am attempting to currently use with TileData. However, in order to get said TileData (aka tempData), I need to reference iTilemap.

GetTileData(tempPosition, resourceTileHolder1, ref tempData);

Question is, how? The current code at top is a runtime error with a NullPointerException, obviously due to “resourceTileHolder1” being null. There is 0 documentation on how to get iTilemap, and every document that I have see so far have USED iTilemap, but no mention on how it was originally passed.

Any information on how to get iTilemap would be welcomed. Help on fixing the code is secondary in my mind to the importance of understanding iTilemap more.

Thank you.

Hi @StrangeBroJim

Did you solve your issue?

I’m not quite sure what you are doing… but say you have a tilemap, where map data bounds are 0,0 to 2,2 so it’s a 2D tile grid of 3x3 tiles. To remove center tile, all you should need to do is:

if (Input.GetKeyDown(KeyCode.R))
{
    var location = new Vector3Int(1, 1, 0);
    tilemap.SetTile(location, null);
    tilemap.RefreshTile(location);
}

@StrangeBroJim

“Any information on how to get iTilemap would be welcomed.”

I haven’t used Tilemap that much. But I remember reading about this…

And I’m not sure what you are trying to do.

ITilemap is more like something what Tilemap itself uses. For example, there was a demo in manual, where you create a custom Tile.

So when you create a Scriptable Object custom Tile class, ITilemap is one parameter you’ll have to pass to this custom tile’s GetTileData method.

So Actually it is the Tilemap system in editor and probably during run-time too that will pass this data to your tile. This example renders tile with different sprite, based on its location, creating checker pattern of two sprites it has, if you draw an area of these tiles:

[CreateAssetMenu]
public class CustomTile : TileBase
{
    public Sprite spriteA;
    public Sprite spriteB;

    public override void GetTileData(Vector3Int position, ITilemap tm, ref TileData tileData)
    {
        bool evenCell = Mathf.Abs(position.y + position.x) % 2 > 0;
        tileData.sprite = evenCell ? spriteA : spriteB;
    }
}

See these pages:

@eses

Thank you for your responses.

So the main issue I have is with GetTileData. I was attempting to use the function to get the TileData, so that I could modify or change the information contained within the tile itself, but in order to do so, you HAVE to pass iTilemap.

How do you pass iTilemap?

You don’t collect iTilemap from the Tilemap class, I’ve checked. There is no function that returns iTilemap as a reference.

So how do I get the object of iTilemap in order to use it in passing into a function, such as GetTileData.

Setting an empty object with iTilemap did not work for me, unless I was doing it incorrectly.

I know that I can override GetTileData, and that I could create my own functions that has iTilemap as a parameter, but that does not mean where I am getting iTilemap to begin with. How can I pass iTilemap into a function, if I don’t know how to collect the object in the first place. And a null parameter wasn’t working.

Help on that matter is what I’m looking for, because I feel like I’m going to need to use GetTileData within my scripts.

You can’t pass ITilemap.

Your base assuption is wrong, the way how you wanna deal with it.

GetTileData is supposed to be used for ScriptableTile.
Not to use it somewhere else on a single Tile after GetTile on a Tilemap.
There will be no way for you to instantiate ITilemap yourself and fill it with data.

Do you even use a ScriptableTile, does it even fit your needs or do you just want to manipulate a Tile at a given position of a Tilemap ?

Then you are good to go with GetTile und SetTile.

Please find some little guidance how could get a start yourself around this topic with point 5. & 6. of this:

If you also intend to manipulate the Color of a Tile, make use of myTilemap.SetColor(position, color)

KR
blu3

1 Like