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
2
Question by MCHammond · Dec 16, 2012 at 11:35 PM · fpsoptimizationloadingturn

Run a complex script in the background without reducing the FPS

The Problem:

I am making a turn based game and at the end of a players turn a complex script is run before it is the players turn again.

The problem is that when this complex script is run the game freezes for about 5 to 8 frames, this is only going to get worse as I add more complexity to the game.

As it is a turn based game it will not technically be a problem for the game to freeze for a few seconds between turns but it does look bad, especially since all the characters idle animations freeze and the game looks crashed.

The Question:

How can I get this complex script to run in the background "over a few frames" once a players turn is over, while still maintaining a frame rate that allows animations to play?

I will obviously not permit inputs during this period and nothing will change "except whats happening in the complex script", other than the idle animations playing.

Bad Solutions:

I could display a "Loading" screen before running the script and then remove it when the script has completed but that will look just as bad and kill immersion.

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 Statement · Dec 16, 2012 at 11:58 PM

You could make use of coroutines. You would have to modify your code to allow the game to "catch up & do other stuff" while your operation is processing.

http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.StartCoroutine.html http://docs.unity3d.com/Documentation/ScriptReference/Coroutine.html

You could make use of threads to run your code on a different core of your processor (or switch between tasks on single core architectures).

http://msdn.microsoft.com/en-us/library/a9fyxz7d(v=vs.90).aspx

Take care if you do threads as you can't access most Unity classes from a different thread than the main thread.

Also you could consider trying to optimize your code.

Comment
Add comment · Show 7 · 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 MCHammond · Dec 17, 2012 at 12:29 AM 0
Share

Thank you.

Coroutines sound like the thing I need to use, although it will require me write my code in small snipits that the CPU can handle.

I was wondering would it be possible to implement a system were a script would pause till the next update if a certain amount of time has passed "ie one 1/30 of a second"?

avatar image save · Dec 17, 2012 at 12:37 AM 0
Share

Using yield inside for- and while loops usually takes care of any hiccups. What heavy algorithm(s) are you running during the freeze?

avatar image Statement · Dec 17, 2012 at 12:47 AM 0
Share

I was wondering would it be possible to implement a system were a script would pause till the next update if a certain amount of time has passed "ie one 1/30 of a second"?

Yes, but you'd have to make sure that the code is testing for the elapsed time frequently so it can yield. Otherwise it sounds like you want to use threads. Depending on how much stuff your script does with "Unity objects", you might want to think about threading ins$$anonymous$$d. If you don't use Unity objects in those algorithms, then you should be pretty fine, and all you need to worry about is the synchronization to signal the main thread when the work is done.

avatar image MCHammond · Dec 17, 2012 at 12:53 AM 0
Share

The game is on a Hex grid with 10,000+ hex's that can move and have walls placed between them.

The Complex Script finds all Hex's adjacent to each Hex and then checks to see if there is a wall blocking movement to that Hex. Then a list of Connections is created for each Hex.

I have a $$anonymous$$i version that just updates the Connections list for Hex's that have changed "and it neighbors" for when a wall gets destroyed during a turn.

But I want to totally rebuild the Hex Grid each turn, for peace of $$anonymous$$d. :P

avatar image save · Dec 17, 2012 at 01:08 AM 0
Share

That does sound very intense, but also interesting! Re-assigning so many objects/components might be tricky to solve without some sort of pause in between. At the end you might want that $$anonymous$$i version for the sake of the end-user, but until then you might be able to loop through hex in blocks (batches) while giving the player a heads up to wait. Animations should be able to play whilst doing so.

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

11 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

Related Questions

How to fix this script? 1 Answer

Why CPU waits for GPU when there is not much work on GPU? 0 Answers

How to optimize load time when pressing the play button in the editor. 1 Answer

Gravely need some optimization tips 3 Answers

Loading many gameObjects 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