# Dynamic Events

## Overview

Provides easy way to subscribe methods to **Vivox Events** using **Attributes** like <mark style="color:blue;">**Unity NetCodeForGameObjects**</mark> or <mark style="color:blue;">**Mirror Networking**</mark> does with **Server/Client** **RPC's**. This functionality relies on the **System.Reflection** library which is known to be slow but I don't know how to use **Mono.Cecil** or **C#** **Dynamic Methods** with **IL Emit/OpCodes.** EasyCode searches assemblies for any methods containing EasyCode event **Attributes** (ex. <mark style="color:green;">`[LoginEvent(LoginStatus.LoggedIn)]`</mark>) at the start of the application.

&#x20;When running starting your game/app **EasyCode** skips common assemblies (_<mark style="color:orange;">you can't modify them anyways</mark>_) to speed up the search and uses async methods to prevent UI blocking as your application starts. EasyCode also stores all found methods containing **event Attributes** and caches/stores them in-memory and when **Vivox Events** are invoked it searches the cache, finds the gameobject that contains the correct event Attribute with a matching parameter type and calls/invokes the method.

As of right now using **DynamicEvents** will work on public, private, and static methods. **Gotcha -** <mark style="color:red;">If the method and class is static an exception may be thrown</mark>. I have yet to test this out but <mark style="color:red;">be warned</mark>.&#x20;

For **Async games/apps** where most functionality is <mark style="color:blue;">async/multi-threaded</mark> consider using the **Unity Job System(**&#x77;ith DOT&#x53;**)** or combine <mark style="color:blue;">async Task/void</mark> methods with <mark style="color:blue;">Parallel.For / Parallel.ForEach</mark> when doing CPU intensive work. <mark style="color:blue;">Parallel.For / Parallel.ForEach</mark> will still block the **UI / main thread** if not used with <mark style="color:blue;">async void</mark> or <mark style="color:blue;">async Task</mark> methods. <mark style="color:green;">Do your own research if you don't know how to use</mark> <mark style="color:green;"></mark><mark style="color:green;">**asynchronous**</mark> <mark style="color:green;"></mark><mark style="color:green;">or</mark> <mark style="color:green;"></mark><mark style="color:green;">**Parallel**</mark> <mark style="color:green;"></mark><mark style="color:green;">programming</mark>. <mark style="color:red;">The advice I have mentioned above is not for all use cases and also considered bad in most contexts (like</mark> <mark style="color:blue;">async void</mark> <mark style="color:red;">or using async with</mark> <mark style="color:blue;">Parellel.ForEach</mark><mark style="color:red;">), especially outside of</mark> <mark style="color:red;"></mark><mark style="color:red;">**Unity Game Engine**</mark> where <mark style="color:red;"><mark style="color:blue;">async<mark style="color:blue;"></mark> is the standard for performant applications. Keep in mind <mark style="color:green;">Parallel programming</mark> <mark style="color:red;">(</mark>as well as <mark style="color:blue;">async</mark><mark style="color:red;">)</mark> comes with a lot of gotchas that could make your code hard to debug or actually make performance worse if not used correctly. Do your own research and avoid async until you have a good understanding of how to use it in Unity
