InteractionCondition
Abstract base class for defining boolean conditions that gate interaction availability. Subclass and implement OnEvaluate() to define custom logic. Supports an Inspector-configurable negate flag to invert the result without creating a separate condition class.
Definition
Namespace: Paragon.Townskeep.InteractionSystem
Assembly: Townskeep.dll
[Serializable]
public abstract class InteractionConditionRemarks
InteractionCondition follows the Template Method pattern: the public Evaluate() method handles negation logic, then delegates to the abstract OnEvaluate() for subclass-specific evaluation.
Conditions are owned by an InteractionContext, which evaluates all of them via conditions.All(c => c.Evaluate(...)) before an interaction can execute. This means:
All conditions must return
truefor the interaction to be available (logical AND)A single
falseresult blocks the interaction
The context field provides access to the owning InteractionContext after Initialize() is called, allowing conditions to reference the interaction, interactor, or interactable through the context if needed.
Quick Lookup
Create a custom condition
Subclass and override OnEvaluate()
Invert condition logic
Toggle negate in Inspector
Access interaction context
Use this.context (available after Initialize())
Fields
context (protected)
The owning InteractionContext. Set by Initialize(). Provides access to Interactor, Interactable, and the Interaction itself.
negate (serialized)
When true, the result of OnEvaluate() is inverted. Configured in the Inspector.
Methods
Initialize
Called by InteractionContext.Initialize() during setup. Stores the context reference for use in OnEvaluate().
context
InteractionContext
The owning interaction context
Evaluate
Evaluates the condition for a given interactable/interactor pair. Applies negation if configured.
interactable
Interactable
The object being interacted with
interactor
Interactor
The entity attempting to interact
Returns: The condition result (potentially negated).
Logic:
Extension Points
Required Overrides
OnEvaluate(Interactable, Interactor)
Core condition logic — return true if the interaction should be available
OnEvaluate (abstract)
Implement this method to define the condition's logic.
interactable
Interactable
The object being interacted with
interactor
Interactor
The entity attempting to interact
Returns: true if the condition is met (before negation is applied).
Implementation Requirements
When subclassing, you MUST:
Override
OnEvaluate()and return a boolean resultMark the class
[Serializable]for Unity serialization
You SHOULD:
Keep
OnEvaluate()fast and side-effect-free (it may be called frequently during interaction checks)Use
contextfor accessing shared interaction state rather than caching referencesUse
[SerializeField]for Inspector-configurable parameters
Common Pitfalls
Initialize() must be called before Evaluate()
The context field is null until Initialize() is called. If your OnEvaluate() accesses context, ensure the condition has been initialized. In normal operation, InteractionContext.Initialize() handles this automatically.
OnEvaluate() should be pure
Conditions are evaluated whenever the system checks interaction availability, which may happen multiple times per frame. Avoid side effects, allocations, or expensive computations in OnEvaluate().
Must be [Serializable]
Subclasses are serialized by InteractionContext via Odin. Missing [Serializable] will cause the condition to be lost on domain reload or serialization.
Examples
Distance-based condition
State-based condition
Using the negate flag
In the Inspector, you can toggle negate to invert any condition:
WithinRangeCondition
false
"Must be within range"
WithinRangeCondition
true
"Must be outside range"
HasItemCondition
false
"Must have item"
HasItemCondition
true
"Must NOT have item"
See Also
Interaction Input — input binding subsystem
Last updated