• 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 komodor · Jan 21, 2014 at 11:38 AM · state machineoop

how to extend states for character

I am working on a 2d game in unity3d (c#) with the character controlled by the arrow keys and with 2 action buttons.

My problem is simple, I can solve it, but the solution seems bit dirty to me. I am using a very simple state machine, the hero is moving in horizontal or vertical way (floor and ladder)

my current state extending is:

 HeroState -> HeroStateStatic -> HeroStateMovement

Then I extend static or movement to final states:

 HeroStateMovement -> HeroStateRunning or HeroStateStatic -> HeroStateLookUp

however I am always calling a huge external function (in Hero class) which decides what to do next and then switch the state

I believe I should have this reasoning within the state itself but states at ladder has different set of checks and possible choices then states at floor and I don't know how to rework those classes to have all necessary code written just one (that's why now is in hero and not in the states) ... is there anything like cross-extending technique?

(I know how to use abstract classes and interfaces but it probably does not solve my trouble)

thanks for your help

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by HappyMoo · Jan 21, 2014 at 02:37 PM

You're extending too much. Don't extend unless you want to share code. Even then, it's probably better to push code that all hero states can use into HeroState and just call it from the other states and not extend.

Also, to handle all your transitions within your state machine, the states need to change the state when needed:

 if (jumpButtonPressed) hero.changeState(new JumpState());
 OR
 if (jumpButtonPressed) hero.changeState(HeroState.JumpState);

see this article for more details: http://gameprogrammingpatterns.com/state.html

Also read the other patterns on the side for example the Component pattern, which unity also uses to dodge the hell of multiple object hierarchies: http://gameprogrammingpatterns.com/component.html

Comment
Add comment · Show 3 · 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 komodor · Jan 21, 2014 at 03:46 PM 0
Share

i want to share that code, but i don't want it accessible from states which can't use it; i understand that i can bend the rule of clean OOP code a bit, but yes i wanted to have it perfect

ex-colleague just advised me to use composition inside those states, so one level would be extending states and the second would be based on position: something like state Static / $$anonymous$$ovement and components like OnFloor / OnLadder

i guess i'll try this one over other ex-colleagues advice to have state machine for each main state because the second option still needs to have same code on different places

the think is there is so fat 50 animations in 30 different states and there will be more and when you need to change one action it must be on one place only

thank you for the answer, it helps me, because i see i wasn't completely wrong

Edit: too bad that c# doesn't support multiple inheritance, then it would be possible the way i asked :D

avatar image Jamora · Jan 21, 2014 at 03:50 PM 0
Share

Single inheritance already causes seven kinds of hell, why would you ever want multiple inheritance?

avatar image komodor · Jan 21, 2014 at 05:40 PM 0
Share

to be able to inherit from more classes ? :D

imagine states $$anonymous$$ovement, Static, AtFloor, AtLadder - if you combine them you can get StaticAtFloor StaticAtLadder $$anonymous$$ovingAtFloor $$anonymous$$ovingAtLadder where half of functionality goes from $$anonymous$$ovement / Static second half from AtFloor / AtLadder, when you have 50 states to manage than it looks like nice idea to me and if it will be written well then it would work for sure

each class i would use to inherit from means 1 dimension of extension for me (i might be wrong of course)

Edit: there is lot of reasoning in the process and i am kinda lost right now, because i have loooong if/else where i am asking what hero wants to do and if he can do it - so i hace the code in one place, but i put it into the states i won't need to ask if he can do it but only if he wants to

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Push GUI button, change another objects variable 2 Answers

Prefab inheritance without scripts. Is it possible? 2 Answers

Duplicating GameObject with variations of the same functionality. 0 Answers

How to Extend/Extrude a Mesh 0 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