Simple and practical Unity state machine Design

Source: Internet
Author: User

State machine is a very common game programming mode, state machine design also has simple or complex differences.

The state machine in my mind

What is the state machine like? This is a very typical state machine design (readily written):

//Status Class    classState {//saved state machine referencesStateMachine _machine; //construct state, save state Machine reference         PublicState (StateMachine machine) {_machine =Machine ;} //Enter Status         Public Virtual voidOnEnter () {}//Leave State         Public Virtual voidOnleave () {}//Update Status         Public Virtual voidOnUpdate () {}}//state Machine Class    classStateMachine {//Current statusState _state; //Change State         Public voidchangestate (state newstate) {_state.            Onleave (); _state=newstate; _state.        OnEnter (); }        //Update current status        voidUpdate () {_state.        OnUpdate (); }    }

This code mainly expresses the idea that:

1. The state contains 3 overridable methods to enter, update and leave.

2. The state machine and the state of the relationship is 1 to many;

3. At most one state machine within a state is "active" at the same time.

Why didn't you see the Transition? Because I think adding this class loses more than it gets. On the one hand the state shift should be in the design rather than in the code, on the other hand the essence of the transfer is to perform a state switch (there may be some other things), such a "process" there is no sufficient justification for the object. This is just my personal point of view, if you have what you want to say can give me a message.

The problem with this design

When you use this design, you need to inherit the state class to implement your own state, overriding Onenter,onleave or OnUpdate. Imagine defining a new class to perform a simple operation, and then using this class to create a state instance, and it is likely that this class will only need to be used to create a state instance. For a long time you may wonder what it all means. If that's not what it is: You may have noticed that the state holds a reference to the owning state machine. In these rewriting methods, you inevitably need to access the properties or methods of the owning state machine, so it is necessary to save the state machine's references first. And you may also need to redefine a Get property to convert the basic statemachine to a custom state machine type. Imagine that every time you read a character's health, add a "character." The prefix. How do you make things easier?

The Final Solution

    //Status     Public classState {//Enter         PublicAction OnEnter; //Leave         PublicAction Onleave; //Update         Publicaction<float>onUpdate; }    //State Machine     Public classStatemachine:monobehaviour {//Current status        PrivateState _state;  PublicState State {//Get current status            Get{return_state;} //Toggle Status            Set            {                if(_state! =NULL&& _state.onleave! =NULL) _state.onleave (); _statetime=0; _state=value; if(_state! =NULL&& _state.onenter! =NULL) _state.onenter (); }        }        //Status Time        Private float_statetime;  Public floatStatetime {Get{return_statetime;} }        //update status (called in Fixedupdate,update or lateupdate)        protected voidUpdatestate (floatdeltatime) {_statetime+=Deltatime; if(_state! =NULL&& _state.onupdate! =NULL) _state.onupdate (deltatime); }    }

The state has no virtual or abstract methods, is replaced with a delegate, and no longer holds a reference to the state machine. In practice, it is not necessary to inherit the State class in most cases, but instead to initialize the delegates for the various states, all the methods used to initialize can be written directly to the state machine class, so these methods can directly access all members of the state machine. To make it easier to use in Unity, the state machine inherits the Monobehaviour, and you can choose a different update mode depending on your actual needs. Additionally, state timings are added for easy programming.

What if the state class still requires multiple levels of inheritance? One of the benefits of delegation is freedom. In an inherited class, you can get, replace, or add a delegate's method, and what is not possible?

Simple and practical Unity state machine Design

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.