Skip to content

Repository files navigation

Command System

A system for calling funcions via text input (made for Unity, can be used outside)

Usage (Unity)

Run Commands

CommandManager.Execute("command");

Set Logger

CommandManager.SetLogger(newUnityDebugCommandLogger());// print logs in the unity console// Custom LoggerpublicclassCustomLogger:ICommandLogger{publicvoidLog(stringmessage)=>Debug.Log(message);publicvoidLogWarning(stringmessage)=>Debug.LogWarning(message);publicvoidLogError(stringmessage)=>Debug.LogError(message);}

Simple Commands

CommandManager.Commands.Register(newSimpleCommand("test",()->
{// Do something// LoggingCommandManager.Log();
CommandManager.LogWarning();
CommandManager.LogError();}));

Advanced Commands

// register static methods with the Command attribute// automatically generates syntax hintsCommandManager.Commands.Register<TestCommands>();// register command "cmd" with the methods as subcommands (e.g. "cmd add 4 2")CommandManager.Commands.RegisterGroup<TestCommands>("cmd");publicclassTestCommands{[Command("add")]// automatically parses primitive argumentspublicstaticvoidAdd(intleft,intright)=>CommandManager.Log($"{left} + {right} is {left+right}");[Command("wait")]// supports asyncpublicstaticasyncTaskTest(floatseconds)=>awaitTask.Delay((int)(seconds*1000));[Command("give")]// other types require custom parsers (see below)publicstaticvoidGiveItem(Itemitem)=>Player.Give(item);}

Parsers

// has built in parsers for most primitives (float, int, long, short, ...) see `ArgumentParser.cs`ArgumentParsers.Register<CustomType>(newCustomParser());// register a custom parserArgumentParsers.Register<CustomEnum>(newEnumArgumentParser<CustomEnum>());// generates an enum parser (parses by name)publicclassCustomParser:IArgumentParser<CustomType>{publicCustomTypeParse(ReadOnlySpan<char>raw)=>// however you want, e.g. from a registry (`null` if fails)publicIEnumerable<string> GetSuggestions()=>Enumerable.Empty<string>();// or all possible values}

About

An automated and extendable command system as Unity package

Topics

Resources

Stars

12 stars

Watchers

1 watching

Forks

Contributors

Languages