- Home /
problems with setters and getters.
hey, so i am trying to follow this tutorial to get a better understanding of the workings of an RPG
tutorial https://www.youtube.com/watch?v=li6ha2d8Arw&list=PLE5C2870574BF4B06
the only problem is that i followed the tutorial exactly(except for a few errors on his part) and i am getting some really weird errors, not sure if it is because of the difference in versions or what but i could use a little help.
here is the code that i wrote.
public class BaseStat
{
private int _baseValue; //the base value of the stat.
private int _buffValue; //the amount of the buff to this stat.
private int _expToLevel; //total amount of exp to reach next level.
private float _levelModifier; //the modifier applied to the exp to raise the skill.
public int baseStat()
{
_baseValue = 0;
_buffValue = 0;
_levelModifier = 1.1f;
_expToLevel = 100;
}
//base setters and getters.
public int BaseValue()
{
get{ return _baseValue; }
set{ _baseValue = value }
}
public int BuffValue()
{
get{return _buffValue;}
set{_buffValue = value;}
}
public int ExpToLevel()
{
get{return _expToLevel;}
set{_expToLevel = value;}
}
public float LevelModifier ()
{
get{return _levelModifier;}
set{_levelModifier = value;}
}
private int CalulateExpToLevel()
{
return (int)(_expToLevel * _levelModifier);
}
private void LevelUp()
{
_expToLevel = CalulateExpToLevel;
_baseValue++;
}
public int AdjustedValue()
{
return _baseValue + _buffValue;
}
}
You seem to be writing properties like they're functions.
Don't bother with custom properties unless you actually need them. Auto implemented properties work just as well. For your implementation as written you might as well just use public variables with no properties.
Properties only make sense when you need to control the output or input. Like
// Other objects can see this but cannot assign a value
public int life {get; private set;}
// This set up ensures input values are above zero
public int life {
get {
return life;
}
set {
if (value > 0 ) {
life = value;
} else {
life = 0;
}
}
}
Answer by MakeCodeNow · May 15, 2014 at 11:12 PM
Properties don't have () after the name. It's just
public int BaseValue
{
//...
}
this fixed it and i have been able to fix all but one error. i am getting error CS0161: 'BaseStat.baseStat()': not all code paths return a value. not quite sure what that means any ideas?
First off, c# error codes are incredibly Google-able. Try it sometime.
This error means you created a function that returns something (an int) but you don't return anything in your function.
i did actually google it but didn't understand the answer it gave, but i ended up figuring it out anyways.