go-bt: Minimalist Behavior Trees for Go go-bt is a Behavior Tree library for Go. Designed for background workers, game AI, mundane task automation, and asynchronous logic. Instead of time.Sleep or infinite while loops, go-bt uses a cooperative multitasking model. Nodes return state instantly, yielding control back to the supervisor. Stateless Nodes: Nodes (Sequence, Selector, etc.) do not hold runtime state. All temporal memory and execution states live entirely within the generic BTContext[T]. The Magic Numbers: Every node's Run method returns exactly one of three integers: 1 (Success): The task is done and successful. 0 (Running): The task needs more time (e.g., waiting on I/O, or sleeping). It yields the thread. -1 (Failure): The task failed. First-Class Context: BTContext[T] directly embeds Go's standard context.Context, ensuring your trees natively respect global cancellation tokens and timeouts. Time-Travel Testing: The engine uses an injected clock directly on the context, allowing you to instantly test a 5-minute Timeout or Sleep node in your unit tests without actually waiting. go-bt provides a minimalist set of core nodes that can be combined to create any logical control flow: Composites: Selector (Stateless Priority / Fallback), Sequence (Stateless AND gate), MemSequence (Stateful chronological execution) Decorators: Inverter (NOT gate), Optional (Swallows failures), Timeout, Retry, Repeat Leaves: Condition (Instant memory read), Action (Execution), Sleep (Non-blocking wait) 1. Define your Blackboard Your blackboard is the custom state your tree will read and manipulate. Because the library uses Go Generics, it can be any struct. type WorkerState struct { IsConnected bool PendingTasks int } You can use the go-bt nodes to assemble your logic. This example asserts that the worker is connected before attempting to process tasks: package main import ( "time" "context" "go-bt/core" "go-bt/composite" "go-bt/leaf" ) func BuildWorkerTree(cancel context.CancelFun...
First seen: 2026-04-08 15:24
Last seen: 2026-04-08 16:25