Skip to main content

Custom commands

Creating custom commands is very easy to do. All you have to do is create a new class which inherits from Command.

using ExtensionTools.Console.Commands;
using ExtensionTools.Console.Commands.Parameters;
using ExtensionTools.Console.Suggestions;
public class SetTagCommand : Command
{
public override string Execute(object[] parameters)
{
throw new System.NotImplementedException();
}

public override string GetCommand()
{
//returns the actual command
return "settag";
}

public override string GetHelpString()
{
//returns the string displayed when using the command incorrectly
return "Set the tag of a GameObject. >settag \"gameObjectName\" \"tag\"";
}
}

Now we have to make sure you can pass parameters to this command, to do this we create the constructor and assign the parameter array to the base constructor.

We assign a ParameterGameObject for the GameObject and ParameterString for the Tag. As you can see we also added a GameObject Suggestion Window to the GameObject Parameter. This will display a popup with all GameObjects in the game to choose from.

IMPORTANT! When using parameters with white space, You should always put the parameter between quotes (for example: >settag Main Camera Untagged should be: >settag "Main Camera" Untagged)

public class SetTagCommand : Command
{
public SetTagCommand():base(new Parameter[] { new ParameterGameObject(new GameObjectSuggestionWindow()), new ParameterString() })
{

}
...
}

We are now ready to actually execute the command, so in the Execute Method we write:

    public override string Execute(object[] parameters)
{
GameObject go = parameters[0] as GameObject;
string tag = (string)parameters[1];

go.tag = tag;

return "Set tag for " + go.name + " to " + tag;
}