Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Invocations & Higher-Order Composition

In Forester, an Invocation calls a declared sub-tree definition, built-in decorator, action, or inline lambda within another tree definition.

Invocations are the primary mechanism for composing complex behavior trees out of smaller, modular building blocks.


Basic Invocation Syntax

Invocations support both positional and named argument syntax:

import "std::actions"

// Sub-tree definition with parameters
sequence check_distance(item: object, threshold: number) {
    store("target_item", item)
    handle_distance(item, threshold)
}

root main sequence {
    // Positional argument invocation
    check_distance({"x": 10, "y": 20}, 50)
    
    // Named argument invocation
    check_distance(
        item = {"x": 30, "y": 40},
        threshold = 100
    )
}

impl handle_distance(item: object, limit: number);

Functional Composition Capabilities

Forester extends standard behavior tree semantics by offering advanced functional composition primitives:

1. Higher-Order Trees (HOT)

Pass sub-trees as parameters (t: tree) to higher-order tree definitions and delegate execution using the t(..) syntax. This allows developers to write generic retry, fallback, or logging wrappers once and reuse them across the codebase.

Read more in Higher-Order Trees.


2. Lambdas (Anonymous Inline Sub-Trees)

Define and instantly invoke inline, anonymous sub-trees (lambda sequence { ... }) without creating throwaway named definitions.

Read more in Lambdas.


Summary of Invocation Types

Invocation TypeExample SyntaxDescription
Standard Sub-Treenavigate_to(target)Invokes a named sub-tree definition.
Action Invocationcall_llm(prompt)Invokes an external impl action.
Delegated Tree (HOT)sub_action(..)Invokes a sub-tree passed as a parameter.
Inline Lambdalambda sequence { ... }Creates and executes an anonymous sub-tree inline.