Hey everyone - I was wondering which of these methods would be more efficient in terms of performance - both are easy to implement and easy to read.
Method 1 :
private float timer = 0f;
private float coolDown = 10f;
void Start(){
timer = coolDown;
}
void Update() {
if(timer <= coolDown)
timer -= time.deltaTime;
if(timer < 0f )
timer = 0f;
CallSomeFunction();
}
private void CallSomeFunction(){
//some code;
timer = coolDown;
}
this how I did it for quite a while .
Recently I stumbled upon something like this :
Method 2 :
private float timer = 0f;
private float coolDown = 10f;
Update(){
if(timer + coolDown > Time.fixedTime){
// CallSomeFunction;
}
}
private void CallSomeFunction(){
// function stuff
timer = Time.fixedTime;
}
I know - in the end both methods (should - if I didn’t make some stupid errors) work and they both should not make significant efficiency differences , BUT method 2 would create some really big numbers if I would run the game for a couple of days without a break -wouldn’t it ? On the other hand, I would save on those stupid conditions for the timer.
btw. I know how to use Mathf.Clamp(timer, 0, coolDown); I just don’t like it. ( however if clamping is very good prog. practice please let me know - i am pretty new and want to learn properly!)
Thank you for your help and opinions.
Cheers
Daniel