Skip to content

KLEP Neurons

Roll4d4 edited this page Feb 22, 2025 · 1 revision

The KLEP Neuron: The Brain of Your AI

The Neuron acts as a holder of keys and a discoverer of behaviors. It manages all setup on Awake, making setting up and cloning AI agents incredibly simple.

When you assign a neuron to a GameObject, any KLEP behaviors (executables) that are children of that object are automatically discovered on awake.


🧠 Handling Larger Hierarchies

In large and complex hierarchies, KLEPNeuron automatically discovers all executables that are children of the assigned GameObject. This behavior is powerful but can lead to performance and management challenges if your hierarchy is deeply nested or contains many executables.

🚦 Simplifying Executable Management:

  • Instead of allowing KLEPNeuron to recursively search all children, you can specify a dedicated parent object to contain only the relevant executables.
  • This reduces overhead and improves performance, especially in large scenes or with complex agent setups.

🛠️ How to Implement This:

  1. Create a Parent Object:

    • Make an empty GameObject called "KLEPBehaviors" or similar.
  2. Organize Executables:

    • Add all KLEPExecutables as children of this parent object.
  3. Assign Parent to KLEPNeuron:

    • In the Unity Editor, set the parent object as the behavior holder for the KLEPNeuron.
  4. Code Example:

public Transform behaviorParent;

void UpdateCurrentFrameExecutables()
{
    HashSet<KLEPExecutableBase> newFrameExecutables = new HashSet<KLEPExecutableBase>();
    if (behaviorParent != null)
    {
        GatherExecutables(behaviorParent, newFrameExecutables);
    }
    else
    {
        GatherExecutables(transform, newFrameExecutables);
    }
    SynchronizeExecutables(newFrameExecutables);
}

⚖️ Pros and Cons:

Approach | Pros | Cons -- | -- | -- Automatic Discovery | Simple setup, no manual linking required | Can become unwieldy in large hierarchies Dedicated Parent Object | Improved performance and control | Requires manual assignment and maintenance

Recommendation:

For small prototypes or simple AI agents, the default behavior is fine.
For large-scale projects, use a dedicated parent object to keep behavior management clean and organized.


🔍 Why This Update Is Better:

  • Provides a clear strategy for managing large hierarchies.
  • Shows how to optionally set a parent object for behavior discovery.
  • Uses a code example to illustrate how to implement this approach.

Would you like help with additional code examples, or are you looking to expand on specific scenarios where this parent object approach would be most useful? 😊


🚦 General Execution Flow:

void Update()
{
    bridge.UniversalEvent("BeforeCleanup");
    keyManager.Cleanup();
    
    UpdateCurrentFrameExecutables();
    keyManager.IssueBufferedKeys();        
    bridge.BridgeUpdate();
    agent.AgentUpdate();
    bridge.UniversalEvent("AfterKeyBufferRelease");
    fixedUpdateKeys = heldKeys;
}

🛠️ Step-by-Step Breakdown:

Cleanup Keys:
    Starts by cleaning up all keys used in the last frame.

Discover New Executables:
    Finds out if new executables have been acquired by the neuron.

Issue Buffered Keys:
    Issues keys that were pushed to the buffer in the previous frame.

Update the Bridge:
    Tells the bridge (a custom event-driven architecture) to update.

Update the Agent:
    The agent updates, which is where all executables fire.

Notify Completion:
    The bridge is notified that execution is complete, allowing listeners to react.

Update Fixed Keys:
    Fixed update keys are updated to reflect the current neuron's keys.

⚖️ Update vs. Fixed Update:

The neuron operates in a normal update cycle, running as fast as the machine allows.
However, some executables may need to operate on fixed time (physics-related logic).
KLEP supports executables firing on either or both update cycles, offering flexibility for your AI logic.

🚀 Why This Matters:

Everything is automated—just add a child object to the agent, and if that object has an executable, it will be:

Incorporated at the start of the next frame.
Immediately available for the agent to use in traversing problem space.

This dynamic discovery and execution allow KLEP agents to adapt in real-time, giving your AI the tools to respond to changing scenarios seamlessly.