Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Help replicating a C++ HFSM / Inheritance example in C#

Discussion in 'Scripting' started by robdood, Feb 18, 2021.

  1. robdood

    robdood

    Joined:
    Jan 18, 2021
    Posts:
    14
    Hi all, I'm having a go at creating my own Hierarchical Finite State Machine, after reading through the excellent chapter at http://www.gameprogrammingpatterns.com/state.html

    Here's the C++ psuedocode:
    Code (CSharp):
    1. class DuckingState : public OnGroundState
    2. {
    3. public:
    4.   virtual void handleInput(Heroine& heroine, Input input)
    5.   {
    6.     if (input == RELEASE_DOWN)
    7.     {
    8.       // Stand up...
    9.     }
    10.     else
    11.     {
    12.       // Didn't handle input, so walk up hierarchy.
    13.       OnGroundState::handleInput(heroine, input);
    14.     }
    15.   }
    16. };
    So I've tried to replicate and tailor to what I need, and have this:
    Code (CSharp):
    1. public class PlayerIdleState : PlayerGroundedState
    2. {
    3.     public override void HandleInput(Player player, InputMessage input)
    4.     {      
    5.         if (input things happen)
    6.         {
    7.             // do stuff
    8.         }
    9.         else
    10.         {
    11.             //do grounded state input handler
    12.             PlayerGroundedState.HandleInput(player, input);
    13.         }              
    14.  
    15.     }

    However, the eagle eyed amongst you will presumably have noticed, this doesn't work as I'm told I need an instance of the PlayerGroundedState class in order to access the method.

    So, I figure I'm either going ot have an instance of each possible state against my player or there's a bit of notation / syntax in C# I'm unaware of to access the parent's similarly-named methods.

    Is there an equivalent of that C++ double colon in C#? Or do I need to create instances (static or otherwise) of each state in order to 'walk up' them?

    Is it because the base state class is abstract, perhaps it shouldn't be? I'm fumbling in the dark a little here despite trying to read up on this... /shame

    Or: how would you fix this? :)

    Any help much appreciated.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    C# uses the keyword base to access members of the parent. So change line 12 from
    PlayerGroundedState.HandleInput(player, input);
    to
    base.HandleInput(player, input);
     
    robdood and PraetorBlue like this.
  3. robdood

    robdood

    Joined:
    Jan 18, 2021
    Posts:
    14
    Thanks @Antistone - I was literally just coming back here to post that I'd got an answer somewhere else :D

    /feelsslightlydumb :)
     
  4. theembracedone

    theembracedone

    Joined:
    Jul 31, 2019
    Posts:
    22
    Please don't be one of those "nvm i figured it out" kind of forum posters who announce they have the solution but dont bother to post it. People will find this thread via the search function or via their search engine and be met with an announcement of your success, but not the solution to your question!
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    What's wrong with the answer already posted right above that?
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    Yeah it appears OP's answer to the problem was the same as antistone's, so no need to repeat it. Like the OP was coming back to post the answer, but saw antistone already posted it.