This project uses multiple different Scene Graph components that are triggered by an action. This includes actions that are triggered by an event:
For example, all of the sphere lights in the image above are Scene Graph entities that have a component named triggerable_light_component, which is a component class that implements the triggerable interface. The different combinations of entities are triggered when the player steps on different white pedestals.
You can use Verse interfaces to define common functionality between unrelated objects or actions. In this case, the common functionality is triggering an action and the unrelated objects or actions are mesh visibility, mesh transformation, and turning lights on or off.
For more information about the Verse language features used on this page, see:
Define Triggerable Interface
You'll begin by creating a new empty Verse file and naming it Triggerable.verse. You'll define the triggerable interface. You can define default functionality and field values with interfaces, or leave them to child classes to define and implement. Then you'll define an event that is used to signal whether a Scene Graph entity that has a component that inherits from the triggerable interface has been triggered.
This file uses the
@editableattribute on various interface fields. To do this, include the /Verse.org/Simulation module:Verseusing { /Verse.org/Simulation }Interfaces in Verse are adjectives since they describe an action that child classes implement. Create a new interface named
triggerable:Versetriggerable<public> := interface:You might want to keep track of whether or not a triggerable object has been triggered — for example, whether a light is on or off or whether a mesh is visible or invisible. It is possible that you might only want to trigger an action once. Add the following logic fields to
triggerableto keep track of these:Versevar<protected> Triggered<public>:logic @editable OnlyTriggerOnce<protected>:logic = false var<protected> HasBeenTriggered<protected>:logic = falseTriggerable objects are used as pieces in the puzzles built later in this tutorial. Additionally, you might want to use triggerable objects as decoy pieces in a puzzle that are triggered by an action and the pieces act like they are part of the puzzle, but their triggered state does not contribute to or take away from a puzzle being solved. Add a
PuzzlePiecelogic field to keep track of whether or not this triggerable object is a puzzle piece.Verse@editable PuzzlePiece:logic = falseFor even more flexibility, you might also want a puzzle where some lights must be on and some must be off for the puzzle to be considered solved. Add a
SolvedStateTriggeredlogic field to indicate whether this triggerable object puzzle piece is considered solved if it is triggered or not triggered.Verse@editable SolvedStateTriggered:logic = trueDeclare three functions:
PerformAction,PerformReverseAction, andPostTrigger. Sincetriggerableis an interface and the interface does not provide a base implementation for these functions, any child class oftriggerablemust provide its own implementation of each of these functions.Verse# Action performed when transitioning from not triggered to triggered state PerformAction():void # Action performed when transitioning from triggered to not triggered state PerformReverseAction():void # Action performed after PerformAction or PerformReverseAction is called PostTrigger():voidDefine three additional functions:
Trigger,InSolvedState, andSetInitialTriggeredState. These three functions all have a base implementation provided in thetriggerableinterface.Triggertriggers the action for the component that implements this interface,InSolvedStatedecides whether the component is in a solved triggered state, andSetInitialTriggeredStatedetermines what the initial triggered state of a component is upon beginning of play.Verse# Trigger the appropriate action Trigger():void = if: (OnlyTriggerOnce? and not HasBeenTriggeredOnce?) or (not OnlyTriggerOnce?) then: if (Triggered?): PerformReverseAction() else: PerformAction() PostTrigger()
Define Triggered Event
This puzzle project also uses Scene events to send events up or down the Scene Graph hierarchy to trigger actions, like transforming a mesh or toggling a light on or off. In this case, we define an event named triggered_event that is sent up the Scene Graph hierarchy when a triggerable object is triggered. This event is handled by a Scene Graph component to determine whether or not a puzzle is solved.
Scene events require the Scene Graph API. To use the Scene Graph API, include the /Verse.org/SceneGraph module:
Verseusing { /Verse.org/SceneGraph }Add a Scene event named triggered_event:
Versetriggered_event<public> := class(scene_event):Add two fields to this event, the first, an optional indicating the entity that is triggered, and the second, a logic indicating whether or not this entity has been triggered:
VerseTriggeredEntity:?entity = false Triggered:logic = false
Next Steps
Next, you’ll create the Puzzle Component to manage a puzzle and determine when it is solved.
Final Code for Triggerable.verse
<#
Interface for triggering an action on a component.
Any component that you want to be triggered when the touch_component has it's Touch function called
must implement this interface.
#>
using { /Verse.org/Simulation }
OnlyTriggerOnceToolTip<public><localizes>:message="Whether this triggerable component should only ever be triggered once."
SolvedStateTriggeredToolTip<public><localizes>:message="Whether this puzzle pieces solved state is the triggered state. Note: Only relevant if PuzzlePiece is set to true."
PuzzlePieceToolTip<public><localizes>:message="Whether this entity is part of a puzzle solution."