Bitmasking and Auto-tile in unity and C#

I’ve been trying to implement an auto tile feature in code instead of using the Unity in-house features,
for studying purposes.

I’ve been using this tutorial How to Use Tile Bitmasking to Auto-Tile Your Level Layouts | Envato Tuts+

and even with ChatGPT, I’m not getting any closer to the goal. I’ve spent several days trying to solve this problem.

I have some misunderstanding about how to make this work programmatically.

I have these methods in my Furniture class. It is meant to assign and calculate bitmask values.
The values are consistent enough, meaning it is possible that I could assign the bitmask values each a sprite, and it does work.

But I want the values to be the same as in the tutorial.

private List<Furniture> furnList = new List<Furniture>();
    public enum Direction
    {
        North,
        East,
        West,
        South,
    }

    // This method changes the bitmask value of the current
    // furn obj, NOT the neighbor furn obj that its looking
    // for by using the HasNeighbor method..

    public void CalculateBitmaskValue()
    {
            int maskValue = 0;
            this.Bitmask = 0;
            if (HasNeighbor(Direction.South))
            {
                maskValue |= 1;  // The current tile is the N neighbor of the S tile.
                Debug.Log($"Tile ({tile.X}, {tile.Z}) has bitmask value: {maskValue} after checking {Direction.South}");
            }
            if (HasNeighbor(Direction.East))
            {
                maskValue |= 2;  // The current tile is the W neighbor of the E tile.
                Debug.Log($"Tile ({tile.X}, {tile.Z}) has bitmask value: {maskValue} after checking {Direction.East}");
            }
            if (HasNeighbor(Direction.West))
            {
                maskValue |= 4;  // The current tile is the E neighbor of the W tile.
                Debug.Log($"Tile ({tile.X}, {tile.Z}) has bitmask value: {maskValue} after checking {Direction.West}");
            }
            if (HasNeighbor(Direction.North))
            {
                maskValue |= 8;  // The current tile is the S neighbor of the N tile.
                Debug.Log($"Tile ({tile.X}, {tile.Z}) has bitmask value: {maskValue} after checking {Direction.North}");
            }
            this.Bitmask = maskValue;
    }

    private bool HasNeighbor(Direction direction)
    {
        int deltaX = 0, deltaZ = 0;
        switch (direction)
        {
            case Direction.North:
                deltaZ = 1;
                break;
            case Direction.West:
                deltaX = -1;
                break;
            case Direction.East:
                deltaX = 1;
                break;
            case Direction.South:
                deltaZ = -1;
                break;
            default:
                throw new ArgumentOutOfRangeException(nameof(direction), "Unknown direction");
        }
        Tile neighboringTile = World.current.GetTileAt(tile.X + deltaX, tile.Z + deltaZ);
        if (neighboringTile == null)
        {
            return false; // No neighboring tile in this direction.
        }
        return neighboringTile.furniture != null && neighboringTile.furniture.objectType == "Stockpile";
    }

    public void UpdateBitmasksForTileAndNeighbors(Tile tile)
    {
    // Add the current furniture to be calculated
        furnList.Add(tile.furniture);
        // Calculate bitmask for neighboring furniture
        foreach (Direction dir in Enum.GetValues(typeof(Direction)))
        {
            Tile neighboringTile = GetNeighboringTile(tile, dir);
            if (neighboringTile != null && neighboringTile.furniture != null && neighboringTile.furniture.objectType == "Stockpile")
            {
                // Add the neighboring furniture to be calculated
                furnList.Add(neighboringTile.furniture);
            }
        }
        foreach (var item in furnList)
        {
            item.CalculateBitmaskValue();
        }
    }

    public Tile GetNeighboringTile(Tile tile, Direction direction)
    {
        int deltaX = 0, deltaZ = 0;
        switch (direction)
        {
            case Direction.North:
                deltaZ = 1;
                break;
            case Direction.West:
                deltaX = -1;
                break;
            case Direction.East:
                deltaX = 1;
                break;
            case Direction.South:
                deltaZ = -1;
                break;
            default:
                Debug.LogWarning("Unhandled direction: " + direction);
                return null;
        }
        return World.current.GetTileAt(tile.X + deltaX, tile.Z + deltaZ);
    }

    static public Furniture PlaceInstance(Furniture proto, Tile tile)
    {
        if (proto.funcPositionValidation(tile) == false)
        {
            return null;
        }
        Furniture obj = proto.Clone();
        //Furniture obj = new Furniture(proto);
        obj.tile = tile;
        // FIXME: This assumes we are 1x1!
        if (tile.PlaceFurniture(obj) == false)
        {
            return null;
        }
        if (obj.linksToNeighbour)
        {
            Tile t;
            int x = tile.X;
            int z = tile.Z;
            t = World.current.GetTileAt(x, z + 1); // Northern neighbour
            if (t != null && t.furniture != null && t.furniture.cbOnChanged != null && t.furniture.objectType == obj.objectType)
            {
                t.furniture.cbOnChanged(t.furniture);
            }
            t = World.current.GetTileAt(x - 1, z); // Western neighbour
            if (t != null && t.furniture != null && t.furniture.cbOnChanged != null && t.furniture.objectType == obj.objectType)
            {
                t.furniture.cbOnChanged(t.furniture);
            }
            t = World.current.GetTileAt(x + 1, z); // Eastern neighbour
            if (t != null && t.furniture != null && t.furniture.cbOnChanged != null && t.furniture.objectType == obj.objectType)
            {
                t.furniture.cbOnChanged(t.furniture);
            }
            t = World.current.GetTileAt(x, z - 1); // Southern neighbour
            if (t != null && t.furniture != null && t.furniture.cbOnChanged != null && t.furniture.objectType == obj.objectType)
            {
                t.furniture.cbOnChanged(t.furniture);
            }
        }
        return obj;
    }

