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 : IDisposable

Implements: IDisposable

Remarks

InteractionContext is the central wiring object in the Interaction System. Each context represents a single "row" in an InteractionTable, binding together:

  • Triggershow the interaction can be started (e.g., click, proximity, key press)

  • Conditionswhether the interaction is available (e.g., within range, has item)

  • Interactionwhat happens when triggered (the async action logic)

Execution Lifecycle

spinner

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.

circle-info

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

Goal
How

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.

Parameter
Type
Description

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.

Parameter
Type
Description

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.

Parameter
Type
Description

interactor

Interactor

The entity performing the interaction

interactable

Interactable

The target being interacted with

circle-exclamation

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().

circle-exclamation

Common Pitfalls

circle-exclamation
circle-exclamation
circle-exclamation
circle-exclamation

Examples

Querying availability and executing

Adding triggers/conditions at runtime

See Also

Last updated