SlideShare a Scribd company logo
Unity3D Scripting:
State Machine
Finite-State Machine
Definition:
This is a set of five elements:
(Σ, Q, s ∈ Q, T ⊂ Q, δ : Q x Σ → Q)
where Σ - the alphabet, Q - a set of states,
s - the initial (start) state, T - the set of
accepting states, δ - the transition function.
A graph with a finite number of states (nodes)
and a finite number of CMV transitions
(curves).
Two Questions
How this system would help us in
programming the game logic?
How this approach is better than others?
How To Use
Behavior of the game object may be
divided into a list of different actions:
Starting State
Target Searching
Attack
Death
Structure
Generalization
Any game component, whether a unit, or
a menu based or an abstract game as a
whole, which uses certain rules can be
programmed by using a state machine.
Advantages of State Machine
1. Divided, limited access to the context of state
2. Encapsulated logic for each state
3. Clarity, ability to upgrade code (source code
customization)
4. Reliability
Divided, Limited-Access Context State
Each state controls only its own context
and there is no need to check the set of
variables for other states
Logic Encapsulation for One State
This allows you to quickly respond to
errors that were found during testing, to
localize them and make changes. At the
same time this greatly reduced the
likelihood of introducing errors into
other states, because their context is not
available, and their logic is elsewhere.
Source Code Customization
Behavior of the object becomes more
classification-ruled and understandable.
Behavior modernization takes less time
and complexity associated with
modifications is greatly reduced.
Reliability
Transition from one state to another
occurs only in specific cases, to move
from one state to another if it was not
planned is not possible.
Property of C #: Operator: “Yield"
Operator: “Yield"
● "syntactic sugar"
● implement pattern: enumerator
Is being used for:
● implementing collection
● dividing the methods in several steps
Unity 3D - Specifications
We will use several features Unity3D:
● Operator yield and pattern enumerator.
● Call for one of the standard object methods
inherited from MonoBehaviour:
- Update
- FixedUpdate
- Start, etc.
Unity 3D - Specifications
We need the following entities:
● States
● Manager, which manages the transition
from one state to another
● Model - context of state machine, to
which the state will call to perform a
variety of actions.
State. Implementation Example
State. Description
No need to restrict the user with
inheritance. System is fairly
abstract, to restrict certain
properties of the state through the
interface.
States Manager: Implementation Example
States Manager: Explanation
Key methods:
● Set the initial state
● Infinite state that calls the logic
of the current status
● Cleanup
Infinite cycle. Implementation Example
Infinite Cycle. Clarification
● Possibility of correct completion
● Debug messages output
● Calling IState.Start (), IState.Cleanup (),
IState.DoWork () methods
● Switch state with the help of Field
NextGameState
IState.DoWork (). Implementation Example
Awaiting Something
Check one of the conditions, and if it does not
suffice you pass control higher up and go back
to check conditions at other times when the
manager would transfer control method.
This way you give a chance to any other logic to
be executed in a single game flow (GAME
THREAD), avoiding a whole bunch of problems
related to multithreading.
Methods
For any application, we need a method to invoke
the script cyclically inherited from
MonoBehaviour: Update, FixedUpdate,
LaterUpdate, etc.
Depending on the task you should choose the
right method and call it for update the state
machine.
Before you update the state machine it is
necessary to perform the initial state install. This
can be done easily in the Start or Awake
methods.
Model (context)
Often there is a need preserve some information
between the states was preserved some
information - the context of state machine. This
context could be a script that renews the state
machine. It is possible to store certain flags and
objects necessary for the states in this script.
Also, this context may contain links to game
resources and other game objects. So you pass
a reference from state to state to this script.
This approach will help to avoid a large number
of singletons in the game.

More Related Content

PPT
State Machine Diagram
PPT
Seminar State Chart1
PPTX
behavioral model (DFD & state diagram)
PPT
5.state diagrams
 
PPT
State Diagrams
PPT
Slide 6 Activity Diagram
PDF
Unity Programming
State Machine Diagram
Seminar State Chart1
behavioral model (DFD & state diagram)
5.state diagrams
 
State Diagrams
Slide 6 Activity Diagram
Unity Programming

Viewers also liked (12)

