Unity Game FrameWork—framework learning—process encapsulation of state machine

Start of the process

UGF encapsulation: ProcedureComponent
The function of the process component is relatively simple and clear, such as the registration process, the initialization process, the startup process and the duration of the acquisition process.
The following code
creates a process manager in awake

public override void Awake()
{
    
    
    base.Awake();

    m_ProcedureManager = GameFrameworkEntry.GetModule<IProcedureManager>();
    if (m_ProcedureManager == null)
    {
    
    
        Log.Fatal("Procedure manager is invalid.");
        return;
    }
}

Three things are mainly completed in Start,
1, 81 lines, Activator.CreateInstance creates the instance of the process selected in the monitor panel
2, 97 lines, calls the initialization method of the process process manager, and executes the OnInit(ProcedureOwner procedureOwner) of the process method.
3. Line 99, the start process, the first process to enter when starting the game.
Regarding the second matter, there may be some ambiguity. It can be seen from the traceability of the printed log that the initialization of the process is finally handed over to the finite state machine to complete. Trace the source through the log, and then look down.
insert image description here
insert image description here
GF package:
ProcedureManager (IProcedureManager): The functions defined in the process manager are basically the same as those of the process.
Line 97 in ProcedureComponent calls the initialization method of the process manager, and the call in the process manager is to create the state machine CreateFsm.
insert image description here
The creation of the state in the state machine is handed over to Fsm to complete, and
insert image description here
the OnInit method is called immediately after the Fsm is created to initialize each process.
insert image description here
ProcedureBase: Process base class, abstract class. Defines the initialization, entry, polling, exit, and destruction life cycle methods of the process. The process base class itself inherits the finite state machine state base class.
Let's go back and look at the switching process in the use of the module, using ChangeState(procedureOwner);. The ChangeState method is derived from the finite state machine FsmState.
After analyzing the code, the essence of the process is actually a state machine. Creating a process is creating a state machine, and the process is a layer of encapsulation of the state machine.
State machine traceability
UGF package: (list script functions)
FsmComponent finite state machine component, create, destroy, and determine whether it exists.
GF package: (list script functions)
IFsm: interface function, get finite state machine (name, holder, number of states, whether it is running normally, duration, current state, all states, data (set, remove, get)) , Start the finite state machine, whether there is a finite state machine state. Focus on the implementation of state machine functions
FsmBase: base class functions, initialize holder type, destroy, poll. Focus on the state machine process
FsmState: the state base class, which defines the virtual method of the process life cycle, and the process base class inherits from the state machine state base class.
Fsm : FsmBase, IReference, IFsm create a finite state machine, and call the life cycle method in the finite state machine state base class FsmState at an appropriate time.
FsmManager: manages the creation, acquisition, and destruction of state machines.
Let's take a look at how the state machine completes the polling start call, and have a deeper understanding of the relationship between the state machine and the process.
Polling start
and end Polling initiator: the Update method of the BaseComponent component.
Inheritance relationship: BaseComponent: GameFrameworkComponent: MonoBehaviour
BaseComponent: Initiate polling

private void Update()
{
    GameFrameworkEntry.Update(Time.deltaTime, Time.unscaledDeltaTime);
}

GameFrameworkEntry: poll all modules

public static void Update(float elapseSeconds, float realElapseSeconds)
{
    foreach (GameFrameworkModule module in s_GameFrameworkModules)
    {
        module.Update(elapseSeconds, realElapseSeconds);
    }
}

FsmManager: Finite state machine manager (one of the modules, state machine module)
The process class in m_Fsms is created in the initialization method Initialize of the ProduceManager process manager. The foreach process polling will determine whether the process is destroyed, and the destruction is skipped. Only execute the Update of the current process.

internal override void Update(float elapseSeconds, float realElapseSeconds)
{
    m_TempFsms.Clear();
    if (m_Fsms.Count <= 0)
    {
        return;
    }
    foreach (KeyValuePair<TypeNamePair, FsmBase> fsm in m_Fsms)
    {
        m_TempFsms.Add(fsm.Value);
    }
    foreach (FsmBase fsm in m_TempFsms)
    {
        if (fsm.IsDestroyed)
        {
            continue;
        }
        fsm.Update(elapseSeconds, realElapseSeconds);
    }
}

Fsm: The finite state machine calls the current state machine Update

internal override void Update(float elapseSeconds, float realElapseSeconds)
{
    if (m_CurrentState == null)
    {
        return;
    }
    m_CurrentStateTime += elapseSeconds;
    m_CurrentState.OnUpdate(this, elapseSeconds, realElapseSeconds);
}

ProcedureBase : FsmState: the end point of polling (inherited by the process class)

public virtual void OnUpdate(IFsm<T> fsm, float elapseSeconds, float realElapseSeconds)
{
}

Guess you like

Origin blog.csdn.net/qq_37619255/article/details/129144406