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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help Improve/Simplify PowerUps Class

Discussion in 'Scripting' started by Mitch_AWeekAgo, Mar 30, 2015.

  1. Mitch_AWeekAgo

    Mitch_AWeekAgo

    Joined:
    Oct 19, 2014
    Posts:
    22
    Hi!

    I've been trying to make to make a simple buff class for item pickup for a while and I'm having trouble solving this simple misunderstanding.

    My goal is to have buff items randomly generated by a buffManager class.
    The class has a dictionary where the key is the name of the buff and the float value is the actual buff.

    Now, my problem is how do I send back the new value of the buff/bonus knowing that it comes from a different class.

    Here is my code. The buff doesn't take effect on the player or gun.
    Code (CSharp):
    1.  
    2.  
    3. /// <summary>
    4.     /// Chooses the bonus.
    5.     /// Randomly choose a power bonus in the dictionary
    6.     /// </summary>
    7.     /// <returns>The bonus.</returns>
    8. public float chooseBonus()
    9.     {
    10.         int index = 0;
    11.         string key = " ";
    12.         string chosenKey = " ";
    13.         string[] keys = bonusListName.ToArray ();
    14.  
    15.         bonusIncreaseList.Keys.CopyTo(keys, 0);
    16.  
    17.         for (int i = 0; i < keys.Length; i++)
    18.         {
    19.              index = Random.Range (0, keys.Length);
    20.         }
    21.  
    22.         key = keys [index];
    23.      
    24.         bonusIncreaseList[key] = 9000;
    25.      
    26.         Debug.Log("BonusName: " + key  + " // BonusValue: " + bonusIncreaseList[key]);  
    27.          
    28.         return bonusIncreaseList[key];
    29.  
    30.     }
    This is how I populate the dictionary. Kinda primitive but before that I kept getting a exception saying that it could not hold the requested elements which were <string, float> so that's why I'm passing string list/float list
    Code (CSharp):
    1.  
    2. //TODO: This MUST be simplify later
    3.     /// <summary>
    4.     /// Fills the bonus list.
    5.     /// filling the bonusPower list, the bonusName and the dictionary
    6.     /// with bonusname as keys and bonuspower as value
    7.     /// </summary>
    8. public void FillBonusList()
    9. {
    10.         //From gunManager
    11.         bonusListPower.Add (gunManager.bonusDmg );
    12.         bonusListName.Add ("+bonusDmg");
    13.  
    14.         bonusListPower.Add (gunManager.fireRateBonus );
    15.         bonusListName.Add ("+fireRateBonus");
    16.  
    17.         bonusListPower.Add (gunManager.moreBulletsBonus );
    18.         bonusListName.Add ("+moreBulletsBonus");
    19.  
    20.         bonusListPower.Add (gunManager.chargeShotXtraDmgBonus );
    21.         bonusListName.Add ("+chargeShotXtraDmgBonus");
    22.  
    23.  
    24.         //From playerControl
    25.         bonusListPower.Add (playerControl.runSpeed );
    26.         bonusListName.Add ("+bonusSpeed");
    27.  
    28.         bonusListPower.Add (playerControl.acceleration );
    29.         bonusListName.Add ("+bonusSpeedAccelerator");
    30.  
    31.         bonusListPower.Add (playerControl.bonusHealOvertime );
    32.         bonusListName.Add ("+bonusHealOvertime");
    33.  
    34.         bonusListPower.Add (playerControl.bonusAcceleratorCD);
    35.         bonusListName.Add ("+bonusAcceleratorCD");
    36.  
    37.         bonusListPower.Add (playerControl.bonusMaxHeal );
    38.         bonusListName.Add ("+bonusMaxHeal");
    39.  
    40.         //Adding to dictionary
    41.         bonusIncreaseList.Add (bonusListName [0], bonusListPower [0]);
    42.         bonusIncreaseList.Add (bonusListName [1], bonusListPower [1]);
    43.         bonusIncreaseList.Add (bonusListName [2], bonusListPower [2]);
    44.         bonusIncreaseList.Add (bonusListName [3], bonusListPower [3]);
    45.         bonusIncreaseList.Add (bonusListName [4], bonusListPower [4]);
    46.         bonusIncreaseList.Add (bonusListName [5], bonusListPower [5]);
    47.         bonusIncreaseList.Add (bonusListName [6], bonusListPower [6]);
    48.         bonusIncreaseList.Add (bonusListName [7], bonusListPower [7]);
    49.         bonusIncreaseList.Add (bonusListName [8], bonusListPower [8]);
    50. }
    51.  
     
    Last edited: Mar 30, 2015
  2. Mitch_AWeekAgo

    Mitch_AWeekAgo

    Joined:
    Oct 19, 2014
    Posts:
    22
    I can compare the variables in the dictionary against the corresponding variable in the player class or gun class.

    If they match then add 9000 to it, like this:
    if(bonusIncreaseList[key] == playerControl.runSpeed) then playerControler.runSpeed += 9000;

    It does work if I do this but I think that this class can be implemented in a simpler way.
    Feedbacks are more than welcome. I want to know how you guys would do this.
     
  3. Mitch_AWeekAgo

    Mitch_AWeekAgo

    Joined:
    Oct 19, 2014
    Posts:
    22
    I really lack in structuring code and it is something that I want to improve. I just need someone to give me the theory/pointers for this.
     
  4. Random_Civilian

    Random_Civilian

    Joined:
    Nov 5, 2014
    Posts:
    55
    Immediate advice I would give would be to ditch the string for keys and use an enum or some const strings instead.

    My method is like the command pattern.
    I represent all quantitative things as nodes contained in a holder attached to the owner. Command objects are passed the holder and manipulate the desired nodes when executed.

    It eliminates the need to return values as the nodes/stats are being manipulated directly.

    But then again, I do not know much about your implementation and I might be over complicating mine :p
     
  5. Mitch_AWeekAgo

    Mitch_AWeekAgo

    Joined:
    Oct 19, 2014
    Posts:
    22
    Thanks for replying. I kinda see what you mean. I'm definitely someone who over complicate things a lot, this thread is the proof. I'm going to try using enum as you said.