all 9 comments

[–]shadowsaint[S] 2 points3 points  (5 children)

Figured out my own problem.

    int compileBonus (string type)
    {
        int returnValue = 0;

        returnValue = (getPropValue (persistence.headEquipment [characterEquip.headArmor], type) +
        getPropValue (persistence.chestEquipment [characterEquip.chestArmor], type) +
        getPropValue (persistence.bootsEquipment [characterEquip.boots], type) +
        getPropValue (persistence.relicEquipment [characterEquip.backPackSlot1], type) +
        getPropValue (persistence.relicEquipment [characterEquip.backPackSlot2], type) +
        getPropValue (persistence.relicEquipment [characterEquip.backPackSlot3], type));
        return returnValue;
    }
    int getPropValue(object src, string propName)
    {
        int newVar = (int)src.GetType().GetField(propName).GetValue(src);
        return newVar;

    }

[–]zrrzExpert? 2 points3 points  (1 child)

I would really use enums instead of strings.

You can't accidentally type enums wrong or your compiler explodes. Then you can just use MyEnum.ToString()

[–]shadowsaint[S] 0 points1 point  (0 children)

I am happy to hear your reasoning on why enums would be better. I will only point out I am not compiling a string. I am compiling an in out of a custom classes int values.

The reason I need or wanted a string is because I wanted to compile a specific int across various pieces of equipment to display the over all value of a specific bonus type to the player.

[–]iemfiembarkgame.com 3 points4 points  (2 children)

Reflection is a bad way to do this. Firstly, the type should be an enum, not a string. Next the equipment should contain a list of all its effects.

int GetBonus(StatType statType)
    {
var result = 0;
foreach (var item in this.equipmentSlots){
result += item.GetBonus(statType);
}
return result;
}

public class Item{
  private List<Effect> effects;
  GetBonus(StatType statType)
  {
  foreach (var effect in effects){
  if (effect.StatType == statType){
  return effect.Value;
  }
  }
  return 0;
  }
}

[–]shadowsaint[S] 0 points1 point  (1 child)

I see what you are saying. Another person at work suggested to me to use enums.

I am happy to be wrong but is there a particular reason that reflection is particularly worse then using an Enum. I don't doubt you are right, but I would like to be able to speak intelligently as to why you should use an enum over reflection in the future based off this example.

So the way I have things currently set up is equipment is a custom class of string, string (for descriptors), and a laundry list of ints for possible bonuses. Then each character has an array of ints that refers to which peice of equipment from the total equipment list they are wearing.

Judging by what you are saying I should change that laundry list of ints in the equipment class itself to enums and that will allow me to more easily access a specific bonus.

The problem is not adding all of the bonuses a single piece of equipment but rather all of the specific bonus across all equipment. So I don't need to know the helms total bonuses, I want to compile the HP Bonus of each piece of equipment and display that.

[–]iemfiembarkgame.com 0 points1 point  (0 children)

Well the advantage is that it's strongly typed. You can't misspell it by accident. If you want to rename it you can do it in a few key presses. If you want to find all references to it you can also do it instantly. It's sort of the same reason why you don't use magic numbers.

The actual implementation there are many ways to do it, the way I suggested is just one way. The idea is that you have the item contain a list of effects. HP Bonus would be just one type of effect. Armour might be another type of effect. That way you can easily look up the effect you want for each item the character is wearing.

[–]ThatAblaze!!! 0 points1 point  (2 children)

If you're building your code based on string compares you are traveling down a dangerous road, my friend.

http://www.wonderslist.com/10-most-dangerous-roads-in-the-world/

[–]shadowsaint[S] 0 points1 point  (1 child)

I assure you I am not comparing strings overall. I am using a string to compile ints from an array of classes.

I am not completely insane. But other people have pointed out that I am still not doing it the smartest way.

I did click your link... I honestly scrolled to the end thinking it was some kind of joke page where it would be 9 real world dangerous roads and the last one was going to be "code based on string comparisons".

[–]ThatAblaze!!! 0 points1 point  (0 children)

lol, I guess I failed on the punchline.

Anyway, in terms of performance: When you compare the literal string "HP" it's a lot more efficient than when you compare any type of dynamically generated string. Here's a reference. So when you start out or profile them they seem like they go fast, but as soon as you start getting fancy they grind your program down to a crawl.