The callbacks are being used to send the furniture instance to the FurnitureSpriteController script
each time a new furniture is made, only controller scripts inherit Monobehaviour in this case.

In the FurnitureSpriteController script, I’m calling the UpdateBitmasksForTileAndNeighbors that is connected to the Furniture instance so all the furniture objects can be updated.

At the moment, I’m only dealing with the four cardinal directions to keep things simple, and trying to make sure I get the same values.

These are the values that I get.

0 1 0
0 10 4
0 0 0

N neighbour has a bitmask of 1
W neighbour has a bitmask of 4
Middle furniture object ends up with 10, it should really end up with 5, which suggests that there is a huge logical error that I’m making,

I’m very much misunderstanding the logic and how to implement it. There are also no tutorials that I can find, on this topic.

The project I’m working on is based on Project Porcupine, a tutorial series by quill18. I also realise that the DRY principle is not really being adhered to in these methods.

Any help or suggestions will be appreciated.

I highly advise staying away from ChatGPT. Not only is it wrong, it is often confidently and subversively wrong.

The concept of evaluating neighbors and encoding their status into bitmaps in order to select edge tiles is very straightforward.

However, it is also abstract and easy to misunderstand.

Get 100% absolutely confident with your bitmask flipping so that you eliminate the misunderstanding of your data.

Bitmasking is ONLY to conveniently encode multiple pieces of boolean data into a single object, nothing else.

You can also accomplish this with simple booleans and a big if/else tree, because underneath even with bitmasks, that is what will be happening effectively.

Otherwise… it sounds like it is simply…

Time to start debugging! Here is how you can begin your exciting new debugging adventures:

You must find a way to get the information you need in order to reason about what the problem is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the names of the GameObjects or Components involved?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

Visit Google for how to see console output from builds. If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer for iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

If you are working in VR, it might be useful to make your on onscreen log output, or integrate one from the asset store, so you can see what is happening as you operate your software.

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

If your problem is with OnCollision-type functions, print the name of what is passed in!

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

“When in doubt, print it out!™” - Kurt Dekker (and many others)

Note: the print() function is an alias for Debug.Log() provided by the MonoBehaviour class.

1 Like

Thanks for the reply. I’m going to research bitmask flipping at some point. I found an implementation that works.

Wondering if you’d be willing to share your implementation that works?

I’m at my wits end trying to figure this out, and as you said, there are very few resources on the topic.

Anything wrong with Unity’s tile maps? It’s pretty easy to extend to get whatever extra functionality you want needed.