InteractionContext
Glue object that binds an Interaction to its triggers and conditions. Manages the execution lifecycle: evaluates availability, matches triggers, sets runtime context (interactor/interactable), executes the interaction asynchronously, and cleans up via IDisposable.
Definition
Namespace: Paragon.Townskeep.InteractionSystem
Assembly: Townskeep.dll
[Serializable]
public class InteractionContext : IDisposableImplements: IDisposable
Remarks
InteractionContext is the central wiring object in the Interaction System. Each context represents a single "row" in an InteractionTable, binding together:
Triggers — how the interaction can be started (e.g., click, proximity, key press)
Conditions — whether the interaction is available (e.g., within range, has item)
Interaction — what happens when triggered (the async action logic)
Execution Lifecycle
IDisposable Pattern
InteractionContext uses IDisposable as a scoped context manager. Execute() wraps the interaction call in a using block:
SetContext() returns this (which implements IDisposable), so Dispose() is called automatically when the interaction completes or throws — clearing the interactor/interactable references and resetting isRunning.
Debug.Assert(!interaction.IsRunning) fires in both SetContext() and Dispose(). The assertion in SetContext prevents double-execution; the assertion in Dispose ensures the interaction has fully completed before context cleanup.
Quick Lookup
Check if available for a pair
context.IsAvailableInContext(interactable, interactor)
Check if a trigger matches
context.CanBeTriggeredWith(trigger)
Execute the interaction
context.Execute(interactor, interactable)
Get the interaction type
context.GetInteractionType()
Get the interaction instance
context.GetInteraction()
Check if currently running
context.IsRunning
Add a trigger at runtime
context.AddInteractionTrigger(trigger)
Add a condition at runtime
context.AddInteractionCondition(condition)
Properties
Interactor
The interactor currently executing this context. Only valid while IsRunning is true.
Interactable
The interactable currently being interacted with in this context. Only valid while IsRunning is true.
IsRunning
Whether this context currently has an active interaction execution in progress.
Methods
Initialize
Initializes all triggers, conditions, and the interaction with this context. Must be called before any availability checks or execution. Called automatically by InteractionTable.Initialize().
IsAvailableInContext
Returns true if the interaction is not already running and all conditions evaluate to true for the given pair.
interactable
Interactable
The target object
interactor
Interactor
The entity attempting to interact
Returns: true if the context is available.
Logic:
CanBeTriggeredWith
Returns true if any of this context's triggers match the given trigger.
interactionTrigger
InteractionTrigger
The incoming trigger to match against
Returns: true if at least one trigger matches.
Execute
Sets the runtime context (interactor/interactable) and asynchronously executes the interaction. Automatically cleans up via Dispose() when the interaction completes or is cancelled.
interactor
Interactor
The entity performing the interaction
interactable
Interactable
The target being interacted with
This method is async void, meaning exceptions will not be caught by the caller. The interaction lifecycle (OnBegin/OnComplete/OnCancel) handles error recovery internally.
GetInteractionType
Returns the System.Type of the contained interaction.
GetInteraction
Returns the contained Interaction instance.
AddInteractionTrigger
Adds a trigger to the context at runtime.
RemoveInteractionTrigger
Removes a trigger from the context at runtime.
AddInteractionCondition
Adds a condition to the context at runtime.
RemoveInteractionCondition
Removes a condition from the context at runtime.
Dispose (explicit IDisposable)
Clears interactor/interactable references and resets isRunning to false. Called automatically by the using block in Execute().
Dispose() asserts !interaction.IsRunning. If the interaction has not fully completed (e.g., stuck in an infinite await), this assertion will fire.
Common Pitfalls
Interactor/Interactable are null when not running
The Interactor and Interactable properties are only populated during Execute(). Outside of execution, they return null. Check IsRunning before accessing them.
Double execution blocked by assertion
SetContext() asserts !interaction.IsRunning. Attempting to call Execute() while the context is already running will trigger a Debug.Assert failure.
Triggers and conditions are serialized via Odin
The triggers and conditions lists use [OdinSerialize] for polymorphic serialization. Standard Unity [SerializeField] will not preserve subclass data. The interaction field uses standard [SerializeField] with Odin's polymorphic support.
Initialize() must be called before use
IsAvailableInContext() calls condition.Evaluate() which depends on the condition being initialized with its owning context. In normal operation, InteractionTable.Initialize() cascades to all contexts.
Examples
Querying availability and executing
Adding triggers/conditions at runtime
See Also
InteractionTable — collection that owns contexts
Interaction — the async action executed by a context
InteractionTrigger — trigger matching base class
InteractionCondition — condition evaluation base class
Interactor — entity that queries and executes contexts
Interactable — target that owns contexts via its table
Last updated