Skip to content

LDtk Neighbours

Shmellyorc edited this page Sep 3, 2026 · 1 revision

The Problem

LDtk levels can connect to each other. A level can have neighbors in any of the eight cardinal and intercardinal directions. When you're building a world, you need to know which levels are connected and in which direction.

Most LDtk integrations give you raw JSON data and force you to manually parse the neighbour information. This adds complexity to your game code and slows down development.

What Void Does

Void provides a fully typed MapNeighbour class that gives you access to neighboring levels by direction. You can check for a neighbor in any direction and get the level ID instantly.

MapNeighbour

The MapNeighbour class represents the neighboring levels of an LDtk level. It provides properties for each direction and a dictionary of all neighbors.

public sealed class MapNeighbour
{
    public string North { get; }
    public string NorthEast { get; }
    public string East { get; }
    public string SouthEast { get; }
    public string South { get; }
    public string SouthWest { get; }
    public string West { get; }
    public string NorthWest { get; }
    public IReadOnlyDictionary<uint, string> Neighbours { get; }
}

Accessing Neighbours

var level = map.GetLevelByName("Level_01");
var neighbours = level.Neighbours;

// Check for a specific direction
if (!string.IsNullOrEmpty(neighbours.North))
{
    // Get the level to the north
    var northLevel = map.GetLevelById(neighbours.North);
}

Neighbour Directions

Direction Property Description
North North Level directly above
NorthEast NorthEast Level above and to the right
East East Level directly to the right
SouthEast SouthEast Level below and to the right
South South Level directly below
SouthWest SouthWest Level below and to the left
West West Level directly to the left
NorthWest NorthWest Level above and to the left

Iterating Over All Neighbours

var neighbours = level.Neighbours;

// Iterate over all neighbours
foreach (var kvp in neighbours.Neighbours)
{
    uint directionHash = kvp.Key;
    string levelId = kvp.Value;

    // Convert hash back to direction if needed
    // Or just use the level ID
    var neighbourLevel = map.GetLevelById(levelId);
}

Quick Example

// Load the map
var map = AssetManager.Instance.Load<LDtkMap>("levels/world.ldtk");

// Get a level
var level = map.GetLevelByName("Level_01");

// Get the neighbours
var neighbours = level.Neighbours;

// Check for a specific direction
if (!string.IsNullOrEmpty(neighbours.North))
{
    var northLevel = map.GetLevelById(neighbours.North);
    Console.WriteLine($"Level to the north: {northLevel.Name}");
}

// Iterate over all neighbours
foreach (var kvp in neighbours.Neighbours)
{
    var neighbourLevel = map.GetLevelById(kvp.Value);
    Console.WriteLine($"Neighbour: {neighbourLevel.Name}");
}

Back to Home

Clone this wiki locally