PPTX
Unity 3d Basics
PPT
Securing PHP Applications
PPTX
Code and Memory Optimisation Tricks
PDF
NYPF14 Report - CDA
PDF
Unity introduction for programmers
PPTX
Game Development with Unity
PPTX
Unity3D Programming
PPTX
Unity is strength presentation slides
PPT
게임 프레임워크의 아키텍쳐와 디자인 패턴
PPT
Unity presentation
PDF
게임 개발에 자주 사용되는 디자인 패턴
PPTX
Introduction to Unity3D and Building your First Game
Unity 3d Basics
Securing PHP Applications
Code and Memory Optimisation Tricks
NYPF14 Report - CDA
Unity introduction for programmers
Game Development with Unity
Unity3D Programming
Unity is strength presentation slides
게임 프레임워크의 아키텍쳐와 디자인 패턴
Unity presentation
게임 개발에 자주 사용되는 디자인 패턴
Introduction to Unity3D and Building your First Game
Ad

Similar to Unity3D Scripting: State Machine (20)

PDF
Developing the State Machine with code c# for application and game development
PPTX
Mgd finite statemachine
PPTX
States, state graphs and transition testing
PPTX
FSM.NET presentation
PPT
Fdocuments.in chapter 11-states-state-graphs-and-transition-testing
PPTX
Тарас Леськів “Game Programming Patterns and Unity”
PDF
Start Building a Game and Get the Basic Structure Running
PDF
Managing application-state-final
PDF
20031109 WRUG Presentation
PDF
Rapid prototyping with ScriptableObjects
PDF
Finite automata
PPTX
States, state graphs and transition testing
PPTX
03 fsm how_toimplementai_state_20161006_jintaeks
PDF
Game Programming 13 - Debugging & Performance Optimization
PDF
State, State Graphs and Transition testing: state graphs, good & bad state gr...
PDF
OGDC 2014_Architecting Games in Unity_Mr. Rustum Scammell
PDF
Thinking Outside the Synchronisation Quadrant
PPTX
Unit 4- State Machine in mobile programming
PPTX
State-Machine-Diagram.pptx
PPTX
Module 3.1.pptx
Developing the State Machine with code c# for application and game development
Mgd finite statemachine
States, state graphs and transition testing
FSM.NET presentation
Fdocuments.in chapter 11-states-state-graphs-and-transition-testing
Тарас Леськів “Game Programming Patterns and Unity”
Start Building a Game and Get the Basic Structure Running
Managing application-state-final
20031109 WRUG Presentation
Rapid prototyping with ScriptableObjects
Finite automata
States, state graphs and transition testing
03 fsm how_toimplementai_state_20161006_jintaeks
Game Programming 13 - Debugging & Performance Optimization
State, State Graphs and Transition testing: state graphs, good & bad state gr...
OGDC 2014_Architecting Games in Unity_Mr. Rustum Scammell
Thinking Outside the Synchronisation Quadrant
Unit 4- State Machine in mobile programming
State-Machine-Diagram.pptx
Module 3.1.pptx
Ad

More from Sperasoft (20)

PDF
особенности работы с Locomotion в Unreal Engine 4
PDF
концепт и архитектура геймплея в Creach: The Depleted World
PPTX
Опыт разработки VR игры для UE4
PPTX
Организация работы с UE4 в команде до 20 человек
PPTX
Gameplay Tags
PDF
Data Driven Gameplay in UE4
PPTX
The theory of relational databases
PPTX
Automated layout testing using Galen Framework
PDF
Sperasoft talks: Android Security Threats
PDF
Sperasoft Talks: RxJava Functional Reactive Programming on Android
PDF
Sperasoft‬ talks j point 2015
PDF
Effective Мeetings
PDF
Unreal Engine 4 Introduction
PDF
JIRA Development
PDF
Introduction to Elasticsearch
PDF
MOBILE DEVELOPMENT with HTML, CSS and JS
PDF
Quick Intro Into Kanban
PDF
ECMAScript 6 Review
PDF
Console Development in 15 minutes
PDF
Database Indexes
особенности работы с Locomotion в Unreal Engine 4
концепт и архитектура геймплея в Creach: The Depleted World
Опыт разработки VR игры для UE4
Организация работы с UE4 в команде до 20 человек
Gameplay Tags
Data Driven Gameplay in UE4
The theory of relational databases
Automated layout testing using Galen Framework
Sperasoft talks: Android Security Threats
Sperasoft Talks: RxJava Functional Reactive Programming on Android
Sperasoft‬ talks j point 2015
Effective Мeetings
Unreal Engine 4 Introduction
JIRA Development
Introduction to Elasticsearch
MOBILE DEVELOPMENT with HTML, CSS and JS
Quick Intro Into Kanban
ECMAScript 6 Review
Console Development in 15 minutes
Database Indexes

