Implement a custom module
Implement `IConvaiModule` to add custom runtime behavior that starts with the SDK, accesses runtime services, and reacts to domain events.
Prerequisites
Quickstart: minimal module
// MinimalModule.cs
using System;
using System.Collections.Generic;
using System.Threading;
using Convai.Domain.DomainEvents.Runtime;
using Convai.Runtime.Components;
using Convai.Runtime.Core.Modules;
using UnityEngine;
public class MinimalModule : MonoBehaviour, IConvaiModule
{
public string ModuleId => "my-project.minimal";
public string DisplayName => "Minimal Module";
public IReadOnlyList<string> RequiredModules => Array.Empty<string>();
public IReadOnlyList<Type> RequiredServices => Array.Empty<Type>();
public IReadOnlyList<Type> ProvidedServices => Array.Empty<Type>();
public bool IsActive { get; private set; }
private IDisposable _sub;
private void Awake() => ConvaiManager.ActiveManager?.RegisterModule(this);
private void OnDestroy() => ConvaiManager.ActiveManager?.UnregisterModule(this);
public System.Threading.Tasks.ValueTask RegisterAsync(IModuleContext ctx, CancellationToken ct = default)
=> System.Threading.Tasks.ValueTask.CompletedTask;
public System.Threading.Tasks.ValueTask StartAsync(IModuleContext ctx, CancellationToken ct = default)
{
_sub = ctx.Events.Subscribe<CharacterSpeechStateChanged>(e =>
{
if (e.IsSpeaking) Debug.Log($"[MinimalModule] Character {e.CharacterId} started speaking.");
});
IsActive = true;
return System.Threading.Tasks.ValueTask.CompletedTask;
}
public System.Threading.Tasks.ValueTask PauseAsync(RuntimePauseReason r, CancellationToken ct = default)
{ IsActive = false; return System.Threading.Tasks.ValueTask.CompletedTask; }
public System.Threading.Tasks.ValueTask ResumeAsync(CancellationToken ct = default)
{ IsActive = true; return System.Threading.Tasks.ValueTask.CompletedTask; }
public System.Threading.Tasks.ValueTask StopAsync(CancellationToken ct = default)
{ _sub?.Dispose(); IsActive = false; return System.Threading.Tasks.ValueTask.CompletedTask; }
}IConvaiModule interface
Lifecycle method reference
Method
When called
What to do
IModuleContext services
Property
Type
Availability
Description
Implement a module
Event subscriber example
Service provider and consumer example
Register a module
MonoBehaviour self-registration (recommended)
CreateRuntimeBuilder override
Use the dependency injection pattern
IInjectable<TDependencies>
IConvaiCharacterDependencies
Property
Type
Availability
IConvaiPlayerDependencies
Property
Type
Availability
Write an injectable component
Usage examples
Example 1: Biometric correlation module for medical simulation
Example 2: Assessment scoring module for industrial training
Troubleshooting
Symptom
Likely cause
Fix
Next steps
Logging, metrics, and retry policyEvent systemLast updated
Was this helpful?