https://github.com/barionlp/commandsystem
An automated and extendable command system as Unity package
https://github.com/barionlp/commandsystem
csharp dotnet unity
Last synced: 9 months ago
JSON representation
An automated and extendable command system as Unity package
- Host: GitHub
- URL: https://github.com/barionlp/commandsystem
- Owner: BarionLP
- License: mit
- Created: 2023-06-26T16:09:16.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2025-04-20T07:58:49.000Z (about 1 year ago)
- Last Synced: 2025-05-21T23:11:19.121Z (about 1 year ago)
- Topics: csharp, dotnet, unity
- Language: C#
- Homepage:
- Size: 67.4 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Command System
A system for calling funcions via text input
(made for Unity, can be used outside)
## Usage (Unity)
- (optional) install UpmGitExtension package (https://github.com/mob-sakai/UpmGitExtension.git)
- install Command Sytem package (https://github.com/BarionLP/CommandSystem.git)
### Run Commands
```csharp
CommandManager.Execute("command");
```
### Set Logger
```csharp
CommandManager.SetLogger(new UnityDebugCommandLogger()); //print logs in the unity console
// Custom Logger
public class CustomLogger : ICommandLogger
{
public void Log(string message) => Debug.Log(message);
public void LogWarning(string message) => Debug.LogWarning(message);
public void LogError(string message) => Debug.LogError(message);
}
```
### Simple Commands
```csharp
CommandManager.Commands.Register(new SimpleCommand("test", ()->{
//Do something
//Log
CommandManager.Log();
CommandManager.LogWarning();
CommandManager.LogError();
}));
```
### Advanced Commands
```csharp
//register static methods with the Command attribute
// atomatically generates syntax hints
CommandManager.Commands.Register();
//register command "cmd" with the methods as subcommands (e.g. "cmd add 4 2")
CommandManager.Commands.RegisterGroup("cmd");
public class TestCommands
{
[Command("add")] //automatically parses primitive arguments
public static void Add(int left, int right) => CommandManager.Log($"{left} + {right} is {left+right}");
[Command("wait")] // supports async
public static async Task Test(float seconds) => await Task.Delay((int)(seconds*1000));
[Command("give")] //other objects require custom parsers (see below)
public static void GiveItem(Item item) => Player.Give(item);
}
```
### Parsers
```csharp
//has built in parsers for most primitives (float, int, uint, short, ...) see in `ArgumentParser.cs`
ArgumentParsers.Register(new CustomParser()); // register a custom parser
ArgumentParsers.Register(new EnumArgumentParser()); //generates an enum parser (parses by name)
public class CustomParser : IArgumentParser
{
public CustomType Parse(ReadOnlySpan raw) => //however you want, e.g. from a registry (`null` if fails)
public IEnumerable GetSuggestions() => Enumerable.Empty(); //or all possible values
}
```