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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Relaunch Giveaway & Discount] Simple Option Stacker - 1.6.0

Discussion in 'Assets and Asset Store' started by Lahzar, Aug 19, 2016.

  1. Lahzar

    Lahzar

    Joined:
    May 6, 2013
    Posts:
    87

    Ladies and gentlemen, I present to you the best system for those who want to write their own AI! Asset Stores first ever Stack-based Finite State Machine! This system greatly improves the Finite State Machine, making it so much easier to write, and so much more flexible. Simple Option Stacker is an addon which works without any extra setup. Will make AI super easy to do. More freedom than visual scripting could ever give you.

    Have you ever wanted to write your own AI, but its too difficult? It gets too messy? Or maybe you know how to write a basic AI, but want to take your game to the next level? Maybe you're tired of the limited freedom the endless visual scripting solutions on the Asset Store offer? Or maybe you're just looking for a damn good AI architecture? Maybe you don't even do AI but need a solid stack system? If any of these are true, this asset is definently for you! If not, do keep reading anyways.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public enum State { Patrol, Alerted, Chase, Combat }
    4. public class ClassicExampleAI : MonoBehaviour {
    5.     private State state = State.Patrol;
    6.  
    7.     public void Update() {
    8.         switch(state) {
    9.             case State.Patrol:
    10.                 Patrol();
    11.                 break;
    12.  
    13.             case State.Alerted:
    14.                 Alerted();
    15.                 break;
    16.  
    17.             case State.Chase:
    18.                 Chase();
    19.                 break;
    20.  
    21.             case State.Combat:
    22.                 Combat();
    23.                 break;
    24.         }
    25.     }
    26.  
    27.     public void Patrol() {
    28.         //PATROL
    29.         //Switch to new state:
    30.         state = State.Alerted;
    31.     }
    32.  
    33.     public void Alerted() {
    34.         //ALERTED
    35.     }
    36.  
    37.     public void Chase() {
    38.         //CHASE
    39.     }
    40.  
    41.     public void Combat() {
    42.         //COMBAT
    43.     }
    44. }
    VS
    Code (CSharp):
    1. using UnityEngine;
    2. using OptionStacker;
    3.  
    4. public class StackExampleAI : MonoBehaviour {
    5.     SFSM_Stack state;
    6.  
    7.     void Awake() {
    8.         state = this.NewStack("Patrol");
    9.     }
    10.  
    11.     void Update() {
    12.         state.Update();
    13.     }
    14.  
    15.     public void Patrol() {
    16.         //PATROL
    17.         //Switch to new state:
    18.         state.Push("Alerted");
    19.     }
    20.  
    21.     public void Alerted() {
    22.         //ALERTED
    23.     }
    24.  
    25.     public void Chase() {
    26.         //CHASE
    27.     }
    28.  
    29.     public void Combat() {
    30.         //COMBAT
    31.     }
    32. }

    Without an SFSM, making new states are a pain in the arse.. AAA games with complicated AI tend to have hundreds of smaller states! Imagine writing an enum and a switch containing every single of these! Even if you go through all this trouble, there are still major problems to overcome.

    What if you want to go back to the previous state? You could either have a variable that keeps track of the last state, or if you have a very simple AI, you could use a Behaviour Tree-like system just like Unity did in their Enemy AI Tutorial. This makes things even more tedious and boring to implement, and uses resources thus causing some lag. Simple Option Stacker takes care of all this under the hood. All you have to do to exit a state and go to the previous state is pop it! In our previous example this would be: state.Pop();

    And the final major problem with Finite State Machines are arguments. Its so complicated and a pain in the arse, we tend to make private variables and change them before going into the state that requires them. Take a look at Unity's Enemy AI Tutorial. With Simple Option Stacker, you would not need atleast the four last variables, as you can use any number of variables just like calling a regular function.

    Code (CSharp):
    1. void Alerted() {
    2.         state.Pop();                            //Remove this instance from the stack
    3.         state.Push("Chase", player.position);   //Put this on top instead. (Using a parameter.)
    4.     }

    Simple Option Stacker also offers Callback Transitions. If you push Patrol, and there is another method called OnEnterPatrol, this OnEnter method will be called before Patrol. The transitions are OnEnter, OnExit, OnSuspended, OnWakeup.

    The title is correct, as a part of relaunching this asset, we are offering a 33% discount, and giving away atleast 5 free copies through the Asset Store. All you have to do is send me a PM agreeing to leave some feedback after trying it. First come, first serve, so be quick!

    Feel free to post any questions you may have below!
     
  2. Lahzar

    Lahzar

    Joined:
    May 6, 2013
    Posts:
    87
    I quickly made this demo which shows how an AI would think using Simple Option Stacker, and how easy it is to program them. (It's not the best example if you do not understand what an SFSM is, though.) It's also included in the current version of the asset.
     
  3. nixter

    nixter

    Joined:
    Mar 17, 2012
    Posts:
    320
    Interesting approach. Your mining demo is working OK, but the Everyday Simulation Demo doesn't load at least not in Firefox, which does support the Unity Plugin. You may wish to consider making a WebGL version.