Building a Hierarchical State Machine in Unity: Troubleshooting Virtual Functions
Автор: vlogize
Загружено: 2025-10-11
Просмотров: 0
Discover how to effectively create a `hierarchical state machine` in Unity and solve common issues with virtual functions using this comprehensive guide.
---
This video is based on the question https://stackoverflow.com/q/68720042/ asked by the user 'Caleb' ( https://stackoverflow.com/u/16628588/ ) and on the answer https://stackoverflow.com/a/68721400/ provided by the user 'SpicyCatGames' ( https://stackoverflow.com/u/5710382/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: Building a hierarchical state machine in Unity, having trouble with virtual functions
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Building a Hierarchical State Machine in Unity: Troubleshooting Virtual Functions
When developing a game in Unity, implementing a state machine for player characters can make managing various states easier and more intuitive. However, when working with hierarchical state machines, you may run into issues, especially concerning the inheritance of virtual functions. If you've found yourself puzzled by why certain methods are not being called as expected, you’re not alone. In this post, we'll look at a specific case where the expected output from a state machine does not match reality, and we'll go through the steps needed to resolve the issue.
Understanding the Problem
In our scenario, we're working with a state machine consisting of a PlayerScript and multiple PlayerStates. We expect the system to print a specific sequence of debug logs when updating the player state, but instead, we're only seeing a part of the output. Here's the intended output after one frame of execution:
Entered Stopped State
Blip
Blop
Blorp
Instead, the actual output is:
Entered Stopped State
Blorp
The crux of the issue lies in how we are defining our methods in the LowPriorityState.
Analyzing the Code
Let's take a step back and examine how the state machine is set up. Here’s the relevant code:
Player Script
[[See Video to Reveal this Text or Code Snippet]]
Player States
Base State Class: PlayerState
[[See Video to Reveal this Text or Code Snippet]]
Derived State Class: LowPriorityState
[[See Video to Reveal this Text or Code Snippet]]
Most Derived Class: StoppedState
[[See Video to Reveal this Text or Code Snippet]]
What's Going Wrong?
The crux of the issue is in the way we defined the Update method in the LowPriorityState class:
[[See Video to Reveal this Text or Code Snippet]]
Using the new keyword here creates a new method that hides the base class method instead of overriding it. Thus, when we invoke State.Update(this), it is still calling the overridden method in the base class PlayerState, which is why we are only seeing "Blorp" after the state entry message.
The Solution
To fix this issue, we need to change the new keyword to override, which ensures that we’re truly overriding the base class method rather than hiding it. Here’s the corrected version in the LowPriorityState:
Corrected LowPriorityState Class
[[See Video to Reveal this Text or Code Snippet]]
Conclusion
By making this change, your hierarchical state machine in Unity will route calls to the correct overridden methods, allowing you to see the expected sequence of debug output. It’s essential to be mindful of the differences between method overriding and method hiding in C# , as this can significantly impact how your code behaves. With this fix, your state machine should work seamlessly, providing an effective way to manage different states for your game characters. Happy coding!
Доступные форматы для скачивания:
Скачать видео mp4
-
Информация по загрузке: