Thursday 20 December 2012

Unity - Playmaker For Beginners

As I have been using a fair amount of Playmaker in Unity for my university project I thought I would do some tutorials on how to set up certain simple components. But before that I'm going to do a quick crash course on what Playmaker is and how to use it.

Playmaker is an add-on for Unity which can be downloaded from the Unity store for around $100 (sometimes this drops and when I picked it up it cost around $25). It is a visual scripting tool for Unity which makes scripting easier for people who do not know the coding sintax. This works well for our university as we are on an art based course so it allows us to do some programming without learning and coding. That being said this is not a miracle program, while it is very useful for me it will have no use to someone who is fluent in a coding language and will have limited use to people who struggle getting their head around the concept of coding.

FSMs

FSM is short for finite state machine and it is a series or events which trigger on certain commands which set how things will react in game. There is no need to understand any complexities about it, it is much more simplistic than it sounds.

When you select and object you wish to use Playmaker on and then open the Playmaker window (Open Playmaker on the top task bar and then Playmaker Editor) you will be presented with an empty editor. To start editing you need to add an FSM by right clicking the empty workspace and selecting 'Add FSM'.

When you select this two boxes will appear. One says 'Start' and it has an arrow pointing to a box saying 'State 1'. Start signifies where the FSM will begin (go figure) and it will begin with 'State 1' unless you state otherwise.



This state is where the magic happens, within this state you can set 'Actions' to tell the state to trigger certain events, you have 'Events' which are used to transition between states and you have 'Variables' which are used to store different types of information which change. An example of a variable will be the Player's health. It may begin at 100 but when damage is done to the Player this variable number will change.

Variables

Let's start simple, the variables can be set up by clicking the variables tab on the left side of the Playmaker window. This takes you to a different page where any variables can be stated quickly and easily.



To declare a variable simply type in a new variable name at the bottom where is says 'New Variable' and then select the variable type. Here is a quick list of some of the different types of variables I use often:

Float : A number with decimal places
Int : Integer - A whole number
Bool : Boolean - A true or false statement. (Sometimes 0 and 1 are used instead of true and flase)
Game Object : A game object within the scene
Vector2 : Used to define the X and Y values of something
Vector3 : USed to define the X, Y and Z valuse of an object, such as it's position in world space

Global variables can be created by clicking the 'Global Variables' button at the bottom of the 'Variables' page then create it as you would normally. Global variables allow other FSMs to read the variable.

As an example to set the health up type 'Health' into the 'New Variable' box and then select the 'Float' type. This sets the health as a number value which can enter decimal points if need be.

Now when you select it you can set the float value to start with, I'll set mine to 100. You can also tick the inspector box if you wish and it will show the variable in real time as you play in the inspector window of Unity.



States

States are simple to set up, right click in the work space and select 'Add New State' simples. You transition between states using events.

Events and Transitions

Events are even more simple to set up as they do not hold any values, they simply wait to be called forwards and then send the FSM in the right direction. To set up an event select the 'Event' tab next to the 'Variable' tab and it will allow you to create events in much the same way as variables. Simply enter the name of the event and hit enter. Here I have created an event called 'Damage'.



The tick box to the left of an event sets it to a global event so that other FSMs can be set to different states depending on what information has been sent. To use the event right click the state you want to transition from and select 'Add Transition' then select the transition you want. You can then left click the new transition and drag the arrow over to the state you want it to go to. Once that event is triggered by an action then the FSM will transition to the next state.



Actions

Actions are used to tell the FSM what to do and to allow you to have control over what the states are doing. These can be created by going to the 'State' tab and the clicking the 'Action Browser' button at the bottom. This will bring up a long list of all the controls you can use, you can search for one by name or you can search for them by category.

For this example I will set it so that when you press a key it will reduce you health to a half, if you ticked to show the 'Health' variable in the inspector you can view this to see if it works.

Got to the action browser and find or search for the action 'Get Key Down', this checks to see if a Player has pressed the key, then select the 'Key' to be whatever key you want to use, I'll use 'E' then set the 'Send Event' function to the 'Damage' event we made earlier. The store result can be left as 'None' for this action.



This now waits for the 'E' key to be pressed and when it is it sends the event 'Damage' which is set to transition from 'State 1' to 'State 2'. The FSM will now complete whatever 'State 2' is set to do, which for now is nothing.

Click on 'State 2' now and open the 'Action Browser' and select 'Set Float Value'. Set the 'Float Variable' to the 'Health' variable we created earlier and and set the 'Float Value' to 50, which is half the health.
Finally add a 'FINISHED' transition which is already set up in Playmaker and link it back to 'State 1' so that it can be done again. If you play the game and press 'E' now you should see you health reduce to 50.

Other Playmaker Information

Playmaker has other useful functions like being able to access scripts and interact with them. You can also watch Playmaker work while you play in real time so you can see exactly where it is and what it's doing, this is exceptionally useful when trying to find out why you FSM is not working.

You can rename states when you have them selected by going to the 'State' tab and typing in the name where it specifies the state name and then hitting enter. This is definitely advised when working with larger FSMs or watching the FSM play to look for bugs.

You can also rename FSMs by clicking the 'FSM' tab and typing the name in the top and hitting enter. This is useful when you have more than one FSM on an object.



I have only been playing with Playmaker for a short while and I'm by no means to programmer or anything of the sort. But I do find it enjoyable and a fun challenge so I intend to play with it much more. So if you have any queries or what some help I'll be happy to try, chances are I won't be able to help but it's worth a try, you can email me by using the email address on the 'Contact Me' tab at the top of this site. Thanks.

No comments:

Post a Comment