Skip to content

Commit 0ddbf9e

Browse files
committed
docs: event listener docs
1 parent 42cc01e commit 0ddbf9e

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

docs/src/content/docs/components/event-listener.mdx

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,66 @@ title: Event Listener
33
description: Event listener component for hooking up events in the inspector.
44
sidebar:
55
order: 10
6-
---
6+
---
7+
8+
Event Listener is a component that allows you to hook up events from a [scriptable event](../../types/scriptable-event/) in the inspector.
9+
10+
## Usage
11+
12+
Assign a scriptable event to the `Target Event` field of the component.
13+
14+
Then pick when you want to start listening. You can pick between `Awake`, `Start`, and `OnEnable`.
15+
16+
- `Awake`: The event will be triggered when the value changes in the `Awake` method.
17+
- `Start`: The event will be triggered when the value changes in the `Start` method.
18+
- `OnEnable`: The event will be triggered when the value changes in the `OnEnable` method.
19+
20+
Then pick when you want to stop listening. You can pick between `OnDestroy` and `OnDisable`.
21+
22+
- `OnDestroy`: The event will be triggered when the value changes in the `OnDestroy` method.
23+
- `OnDisable`: The event will be triggered when the value changes in the `OnDisable` method.
24+
25+
After that you can pick when you want the events to be triggered. You can pick between `Any`, `From Value`, and `To Value`.
26+
27+
- `Any`: The event will be triggered any time the event is invoked.
28+
- `From Value`: The event will be triggered when the event is invoked from the value you set in the inspector.
29+
- `To Value`: The event will be triggered when the event is invoked to the value you set in the inspector.
30+
31+
:::note
32+
`Invoke On` does not exist on event listeners without arguments.
33+
:::
34+
35+
## Extending
36+
37+
Event Listeners are extended by inheriting from the [`ScriptableEventListener<T>`](../../reference/hertzole.scriptablevalues.scriptableeventlistener-1/) class. This will allow you to create your own event listeners for your own types.
38+
39+
```csharp title="MyEventListener.cs"
40+
using Hertzole.ScriptableValues;
41+
using UnityEngine;
42+
43+
public class MyEventListener : ScriptableEventListener<MyValueType>
44+
{
45+
// Simple as that!
46+
}
47+
```
48+
49+
You can also override methods based around the invoking of events.
50+
51+
```csharp title="MyEventListener.cs"
52+
using Hertzole.ScriptableValues;
53+
using UnityEngine;
54+
55+
public class MyEventListener : ScriptableEventListener<MyValueType>
56+
{
57+
protected override bool OnBeforeEventInvoked(object sender, MyValueType args)
58+
{
59+
// Do something before the event is invoked.
60+
return true; // Return false to cancel the invocation.
61+
}
62+
63+
protected override void OnEventInvoked(object sender, MyValueType args)
64+
{
65+
// Do something after the event is invoked.
66+
}
67+
}
68+
```

0 commit comments

Comments
 (0)