Have a method accept generic methods for a in-game console
I'm making a console, like in Counter-Strike so that the devs can add their commands.
I'm making this in Unity's C#.
I have a problem when dealing with the commands. When you want to add a commands, you create a class Command and add it through a method to the Console List of Commands.
The code of the class Command is the following one:
public class Command
{
/// <summary>The string by which this command will be called</summary>
public string commandKey;
/// <summary>The information of this command that the help option will show</summary>
public string helpInfo;
public delegate string stringVoid();
public delegate string stringString(string consoleInput);
public delegate void voidCmd();
public delegate void voidString(string consoleInput);
public stringVoid _stringVoid;
public stringString _stringString;
public voidCmd _voidCmd;
public voidString _voidString;
public Command(string commandInput, string helpTooltip)
{
commandKey = commandInput;
helpInfo = helpTooltip;
}
#region Add Command Methods
public Command addVoidCommand(voidCmd voidCommand)
{
_voidCmd = voidCommand;
return this;
}
public Command addStringVoid(stringVoid stringVoidCmd)
{
_stringVoid = stringVoidCmd;
return this;
}
public Command addStringString(stringString stringStringCmd)
{
_stringString = stringStringCmd;
return this;
}
public Command addVoidString(voidString voidStringCmd)
{
_voidString = voidStringCmd;
return this;
}
#endregion
}
As you can see users can add four types of delegates methods:
Void methods with no parameters.
String methods with no parameters.
Void methods with a string parameter.
String method with a string parameter.
This makes the class very heavy, and makes the users have to navigate through a lot of options to add a class.
I want to have some way to add methods without having to go through so many options, and, from my side, taking out the need of searching through so many options when a command is called from console.
You can find the whole script/project here.
In conclusion, I want to have a way that the user simply write
// way I want it to work
new Command("commandName", "this is an example" ExampleCommand);
new Command("commandString", "this is another example" ExampleString);
void ExampleCommand(){ }
string ExamplesString(string ble){ return ble + "bla"; }
// way it currently works
new Command("commandName", "this is an example").addVoidCommand(ExampleCommand);
new Command("commandString", "this is another example").addStringString(ExampleString);
To simplify everything. Is any way I can achieve this with delegates, Unity Actions or something else?
Thank you very much.
Your answer
Follow this Question
Related Questions
Allocation Messages In Console Won't Go Away [SOLVED] 1 Answer
Console Debug / Print do not show 2 Answers
Traffic Light State Machine Dictionary Issues 2 Answers
Unity run command in console 0 Answers
ForcedScopedThreadAttach 0 Answers