Skip to content

Commit eda024f

Browse files
Create events.md
1 parent af05600 commit eda024f

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/articles/events.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Events
2+
3+
For custom events see [Custom Events](https://fabianterhorst.github.io/coreclr-module/articles/custom-events.html).
4+
5+
Events can be registered via [scripts](https://fabianterhorst.github.io/coreclr-module/articles/create-script.html) or dynamically via event delegates.
6+
This documentation is registering events via delegates dynamically.
7+
8+
To add for example a player connect event handler dynamically you can just add the delegate that will be called when a player connects.
9+
10+
This is a example using the lambda operator to add a event handler.
11+
12+
```csharp
13+
Alt.OnPlayerConnect += (player, reason) => {
14+
Console.WriteLine($"{player.Name} connected.");
15+
};
16+
```
17+
18+
Its also possible to add a method instead of using a lambda. The method can be private, public, static ect.
19+
20+
```csharp
21+
Alt.OnPlayerConnect += OnPlayerConnect;
22+
...
23+
public void OnPlayerConnect(IPlayer player, string reason)
24+
{
25+
Console.WriteLine($"{player.Name} connected.");
26+
}
27+
```
28+
29+
You should not register event handlers in async code.
30+
31+
For a list of all event handlers see [Alt.Events.cs](https://github.com/FabianTerhorst/coreclr-module/blob/master/api/AltV.Net/Alt.Events.cs).
32+
33+
And the method signatures can be found in [Events.cs](https://github.com/FabianTerhorst/coreclr-module/blob/master/api/AltV.Net/Events/Events.cs).
34+
35+
The player event handler looks like this.
36+
37+
```csharp
38+
Alt.OnPlayerEvent += (player, name, args) => {
39+
};
40+
```
41+
42+
For automated arguments validating you can use [Custom Events](https://fabianterhorst.github.io/coreclr-module/articles/custom-events.html).

0 commit comments

Comments
 (0)