Hiding tiles

Hi all
I’m discovering unity and started a top down rpg project.
I can display my tilemap, and I want to hide some tiles depending of the player position, for example, an area surrounded by mountains. (This is not a shadow rendering.)

example, look at the area on top left :
8176145--1064513--images.jpeg

As I use a TileData object on each tile, I added a IsVisible property.

The IsVisible flag should be recalculated only when the player move, and before rendering new tiles.
The method to calculate IsVisible is done and I can call it from the player object before it start to move.

But I can’t find any way to tell the renderer to hide a tile (set to null) before rendering it.
Is there anything like and event or a callback before rendering a tile where I can skip rendering if IsVisible is false ?

Any idea ?
Thanks

The magic sauce you are missing is to give the TileData enough intel to set the associated Tile’s sprite either to what it should be (when visible) or else set it to blank (or color it black) when not visible.

1 Like

Thanks for the fast reply.
I found Tilemap.GetSprite, but I think It’s not what I need… I will look in this direction…

That is for use from the Tilemap’s perspective… eg, I have a Tilemap, what is the Sprite of the Tile at position X/Y?.

If you are already setting isvisible within a Tile, I would imagine you want something that does not care or know about the Tilemap.

1 Like

When the player start to move, I will parse all tiles to set the isvisible flag, and then access the sprites of tiles to hide and change the sprite. Then I will start the player movement.

The problem now is how to change the sprite of a liste of tiles. The GetTile is a Tilemap method

Maybe the cleaner solution will be to create a custom tile (form tilebase), and override the RefreshTile method to set the color, or the sprite, depending of the isvisible flag…
What do you thing ?

There’s plenty of ways to do fog of war. It totally depends on what you want.

There’s even multiple levels of fog of war, such as what you see in Starcraft, for example.

Don’t confuse the various levels of the process:

  • determining what is visible (dependent on your game design)

  • actually showing or hiding stuff (dependent on how your game visualizes stuff)

Visualization and logic should always be kept separate.

You’re right about separation of concern, I already have the logic, what I’m missing is the visualization.

In short, how can I change the sprite properties of a tile before it is rendered ?

Am I missing some part of your actual question?

Here is my actual code:

    void Update()
    {
        var position = new Vector3Int((int)transform.position.x, (int)transform.position.y, 0);
        var upperLeft = new Vector3Int(position.x - distanceToEdge, position.y - distanceToEdge);
        BoundsInt area = new BoundsInt(upperLeft, new Vector3Int(distanceToEdge * 2, distanceToEdge * 2));

        for (int y = area.yMin; y < area.yMax; y++)
        {
            for (int x = area.xMin; x < area.xMax; x++)
            {
                var tile = new Vector3Int(x, y);

                var myTileData = GameManagers.Map.GetTileData(tile);
                if (myTileData != null && !myTileData.IsVisible)
                {
                    UnityEngine.Tilemaps.TileData tileData = default;
                    myTileData.TileBase.GetTileData(position, Tilemap, ref tileData);
                    tileData.sprite = null;
                    myTileData.TileBase.RefreshTile(position,Tilemap);
                }
            }
        }
    }

At the end, I’m getting the Tiledata with the ‘GetTileData’. I need a ITileMap, but TileMap is not implementing ITileMap.
Where can I get the expected object implementing ITileMap ?

Ok, I get it, I need

((Tile)myTileData.TileBase).sprite = null;

TileBase does not have a sprite property.
Sorry, it should be obvious to you…