Unity Finite state machine authoring

Source: Internet
Author: User
Tags fsm

Finite state machine FSM

is an abstraction of the behavioral logic.

In the entire FSM architecture

First, there is a state base class StateObject

There are three methods, which are pre-state, state, state, respectively.

All specific behavior classes inherit this base class, and the logic of each method is implemented in these three methods.

Then, a statemanager (brain) state management class is needed to manage these states, with special attention to the Changestate method inside, which is the key to the state jump.

If you store various states, you can use list classes, dictionary classes, and so on.

Writing:

Build class: Confirm that there are several states, create a new class of corresponding classes, and his manager (management Class) and the parent class state

Code:

1. In the parent class, first public a variable of the management class type so that each state corresponds to a state manager. Then write the state before the state, state after three methods. Note that the types of these three methods are virtual and are convenient for subclasses to rewrite.

Because the state is executed only once before and after, the return value is void. The return value type in the state is the class type, and you can return a null value first.

2. Let all state classes inherit the parent class and override the method inside. Add your own behavioral logic in three methods. Note returns this (the current state itself) in the return in the state

3. Write management Class (emphasis):

1) First the management class also inherits the parent class. Can be made into a state dictionary, storing all the state of an object, which can only be converted in this state dictionary. Note that the using System.Collection.Generic must be used if you want to use a dictionary. Dictionary wording: dictionary<string, state> statedic;

String is used to pass the state name, state is all objects of the current status. Then define the current status: State CurrentState;

and previous status: State LastState;

2) Initialize Statedic = new dictionary<string, state> () in the constructor;

Using Unityengine;

Using System.Collections;

Using System.Collections.Generic;

public class Statemanager:state

{

State management class with a dictionary, is a function class

A state dictionary that stores all states of an object, which can be converted only in this state dictionary.

Dictionary<string, state> statedic;

Current status

State currentstate;

Previous state

State laststate;

Initialization is placed in the constructor

Public Statemanager ()

{

Statedic = new dictionary<string, state> ();

}

Registration status, formal parameters of state name and state parameter required

public void Registerstate (string statename, State State)

{

Each time a state is registered, the field of the state manager that specifies the state corresponds to the registered object.

This refers to the current class object instance dictionary<string, State> (), who. who is Registerstate?

The function is to have multiple states of an object point to the same manager

State.statemanager = this;

Adding to the state dictionary, passing parameter states

Statedic.add (StateName, state);

}

Set default state, pass state name as formal parameter

public void SetDefaultState (string statename)

{

Determines whether the list of methods contains such a state (key value)

if (!statedic.containskey (statename))

{

Debug.logerror ("The status list does not have this status");

}

Set default state

CurrentState = Statedic[statename];

}

Change state

public void Changestate (string statename)

{

Determines whether the list of methods contains such a state (key value)

Failed to change state

if (!statedic.containskey (statename))

{

Debug.logerror ("Status list no this state, switchover failed");

CurrentState = LastState;

}

Toggle success, Change status

CurrentState = Statedic[statename];

}

Execution of the state

public void Dostate ()

{

At the first execution, current is the default state of the set, LastState is empty

if (currentstate! = laststate)//joins the default status of Sleep//current=sleep;last=null//2nd round. Current=eat;last=sleep

{

Currentstate.enterstate ();//sleep method of entry, only once//current=sleep;last=null

Assign the latter state to the current state and jump out of the IF condition

LastState = Currentstate;//current=sleep;last=sleep

}

Execute "in State"

Laststate.staystate ();//sleep's continuous approach//current=sleep;last=sleep

Exit status//sleep Switch to Eat//current=eat;last=sleep

if (laststate! = currentstate)//current=eat;last=sleep

{

Laststate.exitstate (); Sleep exit

}

}

}

Then create a new class that hangs on the game object

Using Unityengine;

Using System.Collections;

public class Fsmmanager_boss:monobehaviour {

Statemanager_boss Manager;

Use this for initialization

void Start () {

Manager = new Statemanager_boss ();

Registration status

Manager. Registerstate ("Idle", New Idlestate_boss ());

Manager. Registerstate ("Run", New Runstate_boss ());

Manager. Registerstate ("Attack", New Attackstate_boss ());

Manager. Registerstate ("Death", New Deathstate_boss ());

Set initial state

Manager. SetDefaultState ("idle");

}

Update is called once per frame

void Update () {

Manager. Dostate ();

}

}

Unity Finite state machine authoring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.