another approach for making the debugger nicer

This commit is contained in:
Lilith Schier 2026-06-29 02:40:34 +02:00
parent b5d5a8f9f3
commit b37c78d75b
4 changed files with 65 additions and 55 deletions

View file

@ -13,23 +13,27 @@ public sealed class Tree(Node root)
public EntityTable Blackboard { get; } = new();
public Random Random { get; init; } = new();
public AnimationPlayer? AnimationPlayer { get; init; }
private int _tickNumber;
public Result Tick() => Process(0f);
public Result Process(double delta)
{
return Root.Tick(new()
var result = Root.Tick(new()
{
Delta = delta,
Blackboard = Blackboard,
Random = Random,
AnimationPlayer = AnimationPlayer,
TickNumber = _tickNumber,
});
_tickNumber += 1;
return result;
}
public void Halt() => Root.Halt();
public Godot.Collections.Dictionary DebugData => Root.DebugData;
public Godot.Collections.Dictionary DebugData => Root.GetDebugData(_tickNumber - 1);
}
public enum Result
@ -53,9 +57,11 @@ public abstract class Node
public NodeState State { get; private set; } = NodeState.Idle;
public virtual Node[] Children => [];
public virtual string? Name { get; init; }
private int _lastTicked = -1;
public Result Tick(in Context context)
{
_lastTicked = context.TickNumber;
if (State is not NodeState.Running) OnStart(context);
var result = OnTick(in context);
State = result.ToState();
@ -88,7 +94,7 @@ public abstract class Node
/// <summary>
/// Called whenever this node is halted forcibly,
/// but only if its state is <see cref="NodeState.Running"/>.
/// but only if its state is not <see cref="NodeState.Idle"/>.
/// Therefore, this will never be called twice in a row.
/// Before being ticked again, <see cref="OnStart"/> is guaranteed
/// to be called first.
@ -97,14 +103,15 @@ public abstract class Node
{
}
public Godot.Collections.Dictionary DebugData =>
public Godot.Collections.Dictionary GetDebugData(int lastTick) =>
new()
{
["type"] = GetType().Name,
["name"] = Name ?? "",
["state"] = State.ToString(),
["is_stale"] = lastTick != _lastTicked,
["children"] =
new Godot.Collections.Array(Children.Select(c => Variant.CreateFrom(c.DebugData))),
new Godot.Collections.Array(Children.Select(c => Variant.CreateFrom(c.GetDebugData(lastTick)))),
};
}
@ -114,6 +121,7 @@ public readonly record struct Context
public required EntityTable Blackboard { get; init; }
public Random Random { get; init; }
public AnimationPlayer? AnimationPlayer { get; init; }
public int TickNumber { get; init; }
}
public static class BehaviorTreeExtensions
@ -148,10 +156,6 @@ public abstract class ListNode(Node[] children) : Node, IEnumerable<Node>
public IEnumerator<Node> GetEnumerator() => ((IEnumerable<Node>)Children).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => Children.GetEnumerator();
protected override void OnHalt()
{
foreach (var child in Children) child.Halt();
}
}
public sealed class Sequence(Node[] children) : ListNode(children)
@ -181,13 +185,9 @@ public sealed class Sequence(Node[] children) : ListNode(children)
}
LoopDone:
// if (State is NodeState.Running && childIdx < _runningChildIdx)
// {
// Children[_runningChildIdx].Halt();
// }
if (State is NodeState.Running or NodeState.Failure)
if (State is NodeState.Running && childIdx < _runningChildIdx)
{
for (var i = childIdx + 1; i < Children.Length; i++) Children[i].Halt();
Children[_runningChildIdx].Halt();
}
if (result is Result.Running) _runningChildIdx = childIdx;
@ -195,7 +195,7 @@ public sealed class Sequence(Node[] children) : ListNode(children)
return result;
}
// protected override void OnHalt() => Children[_runningChildIdx].Halt();
protected override void OnHalt() => Children[_runningChildIdx].Halt();
}
public sealed class MemoSequence(Node[] children) : ListNode(children)
@ -228,15 +228,11 @@ public sealed class MemoSequence(Node[] children) : ListNode(children)
LoopDone:
if (result is Result.Running) _runningChildIdx = childIdx;
if (State is NodeState.Running or NodeState.Failure)
{
for (var i = childIdx + 1; i < Children.Length; i++) Children[i].Halt();
}
return result;
}
// protected override void OnHalt() => Children[_runningChildIdx].Halt();
protected override void OnHalt() => Children[_runningChildIdx].Halt();
}
public sealed class Selector(Node[] children) : ListNode(children)
@ -266,13 +262,9 @@ public sealed class Selector(Node[] children) : ListNode(children)
}
LoopDone:
// if (State is NodeState.Running && childIdx < _runningChildIdx)
// {
// Children[_runningChildIdx].Halt();
// }
if (State is NodeState.Running or NodeState.Success)
if (State is NodeState.Running && childIdx < _runningChildIdx)
{
for (var i = childIdx + 1; i < Children.Length; i++) Children[i].Halt();
Children[_runningChildIdx].Halt();
}
if (result is Result.Running) _runningChildIdx = childIdx;
@ -280,7 +272,7 @@ public sealed class Selector(Node[] children) : ListNode(children)
return result;
}
// protected override void OnHalt() => Children[_runningChildIdx].Halt();
protected override void OnHalt() => Children[_runningChildIdx].Halt();
}
public sealed class DoNothing : Node

View file

@ -52,27 +52,28 @@ public sealed partial class Birb : CharacterBody2D, IAttackable,
{
if (EngineDebugger.IsActive())
EngineDebugger.SendMessage("behavior_debug:add_tree",
new() { GetInstanceId(), Tree.DebugData });
new() { Name, GetInstanceId(), Tree.DebugData });
}
private Bt.Tree CreateTree() => new(
new Bt.Selector([
new Bt.Check(() => _state is State.Attacking) { Name = "Is state Attacking?" },
new Bt.Sequence([
new Bt.Check(() => Target is not null) { Name = "Has target?" },
new Bt.Action(() => FaceToward(Target!.GlobalPosition)),
new Bt.Action(() => FaceToward(Target!.GlobalPosition)) { Name = "Face target" },
new Bt.Selector([
new Bt.MemoSequence([
new Bt.Sequence([
new Bt.Check(() => StabHitbox.GetOverlappingBodies().Contains(Target)),
new Bt.Check(() => StabHitbox.GetOverlappingBodies().Contains(Target))
{ Name = "Target in stab hitbox?" },
new Bt.MemoSequence([
new Bt.Action(Stop) { Name = "Stop" },
new Bt.Action(Stop) { Name = "Stop" },
new Bt.PlayAnimation("prepare_attack"),
new Bt.Delay(0.4) { RandomVariation = 0.2 },
]),
]),
new Bt.PlayAnimation("stab"),
new Bt.Action(() => _state = State.Attacking),
new Bt.WaitUntil(() => _state is not State.Attacking) { Name = "Done attacking?" },
new Bt.Action(() => _state = State.Attacking) { Name = "Set state to Attacking" },
]),
new Bt.MemoSequence([
new Bt.Delay(0.1) { RandomVariation = 0.05 },
@ -94,7 +95,6 @@ public sealed partial class Birb : CharacterBody2D, IAttackable,
]),
]),
new Bt.MemoSequence([
new Bt.Check(() => _state is State.Idle),
new Bt.PlayAnimation("idle"),
new Bt.DoNothing(),
]),