Contents

ActionHandlerProtocol

The base protocol for action handlers.

Declaration

protocol ActionHandlerProtocol

Overview

One of two approaches can be taken when subscribing to, and responding to action events. Which approach is taken is dictated by complexity of the action, and user preference.

The simplest approach is to process action events using an external closure. This saves having to define a formal handler for the action. For example:

struct MyAction: EntityAction {
    ...
}
MyAction.subscribe(to: .started) { event in
    let action = event.action
    // Do something with the action.
}

The other approach is to use a formal handler. This requires defining a structure for the handler that conforms to the ActionHandlerProtocol and registering the handler so it can be instantiated when the action animation is played. It is only necessary to define the event functions for the event types that one wishes to respond to.

For example:

struct MyAction: EntityAction { }

struct MyActionHandler: ActionHandlerProtocol {

   typealias ActionType = MyAction

   // Application data can be stored within the handler.
   var applicationData: ApplicationData

   // Customizable init
   init(action: MyAction, player: Entity, currentLevel: Int) { ... }

   // Process start events
   mutating func actionStarted(event: EventType) { }
}

MyActionHandler.register { event in
    // Create the handler.
    return MyActionHandler(applicationData: appData)
}

Topics

Associated Types

Instance Methods

Type Aliases

Type Methods

See Also

Action management