Skip to main content

Events

TavernLib.Utils.TavernEvents in TavernLib.dll. Both sides.

TavernLib's events, as TavernEvent<T> objects you subscribe to. There are two: TavernEvents.SocketCreated here, and Roles.RolesChanged.

TavernEvent​

public sealed class TavernEvents.TavernEvent<T> (nested in TavernEvents)

MemberWhat it does
void Subscribe(Action<T> handler)Adds a handler.
void Unsubscribe(Action<T> handler)Removes it. Pass the same delegate you subscribed.

Raising it is internal, so only TavernLib can. You can't make your own working TavernEvent<T>; use a normal C# event for your mod's events.

To unsubscribe later, subscribe a method or a stored delegate, not an inline lambda. A second identical lambda is a different delegate.

SocketCreated​

public static readonly TavernEvent<ISocket> SocketCreated

Raised each time the game makes its network socket (Alta.Networking.Socket, see Sockets and connections), with that socket. TavernLib raises it from a Harmony postfix on the Socket constructor, after the constructor has set Socket.Current to the new socket.

It's mostly useful for its ConnectionCreated event, which fires for each new Connection on that socket. That's where you set handlers for your own network messages, the way TavernLib sets up its roles message.

Don't throw from a SocketCreated handler

Nothing catches it. An exception goes up through the Socket constructor, so the game fails to make its socket and can't host or join. Wrap your handler's body in a try.

Subscribe in OnInitializeMelon. Sockets are made long after mods load, so you won't miss the first one.

Example​

using Alta.Networking;
using MelonLoader;
using TavernLib.Utils;

public class SocketWatcher : MelonMod
{
public override void OnInitializeMelon()
{
TavernEvents.SocketCreated.Subscribe(OnSocket);
}

public override void OnDeinitializeMelon()
{
TavernEvents.SocketCreated.Unsubscribe(OnSocket);
}

private void OnSocket(ISocket socket)
{
LoggerInstance.Msg("The game made a socket");
socket.ConnectionCreated += connection => LoggerInstance.Msg($"New connection {connection.Identifier}");
}
}

See also​