Skip to content

Add async/Task support for event handlers #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JakeSmokie opened this issue Dec 23, 2023 · 3 comments
Open

Add async/Task support for event handlers #218

JakeSmokie opened this issue Dec 23, 2023 · 3 comments
Labels
Milestone

Comments

@JakeSmokie
Copy link

I would like to request a feature to allow asynchronous event handlers using the Task type. This would enable more flexibility and performance for handling complex or long-running events.

Currently, the GameEventHandler<T> delegate only accepts a HookResult as the return type, which limits the use of async and await keywords. For example, in this code snippet:

public delegate HookResult GameEventHandler<T>(T @event, GameEventInfo info) where T : GameEvent;

public void RegisterEventHandler<T>(GameEventHandler<T> handler, HookMode hookMode = HookMode.Post) where T : GameEvent
{
    var name = typeof(T).GetCustomAttribute<EventNameAttribute>()?.Name;
    RegisterEventHandlerInternal(name, handler, hookMode == HookMode.Post);
}

There is no way to make the handler parameter an async function that returns a Task<HookResult>.

I propose adding a new delegate type, such as AsyncGameEventHandler<T>, that supports Task<HookResult> as the return type, and a corresponding method to register it, such as RegisterAsyncEventHandler<T>. This would allow users to write event handlers like this:

public async Task<HookResult> OnPlayerDeath(PlayerDeathEvent @event, GameEventInfo info)
{
    // Do some async work here
    await Task.Delay(1000);
    // Return the hook result
    return HookResult.Continue;
}

This feature would be very useful for scenarios where the event handler needs to perform some asynchronous operations, such as database queries, web requests, or other I/O tasks. It would also improve the responsiveness and scalability of the game server by avoiding blocking the main thread.

Thank you for your consideration.

@roflmuffin
Copy link
Owner

If we do support Async game event handlers, then they would have to return void (or just Task in this case), since the game thread needs to immediately know whether or not to block the event, so hook result is somewhat useless in that context.

@JakeSmokie
Copy link
Author

JakeSmokie commented Dec 24, 2023

Yep, I think in those cases we don't need HookResult.
We can use Task as result type.

@roflmuffin roflmuffin added the enhancement New feature or request label Dec 26, 2023
@roflmuffin
Copy link
Owner

We would also need to make a copy of the game event values (as a readonly version), since the actual game event values disappear as soon as the next frame occurs

@roflmuffin roflmuffin added untriaged New issue has not been triaged area-API-GameEvents labels Feb 22, 2024
@roflmuffin roflmuffin added this to the Future milestone Feb 22, 2024
@roflmuffin roflmuffin removed the untriaged New issue has not been triaged label Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants