I trying to create a "player" that has a set of skills and a set of specials. Each skill is associated with a certain special.
Then the player is given 7 specials and 13 skills. To make it more readable (in my oppinion) I use structs for specials, skills and players. This is also for prectice using structs..
Please have a look at my code, it is pretty straight forward.
private struct specials
{
public string name;
public int value;
public specials(string n, int v)
{
this.name = n;
this.value = v;
}
}
private struct skills
{
public string name;
public specials spec;
public int value;
public skills(string n, specials s, int v)
{
this.name = n;
this.spec = s;
this.value = v;
}
}
public struct player
{
public specials strength = new specials("STRENGTH", 0);
public specials perception = new specials("PERCEPTION", 0);
public specials endurance = new specials("ENDURANCE", 0);
public specials charisma = new specials("CHARISMA", 0);
public specials intelligence = new specials("INTELLIGENCE", 0);
public specials agility = new specials("AGILITIY", 0);
public specials luck = new specials("LUCK", 0);
//Complains about charisma, saying an object reference is required for
// the nonstatic field method or property
public skills barter = new skills("Barter", charisma, 0);
}
My problem you can see in the comment in the code. Now, I can't see why this should be a problem.
The player is given his own specials and own skills and the skill is in turn associated (I hope by reference) with a special.
public specials charisma = new specials("CHARISMA", 0);
This creates a new special-object named charisma, right? So why shouldn't this be able to pass to a new skill-object.
One last thing. For some reason I still don't understand but realy would like to know, this is solved by using static when declaring charisma but the I can't change it in my form-class by typing
player.charisma.value = 123;
Regards!
EDIT:
I just discovered something that I can't explain. Instead of writing (that produced an error)
public skills barter = new skills("Barter", charisma, 0);
I write: (player is now a class)
public skills barter = new skills();
Then in the players constructor I can set the barters special like this:
barter.spec = strength;
Why is this exepted? Is this bad practice? Well I wont use it anyway because if I change the players special it wont be updated in the barter because I learned that struct is passed by value not reference. But my question about why I cant initialize the structure with the struct still stands.