How do you get neighboring nodes when writing GridGraphRules?

For example, I’m trying to add a “minimum” height between 2 walkable nodes for them to be 2 distinct layers in a LayerGridGraph. So I’d need to check if a walkable node has at least that minimum distance between the walkable node above it.

What I have so far:

public override void Register(GridGraphRules rules)
{
    // Register this rule with the grid graph
    rules.AddMainThreadPass(Pass.BeforeConnections, this._EvaluateWalkability);
}

private void _EvaluateWalkability(GridGraphRules.Context context)
{
    var _nodePositions = context.data.nodes.positions;
    var _nodeWalkability = context.data.nodes.walkable;

    LayerGridGraph _graph = context.graph as LayerGridGraph;

    for (int i = 0; i < _nodePositions.Length; i++)
    {
        var _nodeInt3
            = _graph.GetNode(
                (int)_nodePositions[i].x,
                (int)_nodePositions[i].z,
                0);

        if (_nodeInt3 != null)
        {
            if (_nodeInt3.position.y < minimumHeight)
                _nodeWalkability[i] = false;
        }
    }
}

But layers seem to always be 0 in any Pass except in Pass.AfterApplied, so I can’t really check above or below a node.

Thanks

You cannot use graph.GetNode in a grid graph rule. The node objects don’t exist yet.

Are you trying to get the node “below” the current one? Or an adacent node?

For adjacent nodes, you may want to use the IConnectionFilter interface instead.

If you are instead checking between nodes directly above/below each other, then this is actually already done in the code. It is exposed as the “Character Height” in the settings. No node will be created if it less than CharacterHeight/2 over the node below it, and it will be created, but made unwalkable, if it is between CharacterHeight/2 and CharacterHeight over the node below it.

1 Like

Character height did it. Thank you!