Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CheekySparrow78 · Mar 02, 2020 at 03:20 PM · serializationscriptableobjectstate-machinestate machine

FSM State implementation — SO, SO+wrappers, custom classes?

I've been wracking my head about this for a couple weeks, so maybe, just maybe, someone could chime in and point me towards the right direction.

My FSM design was initially based on Scriptable Objects for States using the Observer pattern - basically all state data lived on the State machine script and states modified/fetched it through 'owning FSM' reference.

Then I felt the need to implement state queuing, which meant that States could no longer be stateless, they had to contain individual state data. Since Scriptable objects are shared data, I went ahead and devised a 'StateWrapper' class. It contained data for a particular instance of a State and referenced the Scriptable Object that contained the State logic (already getting complicated, huh?).

Next question became obvious - why can't I ditch the Scriptable Objects altogether and just implement States using custom classes. Short answer is - preinitialized States. For debugging purposes I want to be able to 'initialize' some State instance with data (for example, setting the Waypoints inside a Patrol class). It's very easy with Scriptable Objects - I just make a copy of SO, drag and drop Waypoints into the corresponding Scriptable Object field, and then drag this "preinitialized State" into Starting State field, that's it. I also have "Compound states", which are basically State containers (Scriptable Objects, holding a list of State Scriptable objects and executing them sequentially, for example, "Interact" compound state is a State Container that contains "Move" state, "Wait" state and "Use" state in one package - it moves the character to target object, pauses a little and then uses the object). So all of the above is nice and dandy with Scriptable Objects, but I still wanna ditch the unwieldy "StateWrapper class + State Scriptable Object" construction. I now have two things I'm thinking off:

  1. Make State Machine use INSTANCES of Scriptable objects, i.e. each new State would be created using "Instantiate(StateScriptableObject)". This way the States could contain instance data and be stateful. However I'm not sure if creating Instances of Scriptable Objects is feasible from performance point of view (I suspect this operation might be slower than instantiating a custom class through 'new' keyword, and that could be a problem). There might be other pitfalls with this approach.

  2. Refactor states into custom serializable classes and write a Property Drawer for them, use custom editor/editor window for the ease of debugging/initializing states. I'm not sure about this one either, won't I be, in fact, reinventing the Scriptable Object? Any insights/hints would be greatly appreciated.

UPD: Ok, so I've benchmarked 100 000 Instantiate Scriptable Object calls vs 100 000 "new StateClass" calls. It seems like "Instantiate SO" is significantly slower (Time elapsed = 00:00:03.453288 vs 00:00:00.0230978). I guess this answers my question.

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

0 Replies

· Add your reply
  • Sort: 

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

130 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 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 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 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 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 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 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

Saving players progress in ScriptableObject asset 1 Answer

Field is member of a type which is serializable but is of type which is not serializable 0 Answers

Share data between statemachinebehaviours not with scriptableobject 0 Answers

The Quantum Animator? - Animator behaving differently when animation controller is open in editor 0 Answers

Make the same serialized class as a ScriptableObject? 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