Hey, i read somewhere that to use the update function to count time is better than using IEnumerators. What i care about is just the performance. So my question is what better for memory/GC/performance or whatever. Lets say i have a player that between attacks needs to rest. So i can do it with the IEnum by reducing the Counter or i can put it in update like that: Code (CSharp): void Update() { if(Counter > 0) { Counter -= Time.DeltaTime; } } So i can't choose how to program those things. On the one hand the update function happend all time and if i have alot of counters so i can decrease my performance, and on the other hand the IEnumerators take more memory(Not sure about that). So, someone can explain please?
Why? This is certainly micro-optimisations and what should be more important is the readability of the code to you.
1.I care about optimization only because i want to avoid those things when i start a new project. 2.So the asnwer is the IEnumerator worse than update counters?
It depends. I don't like IEnumerators, they generate more problems than they solve. What you are describing is most probably never going to be a bottleneck. If it ever does, you could refactor it.
My favorite functions are IEnumerators, help me alot and i don't find it troublemaker. And what i ment about performance is not a single moment that full of functions, i talk about the long term playing, about losing memory with all those commands.
If you're concerned about this level of optimization, I'd expect this might perform slightly faster since you would only be doing a float comparison every update instead of a float/integer conversion comparison (you're comparing the counter float to the integer 0, and I'm pretty sure the integer 0 has to be converted to float as part of the comparison) followed by a float subtraction operation. You're probably want to do some kind of benchmarking though if you're really concerned about this kind of stuff. I haven't benchmarked it, its just my guess this would be better (by a trivial amount). Code (csharp): private float counter = 0f; //specify timeInFuture as the number of seconds in the future you want to count down private void setCounter(float timeInFuture) { counter = Time.time + timeInFuture; } void Update() { if (counter > Time.time) { //Not done yet } else { //Done } } As @AcidArrow mentioned, I'm also in the camp of avoiding coroutines when they aren't necessary. I generally only use them when tracking something complex in Update over time would not be very clean. For example, I use them when I need to queue up a large number of messages to several servers when my server cluster starts up, to set their starting game state, but I need to regulate how fast I send them to not overwhelm the networking API. It is a lot cleaner to track each queue for each server I'm sending to separately in a coroutine than to create a single monster queue for all servers I'm sending to that I manage in Update, for all different types of messages I'd need to send. But for something like a simple counter, I'd just do it in Update unless you end up with some long spaghetti code Update method for whatever reason. YMMV
I love coroutines, they are great for mini workflows that exist inside your domain. I tend you use them when user input is mixed into the logic, it's a nice way to await asynchronous actions in a synchronous manner