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

How to do State pattern?

Discussion in 'Scripting' started by tensio, Jan 1, 2015.

  1. tensio

    tensio

    Joined:
    Oct 17, 2014
    Posts:
    2
    Hello,

    I 'm making a game in c #, I have the character and enemy classes and I would implement the State pattern to the class enemy.

    The state of enemies are:
    -sleep
    -walk
    -ofensive

    Initially it would be asleep and when the player approached a little the state change to Walk, and get even more change to Offensive .

    I have seen many examples but I dont understand , too complex for me or i cant apply to my code.

    As I have understood the patron state maybe would have these classe :
    Enemy.cs
    State.cs
    StateSleep.cs
    StateWalk.cs
    StateOffensive.cs

    Can someone help me with the structure code? And how to call the states?


    Thanks!
     
    Last edited: Jan 1, 2015
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    There's nothing special about it. Put 3 scripts on your object. When some trigger event happens, disable the working script and enable the transition to script.
     
    Kiwasi likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Well that certainly simplifies a lot of my FSMs. Silently cursing myself as an idiot at the moment. Going to attempt this pattern for simple FSMs in future.