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 sportsfan78 · May 13, 2013 at 09:40 PM · updatebest practicesstates

Best Way To Script State-Specific Behavior

Hi all. I'm working on a RTS-ish game. I want units to Update differently according to whether they are moving around, performing an ability or idle.

I thought I was being super clever by just turning Update into a wrapper function that called a delegate. I could then change the delegate function to update the unit in a state appropriate way. I then read an article that said delegates can be slow. http://sicklebrick.com/?p=411

So is there a method that is commonly acknowledged as better for handling state specific actions? Should I attach state components and enable/disable them as necessary? Should I just make a big if else wrapper in my Update function that calls state specific update functions?

Cheers

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
2
Best Answer

Answer by rutter · May 13, 2013 at 10:00 PM

Honestly, your delegate method sounds fine to me. Is it running okay?

The article you mentioned is probably more concerned with very heavy use of reflection and delegation. It does eventually get to be a problem, but sometimes it's the right tool for the job.

In terms of other options, the two you mentioned could work in a pinch. I'm not sure that they'd run any significantly faster or slower, though. Other things going on are probably hugely more expensive in CPU time.

Comment
Add comment · Show 4 · 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 whydoidoit · May 13, 2013 at 10:08 PM 1
Share

@Rutter Totally agreed. @sportsfan78 presu$$anonymous$$g that you are using a real delegate, not invoking a $$anonymous$$ethodInfo, then its nearly as fast as a native method call, and nothing to be afraid of.

avatar image sportsfan78 · May 13, 2013 at 10:08 PM 1
Share

The delegate method works fine as far as I can tell. I was planning on using it regularly when I came across that article, and I didn't want to set myself up for a performance nightmare just because I neglected to ask someone with experience.

avatar image sportsfan78 · May 13, 2013 at 10:10 PM 0
Share

Oh, terrific! Thanks @Rutter and @whydoidoit!

avatar image whydoidoit · May 13, 2013 at 10:10 PM 0
Share

That article references reflection and delegation in the same sentence - that sounds alarm bells with me. Reflection is slow, but fine for configuration and setup. Delegation (by using delegates not invokation) is as fast as it gets for dynamic program$$anonymous$$g.

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

16 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

Related Questions

Best way to check object selection 0 Answers

How can I check if I pressed F in the class other than Update()? C# 2 Answers

Update not behaving like it should with enum states 1 Answer

Using else if 4 Answers

Best Practices: Import Assets or Build in Unity 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