I'm currently building a small-scope RPG and running into an issue with modifying variables. My goal is to alter my character's stats when I run a procedure to change their equipment. My code is as follows:
public class Player: MonoBehaviour {
//Base stat variables
public static int PAtk = 12;
public static int PDef = 6;
//Battle-exclusive stats. Will have equipment added onto them.
public static int Attack = PAtk;
public static int Defense = PDef;
//Equipment variables.
public string weapon;
public string armor;
private void Awake()
{
//Get equipment
EquipWeapon("Bronze Sword");
EquipArmor("Bronze Armor");
}
void EquipWeapon(string WeaponName)
{
weapon = WeaponName;
EquipmentManager.GetComponent<Equipment>().EquipStatIncrease(PAtk, Attack, PDef, Defense, WeaponName);
}
void EquipArmor(string ArmorName)
{
armor = ArmorName;
EquipmentManager.GetComponent<Equipment>().EquipStatIncrease(PAtk, Attack, PDef, Defense, ArmorName);
}
}
public class Equipment: MonoBehaviour {
//Create dictionary to give each weapon a key ID. This will be used for calling items in another array.
Dictionary<string, int> WeaponDictionary = new Dictionary<string, int>();
//Create weapon array for stats.
public int[,] WeaponStats;
// Start is called before the first frame update
void Awake()
{ //Declare weapon IDs
WeaponDictionary.Add("Bronze Sword", 0);
WeaponDictionary.Add("Bronze Armor", 1);
//Declare stat array
//Table: ID, Atk, Def
WeaponStats = new int[,]
{
{0,4,0},
{1,0,4}
};
}
public void EquipStatIncrease(int BaseAtk, int BattleAtk, int BaseDef, int BattleDef, string Equipment)
{
BattleAtk = BaseAtk + WeaponStats[WeaponDictionary[Equipment], 1];
Debug.Log(BattleAtk + "Battle Power");
BattleDef = BaseDef + WeaponStats[WeaponDictionary[Equipment], 2];
Debug.Log(BattleDef + "Battle Defense");
}
}
The general gist of it is once EquipWeapon
or EquipArmor
is ran, the equipment variables in the Player
class will be updated. Afterwards, another function in the Equipment class will be ran with variables from the Player
class passed over, in order to be modified by the WeaponStats
array.
The Debug logs in EquipStatIncrease
show me that I am getting the desired results. However, the Attack parameter that is being placed into the function is not being permanently modified. During the first execution of the EquipStatIncrease
method, I will get a result of 16 BattleAtk
and 6 BattleDef
, due to the weapon being equipped. However, once it's run a second time to equip the armor, I'm shown a result of 12 BattleAtk
and 10 BattleDef
, even though BattleAtk
should have stayed at 16. This leads me to believe that the variables I'm trying to transform are not being modified, and I'm not sure what to do about this. In the EquipStatIncrease
parameters, BattleAtk
gives the message Parameter 'BattleAtk' can be removed if it is not part of a shipped public API; its initial value is never used
However, I don't know what action to take to resolve this.
I'm aware of being able to use an int function instead of void, but unless I'm missing something, that only works if I'm trying to change one variable. If I want to have an equipment object that affects 2 different stats at once(ex. a weapon that increases BattleAtk
and BattleDef
), then I don't see how an int function would work.