Skip to main content

Lifecycle Handlers

Evolve exposes a number of lifecycle hooks that you can implement to customize the behavior of elements.

The syntax is when:hook_name => expression. when can be either before or `after. Hook names and descriptions are found below:

Event NameDescription
createCalled when the element is first created. At this point no parameters have been passed but state has been initialized
enableCalled when the element was either first created or was re-enabled in a frame
updateCalled every frame, parameters have been set for this frame
finishCalled every frame after all descendents have been updated
disableCalled asynchronously when an element was disabled. This happens on the main thread at the start of the next frame
destroyCalled asynchronously when an element was destroyed. This happens on the main thread at the start of the next frame

Lifecycle can be layered like an onion if an element is decorated. The element itself will fire it's handlers 'in the center' and any decorators are called around it.


@TopLevelDecorator
template SomeElement { ... }

@Decorator1
@Decorator2
SomeElement(
before:update => Debug.Log("Before Inline"),
after:update => Debug.Log("After Inline")
);

// assuming we log each before/after update the output would be

Decorator1.BeforeUpdate
Decorator2.BeforeUpdate
Before Inline
TopLevelDecorator.BeforeUpdate
SomeElement.BeforeUpdate
// onion center, no actual method is called here which means
// that for element declarations, before:update and after:update are basically the same
SomeElement.AfterUpdate
TopLevelDecorator.AfterUpdate
After Inline
Decorator2.AfterUpdate
Decorator1.AfterUpdate

An event or life cycle can also be mapped from the companion or from top level state. As long as the signatures match between the event you are mapping and the method on the companion or state instance, the mapping is valid.

There is also a shorthand for mapping life cycle handlers from a $companion. For lifecycle events the return type is ignored and can be anything as long as your mapping method defines no parameters.

The name mapping table for lifecycle events is below:

Event NameMethod Mapping Name
before:createOnBeforeCreate
after:createOnAfterCreate
before:enableOnBeforeEnable
after:enableOnAfterEnable
before:updateOnBeforeUpdate
after:updateOnAfterUpdate
before:finishOnBeforeFinish
after:finishOnAfterFinish
before:disableOnBeforeDisable
after:disableOnAfterDisable
before:destroyOnBeforeDestroy
after:destroyOnAfterDestroy