I am currently making a simple console based RPG dungeon game, I am still fairly new to C#, I took a visual basic course in school, and I have played around with unity for about a month.
My problem is, that I am programming the first battle that the player will encounter. I have constructed a few weapons, and I want to be able to call the persons current weapon with a separate string, for example, My current weapon is the dagger, or the object wp1, I want my "weapon" to be attached to wp1 in some way, so that I can do something like,
Console.WriteLine("Damage: " + weapon.dmg); rather than hardcoding wp1.dmg, so that later in the game making process, when the player has the opportunity to purchase a better weapon, I can do it with variables, for example, the player now has the shortsword (wp2)
private string weapon = "wp2";
...
Console.WriteLine("Damage: " + weapon.dmg);
I have tried to simply put,
String weapon = wp1;
then call weapon.dmg, but this doesn't work because it thinks i'm trying to call weapon.dmg, and not wp1.dmg
//players base damage
int damage = 0;
//players strength attribute
int strength = 0;
//weapon constructor
class wp
{
public int dmg;
public int cost;
public string name;
public wp(int d, int c, string n)
{
dmg = d;
cost = c;
name = n;
}
}
//three weapons that are constructed
wp wp1 = new wp(1, 25, "dg");
wp wp2 = new wp(3, 100, "ss");
wp wp3 = new wp(5, 250, "ls");
//the current weapon string
public string weapon = "wp1";
void attack()
{
//calculates damage based off of the players damage, strength, and weapon
int newDamage = damage * strength + (weapon.dmg);
}
Expected result: the program should use the player's current weapon's damage value
Actual result, the program tries to find the dmg value of the weapon, but that is not possible because "weapon" is just a string, so it throws an error
string.. store it as thewptype.wp) object to the Attack method so the current weapon is used each timeclass Weaponinstead ofclass wp. Classes and methods should beCasedLikeThis. Never make a public field; use a public property instead. Learn good habits now so you don't have to break them later.wpshould beWeapon.dmgshould beDamage. Pretend you're reading someone else's code and see if you can reason about what things are based on their names. If you can't, you've chosen a bad name. (edit) Yeah, what Eric Lippert said.