Coding


Here, you will learn about the flow to create your own StateBehaviour when using the Arbor.

Please refer to the basics before the If you have not seen yet – Fundamentals.

>>Basic

Preparation of ArborFSM

Create the appropriate GameObject to the scene, please attach the ArborFSM.

Then turn on the Arbor Editor in Open Editor button from ArborFSM of Inspector.

StateBehaviour to output a log

Let’s create a script to issue a future log, depending on the transition of the state.

Creating a script file

First, right-click the Assets folder of the Project browser and select the “Create → Folder”, please the name of the folder can be in the “Scripts”.
(There is no if it is necessary to re-create already.)

The Scripts folder and right-click on the select “Create -> Arbor -> C # Script”, Please refer to the name of the file to be “TestLogBehaviour”.

Coding of TestLogBehaviour

Because I think TestLogBehaviour.cs files are made, please open the editor by double-clicking.

This time I want to code like this.

using UnityEngine;
using System.Collections;
using Arbor;

public class TestLogBehaviour : StateBehaviour
{
	void Awake()
	{
		Debug.Log( "Awake : " + state.name );
	}

	// Use this for initialization
	void Start ()
	{
		Debug.Log( "Start : " + state.name );
	}

	void OnEnable()
	{
		Debug.Log( "OnEnable : " + state.name );
	}

	// Use this for enter state
	public override void OnStateBegin()
	{
		Debug.Log( "OnStateBegin : " + state.name );
	}

	// Use this for exit state
	public override void OnStateEnd()
	{
		Debug.Log( "OnStateEnd : " + state.name );
	}

	void OnDisable()
	{
		Debug.Log( "OnDisable : " + state.name );
	}

	void OnDestroy()
	{
		Debug.Log( "OnDestroy : " + state.name );
	}

	bool updateLogged = false;

	// Update is called once per frame
	void Update ()
	{
		if( !updateLogged )
		{
			Debug.Log ( "Update : " + state.name );
			updateLogged = true;
		}
	}
}

Briefly commentary, and general method that is called by MonoBehaviour, you can define a method called in StateBehaviour, it puts a log of method name and state name.
Also, because the Update method would have been repeatedly referred to, it has to issue a one-time log.

And attach the TestLogBehaviour to state

Let’s try to attach it to create a state already created ArborFSM.

Create a state in the right-click on the Arbor Editor “state creation”.
State name leave tentatively “TestState”.

Then click the state of the gear icon and select the “Add Behaviour -> Scripts -> TestLogBehaviour”.
(Which you can also attach to drag and drop the TestLogBehaviour.cs file directly to the state)

Tutorial01

Now, Now that you have attached, let’s run once.

When you run, it is output log in the Console window.

チュートリアル02

When you exit, you will see the log of the method to be called at the end is.

チュートリアル03

Confirmation of the log at the time of transition

Since then with a, let’s take also of the log if you transition.

Create a state in the right-click the Arbor Editor “state creation”.
State name as “TestState2”.

Next, click the gear icon in the “TestState2”, and then attach the TestLogBehaviour in “Add Behaviour → Scripts → TestLogBehaviour”.

Now that is not transition, and attach the Time Transition from the gear icon in the “Add Behaviour→ Transition → TimeTransition” in “TestState”.
The Seconds to 1, it should be tied to NextState to TestState2.

Tutorial04

You will understand well the transition of flow and check the Console window by running.

チュートリアル05

Describe the transition conditions

I tried to tried to transition in Time, but you may want to provide your own transition conditions.
Here, let’s allowed to transition when the simple you button input to.
(There is a similar function in the built-in Behaviour, but you decided to write directly in that tutorial)

Open a text editor by double-clicking the TestLogBehaviour.cs.
(It can be edited from the gear icon in the script that is attached on the Arbor Editor even from a “script editing”)

It will rewrite as follows.

using UnityEngine;
using System.Collections;
using Arbor;

public class TestLogBehaviour : StateBehaviour
{
	// Add: StateLink
	public StateLink nextState;

	void Awake()
	{
		Debug.Log( "Awake : " + state.name );
	}

	// Use this for initialization
	void Start ()
	{
		Debug.Log( "Start : " + state.name );
	}

	void OnEnable()
	{
		Debug.Log( "OnEnable : " + state.name );
	}

	// Use this for enter state
	public override void OnStateBegin()
	{
		Debug.Log( "OnStateBegin : " + state.name );
	}

	// Use this for exit state
	public override void OnStateEnd()
	{
		Debug.Log( "OnStateEnd : " + state.name );
	}

	void OnDisable()
	{
		Debug.Log( "OnDisable : " + state.name );
	}

	void OnDestroy()
	{
		Debug.Log( "OnDestroy : " + state.name );
	}

	bool updateLogged = false;

	// Update is called once per frame
	void Update ()
	{
		if( !updateLogged )
		{
			Debug.Log ( "Update : " + state.name );
			updateLogged = true;
		}

		// Add: Transition
		if( Input.GetButtonDown( "Fire1" ) )
		{
			if( Transition( nextState ) )
			{
				Debug.Log ( "Transition : " + state.name );
			}
		}
	}
}

The added is,
// Add: StateLink
// Add: Transition
And it is two places that have commented.

If you declare a StateLink in public or [SerializeField] it can be edited on the Arbor Editor, you can transition in Transition.
This time it will transition by the “Fire1” (ie left mouse button down by default).
(In addition, the transition we are logging After successfully for debugging)

Save and transition for the field named Next State to Test Log Behaviour has been added If you look at the Arbor Editor.

The Next State of “TestState2” in connection with “TestState”, let’s try to return the state.

Tutorial06

And Make sure you run.

“TestState” In TestLogBehaviour of Next State because that is not connected no reaction, transition after one second.
The transition by the input of “TestState2” In “Fire1”.
It can be confirmed from the log is that.

チュートリアル07

About StateBehaviour

When using the Arbor, scripts to be attached to the state you must be sure to inherit the StateBehaviour.
I would like to briefly describe this StateBehaviour.

StateBehaviour has inherited the MonoBehaviour, has become able to use it to attach to the state in the sense that you want to attach to the GameObject.
Since the base is MonoBehaviour, in conjunction with the script etiquette and game objects in Unity, such as prefabricated-aware there are no problems as usual approach.
Also, when you want to have some kind of parameters, it can be edited on the Arbor Editor if declared in public or [SerializeField].

And, also you can implement the same editor extension.
Examples, “Arbor -> Scripts -> Editor -> Behaviour” so you see editor extension of the built-in Behaviour from, visit those who are interested.

Arbor is complete tutorial “Coding” in the above!
Because the script can be self-made, of course, or in cooperation with other asset Dari freely teamed up your own logic for the game,
You might say, even the try out in AssetStore by creating an asset like to work with Arbor!