3 min read

Interaction System

Table of Contents

Interaction System Features

Interactions with other elements in the level, such as levers, objects, NPCs or transfers, are possible thanks to an Area2D added to the character. The interaction is managed by a StateInteract state that offers different configurations, such as:

  • The interaction area
  • The states to enable on interaction
  • The states to enable when leaving the interaction area
  • Activate the interaction via the input of an action (defined in Input Map)
  • Activate the interaction only if all conditions are met (for example: the character must face a certain direction or an item must be in the inventory)

Configuration

Who triggers the Interaction

A character (or any other game element) that initiates an interaction must have:

  • An Area2D node with the monitorable property set to true.
  • The collision_layer property of the Area2D must match the collision_mask property of an InteractionArea defined within a StateInteract state.
Interaction Trigger

The element to Interact with

An element that wants to responds to interactions must meet the following requirements:

  • A StateMachine with at least one StateInteract state enabled.
State Interact
  • The StateInteract state must have its interaction_area property linked to an Area2D node.
  • The Area2D node must have the monitoring property set to true.
  • The collision_mask property of the Area2D must match the collision_layer property of the areas triggering the interaction.
Interaction Area
💡

An InteractionArea node is available to quickly add a pre-configured Area2D, ready to be set as an interaction_area in a StateInteract.

Conditions

Conditions are a set of rules that must be met to trigger an interaction. You can add a new condition by adding a new element to the conditions list of a StateInteract state.

add new condition to interaction

You can optionally define a list of states to activate when a condition is not met. To do this, add a new entry to the on_condition_not_met dictionary, specifying:

  • Key: The name of the condition to monitor.
  • Value: The State to enable if the condition is not met.

Make sure that the resource_name of the ContentItem resource is set and that the Key of the entry matches it accordingly.

set condition not met

Pre-Built Conditions

  • CheckDirection: Ensures the character is facing a specific direction.
  • CheckItems: Verifies that the interacting node has an Inventory containing the required items.
  • CheckParamValue: Checks if a specific parameter on the interacting node matches a given value.

Custom Conditions

You can create custom conditions by extending the Check class and implementing the check method in your script. The check method should return true if the condition is met, and false otherwise. You can check the pre-built conditions scripts for reference.

Is something unclear, or do you have suggestions to improve a part of this document? Feel free to share your thoughts leaving a comment below!