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
1
Question by youngapprentice · Nov 20, 2012 at 02:58 AM · statemachine

Quick Question on State Machine

Hello, all! I have just set up a finite state machine for my player, and I would like to know this:

Is it better to run all functions of the Player (for instance, the movement script) inside of the Finite State Machine?

Or is it best practice to keep the State Machine in its own script, and have other scripts attached to the player that reference it to get the state?

Thanks- YA!

Comment
Add comment · Show 27
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 sparkzbarca · Nov 20, 2012 at 05:38 AM 0
Share

movement is a state :P

avatar image fafase · Nov 20, 2012 at 06:21 AM 1
Share

If you want to learn on FS$$anonymous$$, you could have a look there http://unitygems.com/fsm1/ (Warning, it makes use of advanced topics)

avatar image youngapprentice · Nov 20, 2012 at 12:12 PM 0
Share

Well yes I know that movement is a state and the State $$anonymous$$achine has a custom Enum called Player with all of the states. The State $$anonymous$$achine switches the enum for a variable 'player' in the script.

Is it better to have scripts reference that State $$anonymous$$achine or is it best practice to have all scripts pertaining to the player that work with the State $$anonymous$$achine be smooshed into it?

avatar image AlucardJay · Nov 20, 2012 at 12:31 PM 0
Share

Interesting question , both main and the above comment. In the 3D buzz 3rd person character system there are 3 scripts on the character : Controller : $$anonymous$$otor : Animator ; and the State Engine was in the Controller script. All the scripts were static instances (Singletons) so there seemed to be no problem sending enum variables in functions called by all the scripts. When I tried this for myself but not using singletons, I had a problem where the name of the enum existed on another script but I couldn't just put the variable of the enum in the function e.g. $$anonymous$$oveChar( GoLeft ); . That left me just using one script for my player/enemies with the state engine. Saying that, I also use separate trigger and physical colliders often with their own scripts, which talk to their parent object script. Hope this generates some more traffic for you =]

avatar image AlucardJay · Nov 20, 2012 at 12:38 PM 0
Share

Or is the question : If my script has 2000 lines, but most are 3 line functions, and only a few of those functions run per Update, is this efficient?

How you script is up to you. If code is efficient is also up to you! But the principle is simple : what am I doing on a per-frame basis?

Or the approach I have started using is one script for each behaviour. So movement , targetting, shooting, even getting inputs. Then when you start a new project, just drop your movement script onto a cube and instant game. Once you have written the code once, why write it again? I think this approach is called object orientated , although that could be more about monobehaviours generally looking after themselves.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Eclectus · Nov 22, 2012 at 06:57 PM

I'd recommend encapsulating it in its own script:

  1. It is important enough to be encapsulated in a separate file, since it is responsible for the player state, and this is non trivial.

  2. Readers of the code including yourself will see "PlayerFiniteStateMachine" and be reminded of its responsibility.

  3. Your Player file will be smaller and more encapsulated. Left unchecked, classes like Player can grow very bloated over time. The more code you have in a class file, the more you have to read/scroll/consider to get at what you want.

  4. Pulling out the FiniteStateMachine into a separate class/file makes it easier to run diff tools on the file, allows other programmers to work on it in parallel with less source control conflicts.

  5. It also decreases the amount of errors per file - if you have state errors, you can fix these in isolation, without thinking about other errors you may have in Player.

  6. This will allow you to follow the Single Responsibility Principle

Comment
Add comment · Show 6 · 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 youngapprentice · Nov 22, 2012 at 07:24 PM 0
Share

This is interesting to me as well and it makes sense. Thank you for your commentary.

avatar image Eclectus · Nov 22, 2012 at 07:47 PM 0
Share

$$anonymous$$y pleasure - this sort of encapsulation really does reduce headaches, and make code bases live longer. It is also easy to to extract scripts for use in other projects :)

avatar image youngapprentice · Nov 22, 2012 at 08:12 PM 0
Share

One other thing, what is the advantage of making the, say, Player FS$$anonymous$$ its own Class? I'm not going to have multiple instances of it, right? So where would the advantage be? Thanks!- YA

avatar image Eclectus · Nov 23, 2012 at 10:10 AM 0
Share

The advantage is is that by making it its own class, you encapsulate it. The responsibilities of the Player class are broader then the responsibilities of the 'PlayerFiniteState$$anonymous$$achine'. You want to keep them separate, just like you keep cutlery in a different drawer from other kitchen stuff. It makes it easier to find/organise things.

avatar image Eclectus · Nov 24, 2012 at 10:55 PM 2
Share

Both :) Its own file and its own class. Aim for this 80% or more of the time, as the rule rather than the exception.

Show more comments

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

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make an object appear after collecting items? 3 Answers

problem with my simple script please help. 2 Answers

Copy animator controller, is it even possible? 3 Answers

Can someone explain to me the Unity3D API ?! 1 Answer

How to fix code errors? 1 Answer


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