Skip to main content

API Reference Overview

Complete API reference for SoapKit - Unity's premier ScriptableObject Architecture Pattern (SOAP) implementation.

Core Namespaces

SoapKit is organized into logical namespaces for clean architecture:

using FarmGrowthToolkit.Soap;          // Core classes and interfaces
using FarmGrowthToolkit.Soap.Events; // Event system components
using FarmGrowthToolkit.Soap.Variables; // Variable system components
using FarmGrowthToolkit.Soap.Editor; // Editor-only tools (conditional compilation)

Architecture Overview

SoapKit implements a two-pillar architecture:

Events System

  • GameEvent<T>: Generic event base class for type-safe communication
  • Listener Management: Add/remove listeners with automatic cleanup
  • Debug Integration: Built-in debugging and performance monitoring
  • Event History: Editor-only event tracking for development

Variables System

  • BaseVariable<T>: Generic variable base class with change notifications
  • IReadOnlyVariable<T>: Interface for read-only access patterns
  • Type-Specific Operations: Specialized methods per variable type
  • Constraint Validation: Built-in validation and constraint systems

Core Interfaces

IReadOnlyVariable<T>

public interface IReadOnlyVariable<T>
{
T Value { get; }
event System.Action<T> OnValueChanged;
}

IGameEvent

public interface IGameEvent
{
void AddListener(UnityAction action);
void RemoveListener(UnityAction action);
void Raise();
int ListenerCount { get; }
}

IGameEvent<T>

public interface IGameEvent<T>
{
void AddListener(UnityAction<T> action);
void RemoveListener(UnityAction<T> action);
void Raise(T value);
int ListenerCount { get; }
}

Core Classes

BaseVariable<T>

The foundation of the variable system:

[System.Serializable]
public abstract class BaseVariable<T> : ScriptableObject, IReadOnlyVariable<T>
{
// Core Properties
public T Value { get; protected set; }
public virtual bool EnableChangeNotification { get; set; } = true;

// Events
public event System.Action<T> OnValueChanged;

// Core Methods
public virtual void SetValue(T newValue);
public virtual void NotifyValueChanged();
protected virtual bool AreValuesEqual(T a, T b);

// Editor Integration
[ContextMenu("Trigger Change Notification")]
public void TriggerChangeNotification();

#if UNITY_EDITOR
[UnityEditor.MenuItem("CONTEXT/BaseVariable/Debug Value")]
static void DebugValue(UnityEditor.MenuCommand command);
#endif
}

GameEvent<T>

The foundation of the event system:

[System.Serializable]
public abstract class GameEvent<T> : ScriptableObject, IGameEvent<T>
{
// Core Properties
public int ListenerCount => listeners?.GetPersistentEventCount() ?? 0;
public bool EnableEventHistory { get; set; } = true;

// Core Methods
public void AddListener(UnityAction<T> listener);
public void RemoveListener(UnityAction<T> listener);
public void Raise(T value);
public void RemoveAllListeners();

// Editor Integration
[ContextMenu("Raise Test Event")]
public virtual void RaiseTestEvent();

#if UNITY_EDITOR
public List<EventHistoryEntry> EventHistory { get; }
public void ClearEventHistory();
#endif
}

Variable Types

Primitive Variables

TypeClassOperationsConstraints
BoolBoolVariableToggle, SetTrue/False, And, Or, XorNone
IntIntVariableAdd, Subtract, Multiply, Divide, Increment, DecrementMin/Max values
FloatFloatVariableMath operations, Percentage, RoundMin/Max values, decimal places
StringStringVariableAppend, Prepend, Replace, Trim, Case conversionLength limits

Unity Types

TypeClassOperationsConstraints
Vector2Vector2VariableVector math, Normalize, Lerp, MagnitudeMagnitude limits
Vector3Vector3Variable3D operations, MoveTowards, SlerpMagnitude limits
Vector2IntVector2IntVariableInteger vector mathMagnitude limits
ColorColorVariableRGB/HSV, Lerp, Grayscale, InvertNone
GameObjectGameObjectVariableComponent access, Tag validationTag requirements
TransformTransformVariablePosition/rotation, HierarchyNone

Event Types

Primitive Events

TypeClassUse Case
UnitGameEventSimple notifications
BoolBoolGameEventState changes, toggles
IntIntGameEventScores, counters, IDs
FloatFloatGameEventValues, percentages
StringStringGameEventMessages, names

