Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sriram · Dec 01, 2010 at 10:12 AM · stackfinite-state-machine

How to implement a basic Stacked based Finiste state machine design?

Hi!, In the past week i have learnt and implemented a simple game using unity, where i have a character who has to do a set of obstacles to complete a level and as of now my character is able to jump and hit.

I was going through some design patterns and have found a method called 'Stack based Finite State Machine' which would allow me to stack my events like jump,hit,kick(more to be added) into stacks where i would call my respective functions so it would be easier for me to add new features and i will have a clean code throughout.

I have looked through unity answers/forums and havent been able to find out of how i should design/implement it. If anyone could help me out with this/show me where i can look for like a tutorial on how to implement it, it would be great!

Thanks in advance!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by jammingames · Aug 26, 2014 at 04:23 AM

This is an old question, but if you're looking for an extremely simple stack based FSM this may help you note the second class can be in it's own script, it just extends List to include basic Push(); and Pop(); methods

 using System.Collections.Generic;
 using System.Collections;
 using System;
 
 public class FSM
 {
         List<System.Action> states;
 
         public FSM ()
         {
                 this.states = new List<System.Action> ();
         }
 
         public void DoState ()
         {
                 var currentStateFunction = GetCurrentState ();
                 
                 if (currentStateFunction != null)
                         currentStateFunction ();
         }
 
         public System.Action PopState ()
         {
                 return states.Pop ();
         }
 
         public void PushState (System.Action state)
         {
                 if (GetCurrentState () != state)
                         states.Push (state);
         }
 
         public System.Action GetCurrentState ()
         {
                 return states.Count > 0 ? states [states.Count - 1] : null;
         }
 }
 
 public static class ListExtensions
 {
 
         public static T Pop<T> (this List<T> theList)
         {
                 var local = theList [theList.Count - 1];
                 theList.RemoveAt (theList.Count - 1);
                 return local;
         }
 
         public static void Push<T> (this List<T> theList, T item)
         {
                 theList.Add (item);
         }
 }






Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by HeywoodFloyd · Dec 01, 2010 at 05:57 PM

Mono has two stack classes (that I know of). I use C#, and would use System.Collections.Generic.Stack, because generics are cool.

Make a base class for the actions you want to push on the stack, and have all your actions specialize it. If you don't want to specialize MonoBehaviour, you can make them an internal classes of whatever MonoBehaviour is managing your stack, or you can put it in the Standard Assets folder, ensuring it's compiled before your other scripts. (I like the internal classes solution so I don't have to remember where I put files, but it leads to really big files.)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sriram · Dec 02, 2010 at 07:15 AM 0
Share

Thanks for your reply, but i'm still very confused on how to actually implement it.As of now my player does 2 things, he kicks and jumps.Could you maybe give me an example of ow to create a stack fsm wherein i can use both these events as states and push and pop them whenever necessary.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Creating a stack 1 Answer

whats the best technique for managing character animations and movement? 1 Answer

Can't retrieve stack information from a dll 1 Answer

Post Processing Stack Bloom Intensity C# 2 Answers

Behaviour trees: Looking for a way in 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges