- Home /
Update() VS delegate events
What is more performance efficient? Checking for something on Update() (or FixedUpdate() or LateUpdate() depending on the situation) or writing my own Events with Delegates for check?
Way too unspecific. What is "something"? The only answer we could give is: "It depends". Custom events have to be raised somewhere and somehow. If the actual polling for something just happens elsewhere it just adds more complexity.
$$anonymous$$ost larger games use their own message / event system, but that's specifically designed / adapted for their use case. Also the main use for an event / message system is not to increase performance but usually to decouple classes and to abstract the idea of an event.
Answer by WinterboltGames · Jun 08, 2017 at 01:34 PM
delegates don't affect performance, they are means of controlling how/when functions are called, like this:
private delegate void Delegate();
Delegate RefreshInventory:
bool finish;
private void Start ()
{
RefreshInventory += UpdateIcons;
RefreshInventory += ConsumeItems;
RefreshInventory += UpdateStacks;
RefreshInventory += CraftItems;
}
private void Update ()
{
if (RefreshInventory != null)
{
RefreshInventory
}
if (finish)
{
RefreshInventory -= UpdateStacks;
}
}
private UpdateStacks ()
{
finish = true;
}
but it doesn't seem to improve performance anyway (this might not be true tho).
and you say which is best from LateUpdate (), Update() and FixedUpdate ()
well, basically
The update function is used to detect input and other stuff that needs to be done every frame. more info >>>> https://docs.unity3d.com/ScriptReference/MonoBehaviour.Update.html
the fixed update function is used to move physical objects like rigid bodies and to do more physics based stuff like moving a character controller. more info >>>> https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html
the late update function is called after all other update functions. you can use it, for example, to make a camera follow an object. more info >>>> https://docs.unity3d.com/ScriptReference/MonoBehaviour.LateUpdate.html
also here's a link to a tutorial about delegates and how to use them >>> https://unity3d.com/learn/tutorials/topics/scripting/delegates
hope this helps you!
Answer by Stresscavity · Jul 07, 2017 at 02:18 AM
Delegates CAN influence performance, but it depends on what you're using them for, and are often used for their other benefits: ease of implementation, code cleanliness, etc. This is a big part of scalability as well, as adding a batch call to certain events (onClick, onDelete, etc.) make the system far more robust than polling 100 global variables that random objects can alter.
If it's something you need to check every frame, than you won't get anything out of a delegate, because it'll need to be called constantly anyways. BUT, if the alternative is calling an entire function (not just a check) every frame, when it only needs to be called on an object's change, then it is much more efficient to use a delegate.
I.E. if you use Update(), most people would probably do this:
void Update() {
globalObject.updatePos(newX, newY);
}
Which is fine, but what happens when behavior gets more complicated? What if updating the position is expensive? What if in the future you add more complexity to updatePos(), making it more expensive to run? What if it's position only needs to be updated when newX or newY changes? What if there are other functions to be called on Update()? All these problems usually get fixed with the typical horrendous stack of polling variables:
Update() {
if (redraw) {
if (newX != lastX || newY != lastY) {
globalObject.updatePos(newX, newY);
globalObject.redraw();
globalObject.markDifference(newX, new Y);
lastX = newX;
lastY = newY;
} else {
globalObject.markSkipped();
}
}
}
This is an exaggerated example, but I'm sure everyone has seen code or are guilty of writing things like this themselves. More often then not, the nested statements grow ridiculously in size, and make debugging and scaling a pain. That's where delegates, Actions, etc. become a godsent, because anytime you want to add complexity to something, you can simply determine what Action the new behavior is part of, and then add it.
Your answer
Follow this Question
Related Questions
Using Delegates for separating UI and Logic, are these in the right place? 1 Answer
Subscribers of Delegates? 2 Answers
Is "delegates" in "Update", efficient or worst 2 Answers
Unity Events - Subscribe and Unsubscribe Odd Behavor 0 Answers
MonoBehaviour Update() performance vs handling updates in a single UpdateManager script 0 Answers