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 Arkade · Jun 27, 2019 at 10:29 AM · performancecoroutinetiming

Most performant way to schedule a callback at a time, in the next frame and be able to insert extras once scheduled.

I'm looking for the (1) most performant (inc. GC friendly) approach to scheduling an asynchronous callback (per-Behaviour Tree ticks). I need to schedule both (2) "in N seconds from now" and (3) "next frame" however I also need to (4) add scheduled times in. (5) Any given Behavior Tree should only be ticked once per frame. (Requirements numbered to aid referring back to them.)


There's an added complexity to (5):
Req (3) ("next frame") is mostly due to the work in one tick requiring another tick -- just not in this frame. However sometimes an externally prompted tick (e.g. from a pathfinding result) hits in the same frame as a time-scheduled tick. When this happens order matters: it's fine to skip the time-scheduled one if we already ticked this frame but the other way round, the prompted-by-work-done-in-this-frame one needs deferring to the next frame. To start with I'm checking in the tick() to see if already done this frame and deferring any if so. This does occasionally result in 'backed-up' ticks (ticking every frame for 5 frames when we get several that attempt to tick at the same time and they all serialize pointlessly). Still need to fix this.


Potentials


Invoke

I started with by testing the obvious naive approach Invoke(methodName, delay) but struggled with (3) to find a value of delay that guarantees 1 frame later (`Time.deltaTime` sometimes works, sometimes calls in the same frame = disaster). Obviously it's likely only good for (2).


Coroutine

Coroutines don't seem to fit since if one is already in WaitForSeconds(5), we can't insert a new tick (4) since the Coroutine would need interrupting (stopping and recreating?) and Coroutines aren't great for GC anyway)


DOTween

I tried the usually excellent DOTween's Sequence with InsertCallback() but same problems as Invoke().


Update()

The only other solution I can think of is per-frame checking, ideally a single one between all Behaviour trees. Probably a time-ordered List with the tree to tick, minimum time at which to call (for req 2), optional minimum frame number (for req 3). I'll need to search and insert for (4).


This all seems rather messy which makes me worry I've missed other potential solutions.

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

Answer by Arkade · Jun 29, 2019 at 07:51 PM

After some discussion with peers, concluded no better way than to write a scheduler. Created one that is added to the GameObject of the BehaviorManager that is shared between BTs. This had several advantages:


  • enables/disables itself when work to be done = good for (1),

  • allowed scheduling by both time (2) and frame (next frame) (3),

  • scheduling a new tick for either of these is easy (4),

  • allowed de-duping scheduled ticks (5),

  • avoided need to add lots of Script Execution Order tweaks and simply handle in code,

  • easier to debug since can inspect pending work etc.


A single extra MonoBehaviour.Update() that usually looks at the first item on the list and then exits is good for performance (1). GC load is reasonable in initial implementation and I can swap to either structs or pooling to zero the small allocations it's doing later if needed.

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

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

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

Related Questions

Fire and forget coroutines 1 Answer

What's the most efficient way to time events? 2 Answers

Coroutine impact on frame rate 0 Answers

AI Coroutines not behaving properly 1 Answer

using coroutines for move objects - performance. 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