Ghost Commands
  • Getting Started
    • Quickstart
    • Syntax
      • Command syntax
      • Arguments with multiple values
    • Custom commands
  • Documentation
    • Commands
      • Static commands
      • Non-static commands
      • Dynamic commands
      • Overloads
      • Parameters
      • Additional attributes
    • Suggestions
      • Suggestion attributes
      • Suggestor methods
    • Converters
      • Custom parameter types
      • Using the ArgumentReader
      • Multiple ways to interpret an argument
    • Processors
      • Creating a processor
      • Setting priorities
      • Cheat codes example
    • Macros
    • Settings
    • Customization
    • Included Commands
Powered by GitBook
On this page
  • Adding commands at runtime
  • Removing commands at runtime

Was this helpful?

  1. Documentation
  2. Commands

Dynamic commands

If you want even more control over your commands, they can also be added and removed at runtime by accessing the CommandExecutor class directly.

Adding commands at runtime

Any delegate can be turned into a command using CommandExecutor.AddCommand. The method simply requires a name for the command, along with a delegate.

private void Start()
{
    // The method doesn't return anything, so we'll create an Action.
    CommandExecutor.AddCommand("hello", new Action<string>(Hello));
}

private void Hello(string message)
{
    Debug.Log(message);
}
private void Start()
{
    // The method returns a value, so we'll create Func in this case.
    CommandExecutor.AddCommand("multiply", new Func<int, int, int>(Multiply));
}

private int Multiply(int a, int b)
{
    return a * b;
}
private void Start()
{
    // Alternatively you could create the delegate with a lambda expression. 
    CommandExecutor.AddCommand("hello", new Action<string>((string message) =>
    {
        Debug.Log(message);
    }));
}

Removing commands at runtime

Commands can also be removed at runtime. Even those that were created using the [Command] attribute. To do so, use CommandExecutor.RemoveCommand. The example below shows a great way to keep the player engaged for longer.

CommandExecutor.RemoveCommand("quit");

Note

If you've removed commands that were made using the [Command] attribute, such as any built-in command, you can restore all of them with CommandExecutor.Scan().

PreviousNon-static commandsNextOverloads

Last updated 7 months ago

Was this helpful?