Search Unity

CoRoutines vs Time.deltaTime timer...

Discussion in 'Scripting' started by why789, Jan 31, 2012.

?

CoRoutines or countdown variables?

  1. CoRoutines

    20 vote(s)
    58.8%
  2. Countdown variables

    14 vote(s)
    41.2%
  1. why789

    why789

    Joined:
    Sep 23, 2011
    Posts:
    184
    Hello Unity people!

    So I am working on an FPS and I'm programming in my recoil, reload set up... Basically the way it is set up now is I have 2 variables... the time it takes to recoil... and the timer.... what happens is when I want it to start the recoil I set the recoil timer to the amount of time it takes to recoil... than from there I subtract Time.deltaTime from it until it gets below 0 than I reset all the variables and allow the player to shoot again... works great however I want to know if using CoRoutines would be easier (human readable wise as real as processor wise)

    coding it in C#... here's an example of the current timer code:
    Code (csharp):
    1.  
    2. recoilTimer = recoilTime;
    3.  
    4.  
    5. RunDownTimers() {
    6. //RECOIL
    7.         if (recoilTimer > 0) recoilTimer -= Time.deltaTime;
    8.         else if (recoilTimer <= 0  recoilTimer != -1) {
    9.         canShoot = true;
    10.         shot = false;
    11.         recoilTimer = -1;
    12.         }
    13.         else if (recoilTimer == -1) {
    14.             if (shot) {
    15.             shot = false;
    16.             canShoot = true;
    17.             recoilTimer = -1;
    18.             }
    19.         }
    20. }
    21.  
    no problems whatsoever... same set up for reloading.

    so which would you guys use and why? CoRoutines or countdown variables?
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It does not matter in my opinion. If you format your code better, it becomes more readable too ;-).
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    A coroutine would be much simpler and easier to read.

    Code (csharp):
    1. var reloadTimer = 1.0;
    2.  
    3. function Start () {
    4.     while (true) {
    5.         while (!Input.GetButtonDown("Fire")) yield;
    6.         Fire();
    7.         yield WaitForSeconds (reloadTimer);
    8.     }
    9. }
    --Eric
     
  4. why789

    why789

    Joined:
    Sep 23, 2011
    Posts:
    184
    what about speed wise? which is quicker: variables or a coroutine.
     
  5. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    If you find an unacceptable overhead or slowdown somewhere after your initial implementation, go back and optimize and worry about it then. If you google "premature optimization", you'll find plenty of arguments, quotes and jokes.

    Another variant could be letting an animation dictate when you can shoot again. This could be achieved either via an animation even or simply playing the animation once, non-looping and then polling whether or not it is playing.

    On your first implementation, the right answer to your base question is always "whatever makes the most sense to your setup" - that includes readability and the structure of other interdependent systems. By the sound of things, though, you don't have much other structure, in which case readability would be your highest priority, which would suggest a co-routine as Eric describes it.

    In any case, your poll is too brief/invalid.
     
    MichelAlonso likes this.
  6. why789

    why789

    Joined:
    Sep 23, 2011
    Posts:
    184
    ok :d thank you!