dnGLua / API

GLua APIs for C#
https://discord.gg/uxW9S2eBtM
2 stars 0 forks source link

Easy chat-command API #2

Open Cheatoid opened 3 years ago

Cheatoid commented 3 years ago

By using Reflection, it is possible to make certain things easier. The goal is to make it really easy to add chat-commands for Starfall projects. V1 code example:

[ChatCommand("test")] // Permission defaults to myself-only.
public static void TestCmd(BasePlayer invoker, string args) {
    Assert(invoker == Owner, "BUGCHECK");
    printf("Test chat command.  args=%s", args);
}

[ChatCommand("hidden", HideChat=true)] // Hides owner's chat
public static void HideTestCmd(BasePlayer invoker, string args) {
    print("Hidden command entered.");
}

[ChatCommand("join", ChatCommandPermission.Anyone)] // Let anyone run this
public static void JoinCmd(BasePlayer invoker, string args) {
    printf("%s has joined the party!", invoker.Name);
}

The above design could be upgraded by implementing a parser and have arguments fully-typed out method. V2 code example:

[ChatCommand("install")]
public static void InstallCmd(BasePlayer invoker, string name, int num = 5, bool flag = false) {
    printf("Installing...  name=%q num=%i flag=%s", name, num, flag);
}

This version is slightly more complex to make it work, but it would be nicer and more fluent. To run the command, you'd say: /install --name "a string here" --flag true Which should output the following:

Installing... name="a string here" num=5 flag=true

Cheatoid commented 3 years ago

Moved to vNext; This will be added in future version.