Coding


This time, we will explain how to create your own script, such as log output to know the timing of state change and definition of state transition by original condition.

If you have not already seen the basic section, please read the basic section first.

Basic

In addition, we will not provide a basic explanation of the script in Unity.
For details, refer to the Unity manual “Scripting”.

Unity manual “Scripting”

Operating environment

This tutorial is created in the following environment.

  • Unity 5.3.0f4
  • Arbor 2.2.0
  • Script language : C#

Depending on the version, please understand that there are parts different from the explanation.

Create tutorial project

First of all, please create a project for the tutorial.

Project Name ArborTutorial02
Mode 3D
Assets to import Arbor

For information on creating projects and importing Arbor, please refer to “Getting Started : Preparation for using Arbor”.

Create a log output script

There are two kinds of scripts for Arbor, and this time create StateBehaviour which can be set to state.

First, open the Assets folder in the Project window and select “Arbor> StateBehaviour> C# Script” from the Create button.

Then name the script “DebugLogBehaviour”.

GIF

What is StateBehaviour?

StateBehaviour is a script that can describe the state behavior of ArborFSM like TimeTransition and ActivateGameObject used in the basic section.

In addition to control for state, you can use editable parameters and callback like OnCollisionEnter like MonoBehaviour.

Let’s double-click the DebugLogBehaviour.cs file we created earlier and open it in the IDE.

For newly created scripts, commonly used callbacks are predefined in general.

This OnStateAwake (), OnStateBegin (), and others are callback methods involved in state control.

In brief explanation, it plays the following role.

OnStateAwake() This is called only once before OnStateBegin () at the time of entering the state for the first time.
OnStateBegin() This is called when FSM enter the state.
OnStateEnd() This is called when FSM leave the state.
OnStateUpdate() This is called every frame while entering the state.
As the call timing, it is the same as MonoBehaviour.Update().
OnStateLateUpdate() This is called after every frame Update () while entering the state.
As the call timing, it is the same as MonoBehaviour.LateUpdate().

Also, if you have added multiple behaviors to one state, these callbacks are called in order from the top when you see them in the Arbor Editor window.

For more information about StateBehaviour, see “Customize StateBehaviour”.

Customize StateBehaviour

Log output when entering state

First of all, let’s check the logs displayed at the time of entering the state and make sure that the script is running normally.

Open the DebugLogBehaviour.cs we created earlier in the IDE and write in OnStateBegin().

In addition, we will only describe the edited places, so please understand.

public override void OnStateBegin() {
	Debug.Log("OnStateBegin : " + state.name);
}

Since the state setting script can be referred to by the state property, we also make state name out there in the log.

If you can write it, save the file and return to Unity.

Create ArborFSM object

Create an ArborFSM object to make the state machine available.

From the Create button of the Hierarchy window, select “Arbor > ArborFSM” to create a GameObject with ArborFSM component attached.

GIF

State creation

Open the Arbor Editor window from the created ArborFSM object and create the state.

Set the state name to “DebugLog”.

GIF

Add DebugLogBehaviour

Add DebugLogBehaviour to the DebugLog state.

By default, the created script will be stored in Scripts of the Add Behavior window.

Click the DebugLog state setting icon and select “Add Behaviour”.

Select “Scripts > DebugLogBehaviouir” and add it.

GIF

Since DebugLogBehaviour has no parameters, there is no need to set anything, so it is complete.

Confirm the operation so far

Let’s run once here and check it.

Since it is not possible to check the log if the Console window is not displayed, please select “Window > Console” of the menu and display it if you do not have it open.

Press “Play button” to check the Console window.

Then, OnStateBegin() of DebugLogBehaviour added to the DebugLog state is called, and the log described earlier is now displayed.

Declaration of parameters

Next, let’s declare the parameters in the DebugLogBehaviour and allow it to be set in Arbor Editor.

Open DebugLogBehaviour.cs in the IDE and declare member variables.

Since Arbor can declare parameters by Unity serialization function, the basic is the same as MonoBehaviour.

In this time, add a string to this DebugLogBehaviour and add it to the log with OnStateBegin() to confirm the operation.

Add member variables as follows.

public string logText;

Change OnStateBegin() as follows.

public override void OnStateBegin() {
	Debug.Log("OnStateBegin : " + state.name + " : " + logText);
}

If you save the file and return to Unity, it will be recompiled and the script changes will be reflected.

DebugLogBehaviour setting

Looking at the Arbor Editor window, I think that the parameters added are displayed.

Let’s type “Hello Arbor Script!” Into this added Log Text.

Confirm the operation so far

Let’s check whether the log is displayed correctly.

Please press “Play button” to put it in the execution state and look at the Console window.

I think that you can see that the character string entered in Log Text has been added to the log.

In this way, you can edit the parameters added in StateBehaviour from the Arbor Editor and set the behavior.

Declare state transition destination specification

When StateLink is declared as a member variable, a transition button is displayed and it becomes possible to specify it.

Let’s open DebugLogBehaviour.cs in the IDE and declare StateLink.

Please add it as a member variable as follows.

public StateLink nextState;

In this case, we will make transition when Space key is input with OnStateUpdate().

public override void OnStateUpdate() {
	if (Input.GetKeyDown(KeyCode.Space)) {
		Transition(nextState);
	}
}

Let’s save the file and return to Unity.

You can see that “Next State” has been added to the DebugLogBehaviour in the DebugLog state.

Create another state

Since there is only one state, I will create another state and try to make the transition clearer.

This time we will duplicate the DebugLog state.

Click the DebugLog state setting icon and select “Duplicate”.

GIF

Double-click on the name column to make it “DebugLog2” because you can not tell if the name remains the same.

GIF

Connect transition button

Drag & drop the “Next State” in the DebugLog state to the DebugLog2 state.

Similarly, connect the “Next State” in the DebugLog2 state to the DebugLog state by dragging and dropping.

GIF

Now the state transitions each time the space key is pressed, and the log is displayed in the Console.

Operation check

Let’s check the operation at the end.

Press “Play button” to put it in the execution state.

While watching the Console window, try pressing the space key in the Game window.

GIF

You can see that the state switches each time you press the space key and the log is output.

If you also check the Arbor Editor window, you can confirm that the state is switched normally.

GIF

Refer to “Customize StateBehavior” for more detailed customization methods such as passing data between states and calculator nodes.

Customize StateBehaviour

Next step

That’s it “Coding” is complete.

“Coding” complete tweets

Next time is “Let’s make a coin pusher”.

I will create a simple game like a coin pusher using Arbor.

Let’s make a coin pusher