YarnCommandattribute, or manually, using the
YarnCommandattribute lets you expose methods in a
MonoBehaviourto Yarn Spinner.
YarnCommandattribute to a method, you specify what name the command should have in Yarn scripts. You can then use that name as a command.
FadeCamera.cs, you can run this code in your Yarn scripts like this:
static, you call it with the name of the game object you want the command to run on.
CharacterMovement.cs, create a new game object called
MyCharacter, and attach the
CharacterMovementscript to that game object, you can run this code in your Yarn scripts like this:
YarnCommandmay take the following kinds of parameters:
false. Additionally, the name of the parameter is interpreted as
nullwill be passed.
nullwill be passed.
AddCommandHandlertakes two parameters: the name of the command as it should be used in Yarn Spinner, and a method to call when the function is run.
AddCommandHandlerthat takes parameters, you must list the types of those parameters.
camera_lookin the Dialogue Runner you attach.
AddCommandHandlermethod and the
YarnCommandattribute. Both of these provide effectively the same functionality, and under-the-hood the
YarnCommandattribute is even a wrapper around the
AddCommandHandlercall. So if there are two different ways to achieve the same thing when should you use each one?
YarnCommandattribute allows you to tag specific methods as being a command, Yarn Spinner will then automatically handle the binding and connection of the the command in text to the method call in C#.
AddCommandHandlermethod allows you to manually connect a method in C# to a command in Yarn, letting you set the name of the command and which method it connect to, giving you the control over the binding.
YarnCommandattribute is the better option, it is easier to use and maps well to how we find most people use commands, that is to say calling specific methods on specific GameObjects. This convenience however does come at a cost of flexibility as your
YarnCommandseither need to be on static methods or follow specific calling conventions which may not be what you need or want. The
YarnCommandworks best in our opinion when your commands are calling into specific GameObjects in your scene, so it works very well for moving, animating, or changing characters and items in a scene. For larger gameplay changing moments such as loading new scenes, or moving between dialogue and the rest of your game, or for more global events like declaring a save should happen or an achievement has been unlocked the
AddCommandHandlermethod is better.
YarnCommandattribute performs a search on your project to locate and bind commands to specific method calls which the
AddCommandHandlerdoes not have to do. While for the most part this lookup will go by unnoticed, on larger projects you may find this adds a noticeable performance penalty.
YarnCommandattribute, or the
AddCommandHandlermethod, and the method you're using it with is a coroutine (that is, it returns
WaitForSeconds), Yarn Spinner will pause execution of your dialogue when the command is called.
<<wait>>. (You don't have to do this in your own games, because
<<wait>>is already added for you, but this example shows you how you'd do it yourself.)
YarnFunctionattribute, or the
AddFunctionmethod on a Dialogue Runner. These work very similarly to commands, but with two important distinctions: