dimanche 14 janvier 2018

Alternatives to event handlers?

Let's say you have some background service running (in my case, administrating a game server), and you want to expose the ability to code plugins. For this, the most intuitive (and in most cases, probably the best) approach would be to use events, like OnChat, OnSpawn, etc., to which the plugins can subscribe. I have been down this route already. Now you have code like the following in every plugin that subscribes to the OnChat event:

void OnChat(String from, String content, int team, int squad) {
    if (content.split(" ")[0] == "metro") { // <--- this line
        // player wants to vote for the next map called "metro"
    }
}

// in another plugin
void OnChat(String from, String content, int team, int squad) {
    if (content.split(" ")[0] == "!ban" && from == "some_admin") { // <-- again
        // swing banhammer
    } else {
        // permissions denied
    }
}

The problem is that all subscribed OnChat functions are called every time a player says something. Again, in most cases this is fine since the performance hit is negligible, and the added complexity of working around this would be worse. But intrigued by what alternative approaches there might be, here I am asking you.

Aucun commentaire:

Enregistrer un commentaire