Skip to main content

Register

Registers a command at runtime.

Syntax

```csharp bool NAPI.Command.Register(MethodInfo method, CommandArgs = null); ```

Optional Arguments

  • CommandArgs: null by default

NOTE: This function returns a bool, true if successful and false if failed.

Usage example(s)

```csharp namespace myresourcename {

   public static class Commands // Make sure to not inherit Script class in order to avoid registering the command on stratup
   \{
       [Command] // Don't forget to add the Command Attribute
       public static void TestCmd(Client sender) // Method must be static!
       \{
           NAPI.Util.ConsoleOutput("TestCommand Invoked!");
       \}
   \}

   public class Main : Script
   \{
       [ServerEvent(Event.ResourceStart)]
       public void ResourceStart()
       \{ 
           var methodInfo = Type.GetType("myresourcename.Commands").GetMethod("TestCmd"); // Reflection: Type.GetType("Namespace.Class").GetMethod("Method");
           if (methodInfo != null) 
           \{
               var args = new CommandArgs \{InvokeString = "test", Description = "Some description"\};
               var success = NAPI.Command.Register(methodInfo, args);
               if (success) NAPI.Console.WriteLine("Command added successfully!");
           \}
       \}
   \}

} ```