Skip to content

SlashBridge

Roll4d4 edited this page Feb 23, 2025 · 1 revision

๐Ÿง  SLASHBridge: The Event-Driven Backbone of KLEP

The SLASHBridge is a centralized event management system for KLEP. It enables loosely coupled components to communicate through events, ensuring flexible and dynamic behavior management. Executables, neurons, and agents can register, unregister, and trigger events without needing direct references to one another.


๐Ÿšฆ What Does SLASHBridge Do?

  1. Event Registration & Management:

    • Stores and manages event delegates, allowing dynamic subscription and unsubscription.
  2. Universal Event System (UES):

    • Invokes registered events and logs event activity.
  3. Frame-Based Event Tracking:

    • Keeps track of events that occur within each frame, enabling conditional behavior based on recent events.
  4. Logging & Debugging:

    • Logs events with timestamps, allowing for diagnostics and debugging.

๐Ÿ” Core Components of SLASHBridge:


๐ŸŽฎ 1. Universal Event System (UniversalEvent)

public void UniversalEvent(string eventName, object eventData = null, string source = "NOT Filled out")
{
    currentFramesEvents.Add(eventName); // Add to current frame's log

    if (eventsDictionary.TryGetValue(eventName, out var handler)) // Check if event is registered
    {
        if (handler != null) // Null check
        {
            handler.Invoke(eventName, eventData); // Invoke the event
            LogEvent(eventName, source); // Log successful event
        }
        else
        {
            LogEvent(eventName, source, false); // Log as unregistered
        }
    }
    else
    {
        LogEvent(eventName, source, false); // Log as unregistered
        Debug.Log($"Unregistered event: {eventName} called by {source}");
    }
}
  • Purpose: Acts as the central method for triggering events across the KLEP system.

  • Invocation Process:

    1. Adds the event to current frame's event list.
    2. Checks if the event is registered.
    3. If registered, invokes the delegate and logs the event.
    4. If not registered, logs as an unregistered event.
  • Use Case:

    • "KeyAdded" event: When a key is added to a neuron, the agent can react by rechecking behaviors.
    • "ExecutableCompleted" event: Allows goals to track progress when sub-executables complete.

๐Ÿง  2. Event Registration & Unregistration:

โœ… Registering an Event:

public void RegisterEvent(string eventName, UniversalEventDelegate eventDelegate)
{
    if (!eventsDictionary.ContainsKey(eventName))
    {
        eventsDictionary[eventName] = eventDelegate; // Add new event
    }
    else
    {
        eventsDictionary[eventName] += eventDelegate; // Append to existing event
    }
}
  • Allows components to subscribe to specific events.
  • Supports multiple delegates for the same event, enabling multi-listener scenarios.
  • Example: An action and a goal could both listen to the "PlayerSpotted" event.

โŒ Unregistering an Event:

public void UnregisterEvent(string eventName, UniversalEventDelegate eventDelegate)
{
    if (eventsDictionary.ContainsKey(eventName))
    {
        eventsDictionary[eventName] -= eventDelegate; // Remove the specific delegate
    }

    if (eventsDictionary[eventName] == null || eventsDictionary[eventName].GetInvocationList().Length == 0)
    {
        eventsDictionary.Remove(eventName); // Clean up empty events
    }
}
  • Ensures that events are properly unregistered, preventing memory leaks or invalid callbacks.
  • Removes events with no active listeners, keeping the dictionary clean.

โฒ๏ธ 3. Frame-Based Event Tracking (BridgeUpdate)

public void BridgeUpdate()
{
    currentFramesEvents.Clear(); // Clear the current frame's events
}
  • Resets the frame-based event list at the end of each frame.
  • Prevents stale events from affecting future logic, ensuring frame-accurate behavior responses.
  • Use Case:
    • An executable might only fire if "PlayerInSight" was triggered in the same frame.

๐Ÿ“‹ 4. Event Logging (LogEvent)

private void LogEvent(string eventName, string source, bool isRegistered = true)
{
    if (!enableLogging) return; // Logging can be toggled on/off

    string timestamp = DateTime.UtcNow.ToString("o"); // ISO 8601 format
    string logMessage = isRegistered 
        ? $"Event Triggered: {eventName}, Source: {source}, Timestamp: {timestamp}"
        : $"Unregistered Event: {eventName}, Source: {source}, Timestamp: {timestamp}";
    
    eventLog.Add(logMessage);
}
  • Records all event triggers, including source information and timestamps.
  • Distinguishes between registered and unregistered events, providing detailed logs.
  • Enables debugging: The event log can be queried via GetEventLog().

๐Ÿ’ก Practical Example:

๐ŸŽฎ Scenario: AI Alert System in a Stealth Game

  1. A sensor detects the player and triggers a "PlayerSpotted" event:
parentNeuron.bridge.UniversalEvent("PlayerSpotted", playerTransform, "PlayerSensor");
  1. The goal listens to this event and initiates a response:
parentNeuron.bridge.RegisterEvent("PlayerSpotted", OnPlayerSpotted);

private void OnPlayerSpotted(string eventName, object eventData)
{
    Debug.Log("Player Spotted! Initiating attack sequence.");
    parentNeuron.AddKey(KeyCreationService.CreateKeyData("AttackPlayer_KEY", 1.0f, keyLoader));
}
  1. The agent reacts, prioritizes attack behavior, and executes the appropriate action.

โœ… Key Takeaways:

  • Centralized Event Management:

    • Allows any component to broadcast or listen for events, promoting modular design.
  • Dynamic Behavior Control:

    • Events trigger changes in behavior, enabling reactive and adaptive AI systems.
  • Frame-Based Logic:

    • Events are tracked by frame, allowing for temporal checks in AI decision-making.
  • Logging & Debugging:

    • Provides a clear history of events, aiding in development and testing.

๐Ÿง  How This Fits Into KLEP:

  • Sensors can broadcast events when environmental conditions change.
  • Routers can convert event data into keys, enabling conditional behavior flows.
  • Goals and actions can react to specific events, adding complexity to AI behavior trees.

Would you like more focus on specific use cases, like triggering goals or handling complex event chains within KLEP? ๐Ÿ˜Š

KLEP Wiki Navigation

๐Ÿš€ Getting Started

๐Ÿง  Core Concepts

๐ŸŽฎ Examples and Tutorials

๐Ÿ› ๏ธ Scripts and Components

๐Ÿ’ก Experimental/Project Projections

๐Ÿงฌ Contributing

Clone this wiki locally