Home > Back-end >  How to avoid repetition assigning lots of variables with similar names to lots of fields in many obj
How to avoid repetition assigning lots of variables with similar names to lots of fields in many obj

Time:08-05

I'm modding a game and I want to create a list of weapons with different statistics. What I have in mind is this: I would like to create a lot of variables for different stats and have them somewhere in one place in my code so I can easily modify them later if need be. E.g. I would do this:

ironSword_AttackSpeed = 1.5f;
ironSword_Damage = 15;
ironAxe_AttackSpeed = 1.2f;
ironAxe_Damage = 20;
etc.

Then, I will create lots of variables which will represent weapons (instances of the class SL_Weapons which contains fields such as AttackSpeed, Damage, etc.) and add them to a list. Those variables will be named: ironSword, ironAxe, etc. And finally I want to assign each of those weapons its corresponding stats. What I want to avoid is assigning each value 'individually' like this:

ironSword.AttackSpeed = ironSword_AttackSpeed;
ironAxe.AttackSpeed = ironAxe_AttackSpeed;
etc.

I would like to do sth like this instead:

foreach (SL_Weapon weapon in weapons)
        {
            ((SL_WeaponStats)weapon.StatsHolder).AttackSpeed = VARIABLENAME_AttackSpeed;
        }

Is something like that even possible? I mean the compiler would somehow have to know the name of each SL_Weapon variable in foreach loop (like ironSword, ironAxe, etc.) and then take this variable as a string and then assign to this weapon's AttackSpeed field another variable that is called the same _AttackSpeed. I want to change around 150 weapons in my mod and each of them has at least 5 fields to change so if I have to do it manually, there is at least 750 lines of code for those changes alone. And all those lines are repeatable in some sense, so I hope there is a way to do it more efficiently :)

CodePudding user response:

No such an approach is not feasible.

What you probably want to do instead is to serialize the different weapon classes into json or xml files and deserialize them when needed.

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to

CodePudding user response:

What you can do is make a dictionary. First you would make an Enum with each weapon type, like this:

public enum WeaponType
{
   IronSword,
   IronAxe
   //more weapons
}

Then make a dictionary for each stat:

Dictionary<WeaponType, float> attackSpeedDict = new Dictionary<WeaponType, float>();
Dictionary<WeaponType, int> damageDict = new Dictionary<WeaponType, int>();

So then the section where you assign the attack speed and damage would look like this:

attackSpeedDict.Add(WeaponType.IronSword, 1.5f);
damageDict.Add(WeaponType.IronSword, 15);

attackSpeedDict.Add(WeaponType.IronAxe, 1.2f);
damageDict.Add(WeaponType.IronAxe, 20);

And then you would finally assign the values to the class like below. Please note you would have to add a WeaponType field to your SL_Weapons:

foreach (SL_Weapon weapon in weapons)
{
    weapon.AttackSpeed = attackSpeedDict[weapon.WeaponType];
}

Additionally you would probably want to check if that key exists in the dictionary first in case you forgot to add it.

Hopefully that helps!

CodePudding user response:

  1. If you can save this data to a file, and load it from that file once when the addon loads, that's the best approach to take. A CSV file should do nicely, where the first column is the weapon name, and each column thereafter is a weapon stat (in any order you choose). Then you just iterate over the rows in the file and load them into an array.

  2. If you can't do that, learn to love Excel or any of its free competitors. Punch the data into a spreadsheet. In one of the last columns, write a formula to copy data from one instance of the weapon to another. Copy the result of that formula to your mod. If it's not right, tweak the formula until it is, and do it again. Ultimately, there's no reason to write this code by hand.

  • Related