- Home /
Efficiency of Game Loops
What is the difference between letting the objects run their own update functions during run time, versus, using a Game Manager to call their "update" functions during run time? Is there any efficiency cost of using a GameManager Class to run the game rather than just using the default component based system that Unity provides?
==========================================================================
Example of Component Based:
Script1:
function Update() { // Run Object 1's Update stuff; }
Script2:
function Update() { // Run Object 2's Update stuff; }
And so on...
==========================================================================
Example of Using a Game Manager:
public class GameManager : MonoBehaviour {
// Update Function for the Game Manager Class
void Update() { // Check for Input and Assign values to Player
// Update Player
// Update Environment
// Update so on...
}
}
===========================================================================
Feedback would be greatly appreciated... Thank you (:
Answer by Eric5h5 · Mar 25, 2011 at 02:28 AM
If you mean looping through all objects in a single Update function as opposed to having each object use its own Update function, then yes, there is overhead involved in an Update call that you can eliminate by consolidating them.
However, it's questionable whether you would call this "improved efficiency". It's likely your code will become more complicated, for one, which means more chance for bugs. It's possible you're not CPU-limited in the first place, or that the Update calls are contributing a small enough amount that "fixing" this won't actually improve the framerate at all. Also, it's possible that you're better off not using Update in the first place anyway, and would find coroutines or other methods like InvokeRepeating more effective.
Hey Thanks Eric,
I was just wondering whether consolidation of the objects in a single update function would cause a drop in efficiency, since Unity was built to be component based.
I don't have much CPU power to play around with since it's the iPhone.
Well for me i prefer having objects in my control and choosing which to update each frame, so i think i would prefer having an array of objects and have them have a scheduled update given by the Game$$anonymous$$anager.
Anyway, thanks a lot for the answer :D
Cheers,
Renqi
Answer by meat5000 · Aug 10, 2013 at 12:11 PM
A central game manager, in my opinion should only be used in specific circumstances.
Imagine a variable 'x' from a script 'Script'. If x is used in an update of Script :
Use -> x;
If you use a central manager to do this :
Retrieve x from 'Script'; Use -> x;
Which is essentially 'twice' the operation. If you do this for a large amount of stuff you would impact performance.
Use GetComponent over gameObject.find but minimise usage of both, and if possible restrict it to use in the start() functions.
Your answer
Follow this Question
Related Questions
Performance when having Update functions in many gameObjects ? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Number of scripts per Object. Optimization question. 1 Answer
a ton of sprites vs sprite-tiling on one object 0 Answers
Efficient mesh vertex count changes? 1 Answer