Search Unity

Continue problem, returning null

Discussion in 'Scripting' started by Flynn_Prime, Jun 23, 2018.

  1. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    In the following code, I am generating weapon stats and trying to check if a stat already exists. If it does, I want it to be ignored from the pool altogether. I'm trying to do this using the continue keyword, and it works in the sense that it stops duplicate stats, however GetMHStat is still sometimes returning null.

    Code (CSharp):
    1.  public WeaponStat GetMHStat(WeaponItem w)
    2.     {
    3.         int cumulativeTotal = 0;
    4.         int randomValue = Random.Range(0, totalWeightMH);
    5.  
    6.         for (int i = 0; i < mainhandStatPool.Count; i++)
    7.         {
    8.             if (WeaponStatAlreadyExists(w, mainhandStatPool[i]))
    9.             {
    10.                 randomValue = Random.Range(0, totalWeightMH - mainhandStatPool[i].weight);
    11.                 continue;
    12.             }
    13.             cumulativeTotal += mainhandStatPool[i].weight;
    14.             if (cumulativeTotal > randomValue)
    15.             {
    16.                 WeaponStat newStat = Object.Instantiate(mainhandStatPool[i]);
    17.                 RandomizeStat(newStat);
    18.  
    19.                 return newStat;
    20.             }
    21.         }
    22.         Debug.Log("Get Stat returned null!");
    23.         return null;
    24.     }
     
    Last edited: Jun 23, 2018
  2. santiagolopezpereyra

    santiagolopezpereyra

    Joined:
    Feb 21, 2018
    Posts:
    91
    In terms of logic, the only way for this script to return null is if the second if statement is never met as true or if the first if statement is always met as true.

    If the first if statement is always true it will continue cycling back to the beginning of the for loop until this finishes all its iterations and then will return null.

    If the second statement is never true, well... it will never return a newStat.

    I don't know the specifics of your game or this script, that's why all the help I can provide is about the logic. The question you should be asking is: is there a situation in which the first if statement will return true on every iteration, or the second if statement will return false on every iteration? If there is, that's when you are getting null.
     
    Last edited: Jun 23, 2018
  3. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Can you explain a bit more about your structure. So what is your expected result in the end and do you debug log what the ints are while returning null?
     
  4. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    Thank you for the replies. I'm not at my computer to post more code at the moment but I will later on today. Basically I have a LootManager class on each enemy that designates a Loot table with weights etc.

    When an item is dropped I have logic that determines the item type, and if it is an EquippableItem it will roll for item rarity. Rarity determines how many stats is allowed on the piece of equipment. Then for each stat slot I will generate a random stat with the code above until all slots are filled. That works correctly for now other than the problem described above with duplicate stats being added.

    Another issue I have only been able to solve by using different methods for generating a stat is the fact that Offhand weapons, mainhand weapons, Armor and Accessories all have their own stat pools (stats that are allowed on the weapon). This is not very effective as I have to repeat myself, use a different list for each stat pool, and various if/switch statements. Any help on how to approach this would also be appreciated.
     
  5. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Just to add, it will also return null if mainhandStatPool.Count is zero. :)
     
  6. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    Noted. The stat pools are loaded from my Resources folder. They never equal zero. I'm almost sure the problem I'm getting is due to the weights. Need to do some more debugging when I get the chance
     
  7. santiagolopezpereyra

    santiagolopezpereyra

    Joined:
    Feb 21, 2018
    Posts:
    91
    Doug_B's addition is accurate, I missed that one. :)

    You could solve this -if I correctly understood the architecture of your code and my assumptions are correct- through inheritance. I don't know how the scripts of your items are setted, but you could have a father class defining the common elements every item shares, and then specific classes, childs of the previously mentioned class, that defines the unique elements of each type of item and, among those elements, the stats pools they are to have.

    This way you wouldn't have to handle several lists from a single class, but locate each pool on each type of item; therefore, you'd only need to know the type of the item you are dealing with to access its pool, which would be just some sort of variable inside of that type's class. I don't know if I made myself clear, I hope I did. English is not my native language.
     
  8. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    Hmm I don't know why I didn't think of that! I already use inheritance for my items too (which are scriptable objects). Your suggestion also means different items of the same type can actually have different stat pools too. I'll definitely be trying that
     
  9. santiagolopezpereyra

    santiagolopezpereyra

    Joined:
    Feb 21, 2018
    Posts:
    91
    Glad I could be helpful.
     
  10. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    387
    @santiagolopezpereyra You Sir, are a star. Putting my Stat Pools on the item itself not only solved the issue of duplicate code, but also the initial coding issue. Now I can just remove a stat from the pool once it is added, as it will no longer be needed (and vice versa if I implement item modding). Thanks a billion!
     
  11. santiagolopezpereyra

    santiagolopezpereyra

    Joined:
    Feb 21, 2018
    Posts:
    91
    Once again, glad I could be helpful! :)

    The fact that this also solved the initial problem shows why clean and well organized code is so important, preventing bugs and unwanted behaviour. Keep on coding!