Skip to content

dev:Event Arguments

Väinämö Łūmikērø edited this page Feb 2, 2017 · 10 revisions

Event Arguments (Dynamic Events)

F2Core.EventArguments

Event Arguments are used to define dynamic events fired by extensions.


Construction

EventArguments(string handler)
EventArguments(string handler, string invoker)
EventArguments(string handler, string invoker, params object[] params)
  • string handler
    The name/handler of this event. Should start with the namespace of the extension.
  • string invoker
    The namespace of the extension invoking this event. Not necessary when the event is handled by the same extension.
  • object[] params
    Parameters to be sent with this event.

Properties

  • object CallbackReturn
    Obsolete.
  • string EndpointId
    The id of the client sending the event.
  • string EventHandler
    The name/handler of this event. Should start with the namespace of the extension.
  • string Invoker
    The namespace of the extension invoking this event.
  • object[] Parameters
    Parameters sent with this event.

Methods

string ToClient()

string ToClient()

Converts this event into a string eligable to be sent from the server.


string ToServer()

string ToServer()

Converts this event into a string eligable to be sent from a client.

Note: The result of these functions might be the same but the way it is created is different.
Calling ToClient() in a client instance will fail and vice-versa ToServer() fails when called in a server instance.


Examples

The following example creates an event called ml.festival.demo.request after entering /demo <id> in the console which gets sent to the specified client.
When the client receives the event, an event called ml.festival.demo.answer containing the current time is sent back and the time is printed to the server console.

[...]

public void OnEnable() {
    RegisterServerListener(new OnConsoleInputReceived(ConsoleInputReceived), Event.ConsoleInputReceived);
    RegisterServerListener(new OnServerDynamicEvent(ServerDynamicEvent), Event.Dynamic);
    RegisterClientListener(new OnClientDynamicEvent(ClientDynamicEvent), Event.Dynamic);
}

public delegate void OnConsoleInputReceived(string input);

public void ConsoleInputReceived(string input) {
    if (CommandParser.IsMatch(input, "demo .+", this)) {
        var Id = input.Remove(0, 6);
        var Packet = new EventArguments("ml.festival.demo.request");
        ExtensionPool.Server.SendPacketTo(Id, Packet.ToClient());
    }
}

public delegate void OnClientDynamicEvent(EventArguments e);

public void ClientDynamicEvent(EventArguments e) {
    if (e.EventHandler == "ml.festival.demo.request") {
        var Packet = new EventArguments("ml.festival.demo.answer", "", DateTime.Now.ToShortTimeString());
        ExtensionPool.Client.SendPacketToServer(Packet.ToServer());
    }
}

public delegate void OnServerDynamicEvent(EventArguments e);

public void ServerDynamicEvent(EventArguments e) {
    if (e.EventHandler == "ml.festival.demo.answer") {
        ExtensionPool.Server.PrintToConsole($"The client {e.EndpointId} received the event at {e.Parameters[0]}.", Color.Black);
    }
}

The example is using CommandParser.IsMatch() to check the command. See Command Handling why this is useful.