Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Too Many if's()?: best way to gather percentage and pass it

Discussion in 'Scripting' started by renman3000, Jun 4, 2013.

  1. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Hi there,
    I have a healthBar in my 2D game. My original plan was to have the bar's health points, see pic, made up, each by a sprite. There are 25 sprites that make up the meter. My thought was if, 4 pts damage given, remove one bar.

    My question is this, It will require to continually calculate the percentage of damage done, then ask, if damageDone == this.... a bunch of times over. So My question is, is there a much easier way to gather this information than as I am doing it?


    Code (csharp):
    1.  
    2. void damgeTaken(){
    3. damageTakenValue -= damageGiven;
    4. percentOfDamageTaken = (damageTakenValue /damageTakenLimit) *100;
    5. if(percentOfDamageTaken < 100  percentOfDamageTaken >= 96 )
    6. rendererOfHealthBarMeterNode1.renderer.enabled = false;
    7. else if(percentOfDamageTaken < 96  percentOfDamageTaken >= 92 )
    8. rendererOfHealthBarMeterNode2.renderer.enabled = false;
    9.  
    10. ///...AND SO ON to 0.
    11.  
    12. }

    Is there a much easier way to get this percentage info and determine the values relevance??
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
  3. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    So, I am building for mobile.
    I suppose I should have ran tests first but it is the code regardless, of creating 25 comparisons. if(x y). I was just hopeful something could ease it.

    No worries.


    @appels, fir my case, no pun intended, this might be much better as it will save me using and, sifting thru each if, until it finds the correct answer.

    Much appreciated.
    Thanks.
     
  5. NTDC-DEV

    NTDC-DEV

    Joined:
    Jul 22, 2010
    Posts:
    593
    1) Put all your rendererOfHealthBarMeterNode(s) in an array.
    2) You appear to have 20 of those... 1 for every 5%; transform your percentage into a x/20 value; rounded to an int
    3) for each [...] rendererOfHealthBarMeterNode.renderer.enabled = false;
    4) rendererOfHealthBarMeterNode[(x/20)-1]renderer.enabled = true;
    5) profit
     
  6. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    656
    I would use a more dynamic approach, surely. Something like this:

    Code (csharp):
    1. Renderer[] healthbarNodes;
    2.  
    3.     void damgeTaken()
    4.     {
    5.  
    6.  
    7.  
    8.         damageTakenValue -= damageGiven;
    9.  
    10.         damageTakenRatio = damageTakenValue / damageTakenLimit;
    11.  
    12.         for (int i = 0; i < healthbarNodes.Length; i++)
    13.         {
    14.  
    15.             if ((float)i / (healthbarNodes.Length - 1) > damageTakenRatio)
    16.             {
    17.  
    18.                 healthbarNodes[i].enabled = false;
    19.             }
    20.             else
    21.             {
    22.                 healthbarNodes[i].enabled = true;
    23.             }
    24.  
    25.         }
    26.  
    27.  
    28.  
    29.     }
    This is not tested code, and maybe it should be ">=", but it is just to give you an idea about how to approach this without having to write every single "if". Get back to me if you have any troubles :)
     
  7. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    I think Kragh may have won the sweepstakes. Very good way to go about this. Cheers! Ps, is Kragh Kilngon?
     
  8. Kragh

    Kragh

    Joined:
    Jan 22, 2008
    Posts:
    656
    No, Kragh is not Klingon, sadly ;) Or maybe it is, and I just don't know about my past... Anyways, glad you could use the approach, and hope this will help you generally in your approach to other problems.
     
  9. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Thanks.
    Been watching a lot of Star Trek the Next Generation lately. Ryker is the worst. Google "Ryker Sits Down" for a laugh.
     
  10. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    Use a for loop.

    Code (csharp):
    1.  
    2. int spritesToDraw = CalculateNumberOfSprites(); // Figure out how many you should draw. Probably integer division will do it.
    3. int xPos = 0;
    4. for (int spritesDrawn = 0; spritesDrawn < spritesToDraw; i++) {
    5. // Draw a sprite at xPos
    6.  
    7. // Move xPos over a bit
    8. }
    9.  
    In the vast majority of cases if your design necessitates a long stack of if statements you're making life very difficult for yourself.

    Edit: If this is for mobile and you're using OnGUI this approach could be pretty intense performance wise. Not sure about Unity 4, but in Unity 3 each OnGUI draw is a new draw call, which makes it a pretty expensive way to draw a health bar.

    Edit edit: Missed Kragh's post, oops!
     
    Last edited: Jun 18, 2013
  11. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Don't be hatin' on Jonathan Frakes...
     
  12. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Cant help it. He makes it so easy. Tho yesterday he was good in the Lt Data's brother episode.
    http://www.youtube.com/watch?v=lVIGhYMwRgs







    On a lighter note, I have a question. I am using this code to adjust the wpnHudMeter when a new wpn is selected. However, it always takes the firsrt node off even if the energy level is full and I am unsure why. Any reasons you can see?
    Code (csharp):
    1.  
    2.     public void adjustHudToNewWpn(){
    3.         wpnEnergyUsedRatio = wpnEnergyUsedTotal/wpnEnergyLimit;
    4.         for(int i = 0; i < wpnEnergyNodesLength; i++){
    5.             if((float)i / (wpnEnergyNodesLength - 1) > wpnEnergyUsedRatio){
    6.                 wpnEnergyNodes[i].enabled = true;
    7.             }
    8.             else{
    9.                 wpnEnergyNodes[i].enabled = false;
    10.                 print("a wpnEnergyNodes[i] " + wpnEnergyNodes[i]);
    11.             }
    12.         }                  
    13.     }
    14.  
     
  13. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    but you can use a simple div/mod system (aka % and /) to call a method that takes a parameter of type int or float or whatever... I notice you use intervals of 4, so a simple Array of RendererOfHealthBarMetterNodes (phew that's a mouthfull) can be activated based on that quotient (not sure that's the right translation).
     
  14. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,681
    Thanks,
    This is working for me. My issue is figuring out why, when I switch from one wpn to another, the old weapon is loosing its energyUsed value.
     
    Last edited: Jun 18, 2013