Recently uploaded (20)

PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Spectroscopy.pptx food analysis technology
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Encapsulation theory and applications.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Big Data Technologies - Introduction.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
MIND Revenue Release Quarter 2 2025 Press Release
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectral efficient network and resource selection model in 5G networks
Spectroscopy.pptx food analysis technology
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
sap open course for s4hana steps from ECC to s4
Building Integrated photovoltaic BIPV_UPV.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Review of recent advances in non-invasive hemoglobin estimation
Diabetes mellitus diagnosis method based random forest with bat algorithm
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Encapsulation theory and applications.pdf
Machine learning based COVID-19 study performance prediction
Big Data Technologies - Introduction.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Programs and apps: productivity, graphics, security and other tools
MIND Revenue Release Quarter 2 2025 Press Release

Unity3D Scripting: State Machine

  • 2. Finite-State Machine Definition: This is a set of five elements: (Σ, Q, s ∈ Q, T ⊂ Q, δ : Q x Σ → Q) where Σ - the alphabet, Q - a set of states, s - the initial (start) state, T - the set of accepting states, δ - the transition function. A graph with a finite number of states (nodes) and a finite number of CMV transitions (curves).
  • 3. Two Questions How this system would help us in programming the game logic? How this approach is better than others?
  • 4. How To Use Behavior of the game object may be divided into a list of different actions: Starting State Target Searching Attack Death
  • 6. Generalization Any game component, whether a unit, or a menu based or an abstract game as a whole, which uses certain rules can be programmed by using a state machine.
  • 7. Advantages of State Machine 1. Divided, limited access to the context of state 2. Encapsulated logic for each state 3. Clarity, ability to upgrade code (source code customization) 4. Reliability
  • 8. Divided, Limited-Access Context State Each state controls only its own context and there is no need to check the set of variables for other states
  • 9. Logic Encapsulation for One State This allows you to quickly respond to errors that were found during testing, to localize them and make changes. At the same time this greatly reduced the likelihood of introducing errors into other states, because their context is not available, and their logic is elsewhere.
  • 10. Source Code Customization Behavior of the object becomes more classification-ruled and understandable. Behavior modernization takes less time and complexity associated with modifications is greatly reduced.
  • 11. Reliability Transition from one state to another occurs only in specific cases, to move from one state to another if it was not planned is not possible.
  • 12. Property of C #: Operator: “Yield" Operator: “Yield" ● "syntactic sugar" ● implement pattern: enumerator Is being used for: ● implementing collection ● dividing the methods in several steps
  • 13. Unity 3D - Specifications We will use several features Unity3D: ● Operator yield and pattern enumerator. ● Call for one of the standard object methods inherited from MonoBehaviour: - Update - FixedUpdate - Start, etc.
  • 14. Unity 3D - Specifications We need the following entities: ● States ● Manager, which manages the transition from one state to another ● Model - context of state machine, to which the state will call to perform a variety of actions.
  • 16. State. Description No need to restrict the user with inheritance. System is fairly abstract, to restrict certain properties of the state through the interface.
  • 18. States Manager: Explanation Key methods: ● Set the initial state ● Infinite state that calls the logic of the current status ● Cleanup
  • 20. Infinite Cycle. Clarification ● Possibility of correct completion ● Debug messages output ● Calling IState.Start (), IState.Cleanup (), IState.DoWork () methods ● Switch state with the help of Field NextGameState
  • 22. Awaiting Something Check one of the conditions, and if it does not suffice you pass control higher up and go back to check conditions at other times when the manager would transfer control method. This way you give a chance to any other logic to be executed in a single game flow (GAME THREAD), avoiding a whole bunch of problems related to multithreading.
  • 23. Methods For any application, we need a method to invoke the script cyclically inherited from MonoBehaviour: Update, FixedUpdate, LaterUpdate, etc. Depending on the task you should choose the right method and call it for update the state machine. Before you update the state machine it is necessary to perform the initial state install. This can be done easily in the Start or Awake methods.
  • 24. Model (context) Often there is a need preserve some information between the states was preserved some information - the context of state machine. This context could be a script that renews the state machine. It is possible to store certain flags and objects necessary for the states in this script. Also, this context may contain links to game resources and other game objects. So you pass a reference from state to state to this script. This approach will help to avoid a large number of singletons in the game.