-
Notifications
You must be signed in to change notification settings - Fork 0
SlashBridge
Roll4d4 edited this page Feb 23, 2025
·
1 revision
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.
-
Event Registration & Management:
- Stores and manages event delegates, allowing dynamic subscription and unsubscription.
-
Universal Event System (UES):
- Invokes registered events and logs event activity.
-
Frame-Based Event Tracking:
- Keeps track of events that occur within each frame, enabling conditional behavior based on recent events.
-
Logging & Debugging:
- Logs events with timestamps, allowing for diagnostics and debugging.
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:
- Adds the event to current frame's event list.
- Checks if the event is registered.
- If registered, invokes the delegate and logs the event.
- 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.
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.
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.
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.
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().
- A sensor detects the player and triggers a "PlayerSpotted" event:
parentNeuron.bridge.UniversalEvent("PlayerSpotted", playerTransform, "PlayerSensor");- 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));
}- The agent reacts, prioritizes attack behavior, and executes the appropriate action.
-
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.
- 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? ๐