Unity Type Events

TypeClassUse Case
Vector2Vector2GameEvent2D positions, UI coordinates
Vector3Vector3GameEvent3D positions, directions
Vector2IntVector2IntGameEventGrid coordinates
ColorColorGameEventColor changes
GameObjectGameObjectGameEventObject references
TransformTransformGameEventTransform references

Editor Tools API

SoapKitDebugWindow

public class SoapKitDebugWindow : EditorWindow
{
// Static access
[MenuItem("Tools/SoapKit/Debug Window")]
public static void ShowWindow();

// Runtime monitoring
public void RefreshVariableList();
public void RefreshEventList();
public void ClearEventHistory();
}

SOAPDependencyVisualizer

public class SOAPDependencyVisualizer : EditorWindow
{
// Visualization modes
public enum ViewMode { GameObjectCentric, AssetCentric, Graph }

// Core methods
public void AnalyzeDependencies();
public void ExportDependencyReport();
public void RefreshVisualization();
}

SOAPPerformanceAnalyzer

public class SOAPPerformanceAnalyzer : EditorWindow
{
// Performance monitoring
public void StartProfiling();
public void StopProfiling();
public void GeneratePerformanceReport();

// Hotspot detection
public List<PerformanceHotspot> DetectHotspots();
public void ShowOptimizationRecommendations();
}

Usage Examples

Basic Variable Usage

// Create and use a variable
[SerializeField] private IntVariable playerHealth;

private void Start()
{
// Subscribe to changes
playerHealth.OnValueChanged += OnHealthChanged;

// Use specialized operations
playerHealth.Add(25);
playerHealth.SetMax(150);
}

private void OnHealthChanged(int newHealth)
{
Debug.Log($"Health changed to: {newHealth}");
}

Basic Event Usage

// Create and use an event
[SerializeField] private BoolGameEvent onGamePaused;

private void OnEnable()
{
onGamePaused.AddListener(OnGamePausedChanged);
}

private void OnDisable()
{
onGamePaused.RemoveListener(OnGamePausedChanged);
}

private void OnGamePausedChanged(bool isPaused)
{
Time.timeScale = isPaused ? 0f : 1f;
}

Performance Considerations

Variable Performance

  • Change Notifications: Only triggered when values actually change
  • Batch Operations: Disable notifications during bulk updates
  • Memory Usage: Minimal overhead per variable instance
  • Thread Safety: Not thread-safe, use on main thread only

Event Performance

  • Listener Management: Efficient add/remove operations
  • Memory Leaks: Always pair AddListener with RemoveListener
  • Event Frequency: Monitor high-frequency events for performance impact
  • History Tracking: Editor-only feature with no runtime cost

Best Practices

Variable Best Practices

  1. Initialization: Always set initial values
  2. Constraints: Use built-in validation when possible
  3. Change Detection: Subscribe to OnValueChanged for reactive behavior
  4. Naming: Use descriptive asset names for debugging

Event Best Practices

  1. Lifecycle Management: Subscribe in OnEnable, unsubscribe in OnDisable
  2. Parameter Types: Use specific event types for type safety
  3. Event Handlers: Keep handlers lightweight and fast
  4. Documentation: Add tooltips and descriptions to event assets

Migration Guide

From Unity Events

// Old Unity Event approach
[SerializeField] private UnityEvent onHealthChanged;

// New SoapKit approach
[SerializeField] private IntGameEvent onHealthChanged;

From Direct References

// Old direct reference approach
[SerializeField] private HealthSystem healthSystem;

// New SoapKit approach
[SerializeField] private IntVariable playerHealth;
[SerializeField] private IntGameEvent onHealthChanged;

Error Handling

Common Issues

  1. Null References: Always check for null before using ScriptableObject references
  2. Missing Listeners: Use OnEnable/OnDisable pattern for reliable subscription
  3. Memory Leaks: Unsubscribe from events to prevent memory leaks
  4. Performance: Monitor listener counts and event frequency

Debug Tools

  • SoapKit Debug Window: Monitor variables and events in real-time
  • Performance Analyzer: Identify performance bottlenecks
  • Dependency Visualizer: Understand system relationships
  • Event History: Track event flow for debugging

Next Steps