# YarnCommandAttribute

Class in [Yarn.Unity](https://docs.yarnspinner.dev/2.1/api/csharp/yarn.unity)

Inherits from [`YarnActionAttribute`](https://docs.yarnspinner.dev/2.1/api/csharp/yarn.unity/yarn.unity.yarnactionattribute)

## Summary

An attribute that marks a method on an object as a command.

```csharp
public class YarnCommandAttribute : YarnActionAttribute
```

## Remarks

When a [DialogueRunner](https://docs.yarnspinner.dev/2.1/api/csharp/yarn.unity/yarn.unity.dialoguerunner) receives a `Command`, and no command handler has been installed for the command, it splits it by spaces, and then checks to see if the second word, if any, is the name of an object.

By default, it checks for any `GameObject`s in the scene. If one is found, it is checked to see if any of the `MonoBehaviour`s attached to the class has a [YarnCommandAttribute](https://docs.yarnspinner.dev/2.1/api/csharp/yarn.unity/yarn.unity.yarncommandattribute) whose [CommandString](https://docs.yarnspinner.dev/2.1/api/csharp/yarn.unity/yarn.unity.yarncommandattribute/yarn.unity.yarncommandattribute.commandstring) matching the first word of the command.

If the method is static, it will not try to inject an object.

If a method is found, its parameters are checked:

* If the method takes a single `string`\[] parameter, the method is called, and will be passed an array containing all words in the command after the first two.
* If the method takes a number of parameters equal to the number of words in the command after the first two, it will be called with those words as parameters.
* If a parameter is a `GameObject`, we look up the object using `GameObject.Find(string)`. As per the API, the game object must be active.
* If a parameter is assignable to `Component`, we will locate the component based on the name of the object. As per the API of `GameObject.Find(string)`, the game object must be active. If you'd like to have a custom injector for a parameter, use the [YarnParameterAttribute](https://docs.yarnspinner.dev/2.1/api/csharp/yarn.unity/yarn.unity.yarnparameterattribute).
* If a parameter is a `bool`, the string must be `true` or `false` (as defined by the standard converter for `string` to `bool`). However, we also allow for the string to equal the parameter name, case insensitive. This allows us to write commands with more self-documenting parameters, eg for a certain `Move(bool wait)`, you could write `<<move wait>>` instead of `<<move true>>`.
* For any other type, we will attempt to convert using `Convert.ChangeType(object, Type, IFormatProvider)` using the `System.Globalization.CultureInfo.InvariantCulture` culture. This means that you can implement `IConvertible` to add new accepted types. (Do be aware that it's a non-CLS compliant interface, according to its docs. Mono for Unity seems to implement it, but you may have trouble if you use any other CLS implementation.)
* Otherwise, it will not be called, and a warning will be issued.

This attribute may be attached to a coroutine.

{% hint style="info" %}
The [DialogueRunner](https://docs.yarnspinner.dev/2.1/api/csharp/yarn.unity/yarn.unity.dialoguerunner) determines if the method is a coroutine if the method returns `IEnumerator`.
{% endhint %}

If the method is a coroutine, the DialogueRunner will pause execution until the coroutine ends.

## Properties

| Name                                                                                                                                                  | Description                                        |
| ----------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------- |
| [CommandString](https://docs.yarnspinner.dev/2.1/api/csharp/yarn.unity/yarn.unity.yarncommandattribute/yarn.unity.yarncommandattribute.commandstring) |                                                    |
| [Injector](https://docs.yarnspinner.dev/2.1/api/csharp/yarn.unity/yarn.unity.yarncommandattribute/yarn.unity.yarncommandattribute.injector)           | Override the state injector for this command only. |
