Register: Difference between revisions

From RAGE Multiplayer Wiki
(Created page with "Registers a command at runtime. ==Syntax== <syntaxhighlight lang="C#" >bool NAPI.Command.Register(MethodInfo method, CommandArgs = null);</syntaxhighlight> '''Optional Argum...")
 
m (Xabi moved page NAPI.Command.Register to Register)
 
(No difference)

Latest revision as of 13:09, 27 November 2019

Registers a command at runtime.

Syntax

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)

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!");
            }
        }
    }
}