As a new programmer I was curious if there was a nicer/better/more efficient e.t.c. way to achieve this same end?
public class Attributes_Base : MonoBehaviour {
[SerializeField]
private Dictionary<RPG.Attribute, int> attributes = new Dictionary<RPG.Attribute, int>();
protected virtual void Start() {
attributes.Add(RPG.Attribute.Constitution, 10);
attributes.Add(RPG.Attribute.Strength, 10);
attributes.Add(RPG.Attribute.Endurance, 10);
attributes.Add(RPG.Attribute.Dexterity, 10);
attributes.Add(RPG.Attribute.Agility, 10);
attributes.Add(RPG.Attribute.Knowledge, 10);
attributes.Add(RPG.Attribute.Charisma, 10);
attributes.Add(RPG.Attribute.Luck, 10);
}
public void SetAttributeValue(RPG.Attribute attribute, int value) {
attributes[attribute] = value;
}
public void AdjustAttributeValue(RPG.Attribute attribute, int value) {
attributes[attribute] += value;
}
public int GetAttributeValue(RPG.Attribute attribue) {
return attributes[attribue];
}
}
You can make your code marginally less verbose by initialising and populating the Dictionary at the same time, using this syntax:
private Dictionary<RPG.Attribute, int> attributes;
protected virtual void Start() {
attributes = new Dictionary<RPG.Attribute, int>() {
{RPG.Attribute.Constitution, 10},
{RPG.Attribute.Strength, 10},
{RPG.Attribute.Endurance, 10},
{RPG.Attribute.Dexterity, 10},
{RPG.Attribute.Agility, 10},
{RPG.Attribute.Knowledge, 10},
{RPG.Attribute.Charisma, 10},
{RPG.Attribute.Luck, 10},
};
im new with this engine,but i know c# … so use ALWAYS Gernerics, the non gernirc versions have alwasy lower performance…(packing and unpacking cost performance)
What u write looks like Java,becase there u have to write getter and setter funktions, IN C# are accesors implemnted,(thats the better option).Im a bit confuised that noone rely use this in unity.
so how does this look like ?
int AttributeValue {
set{
if (value < 0) return;
attributs[AttributValue] = value;
}
get {
return attributs[AttributValue];
}
}
for example it can look lie this…
Now u can acces thefield with object.AttributValue = NewValue for example
get & set accesors are like funktions,so u can implement more stuff then me