- Home /
Finding struct values by string
Hi everyone,
Here is my question: I want to be able to change the skill I can train and I would like to do this by means of a struct.
Here is my struct:
public struct XPStatsList{ int _strenght; int _constitution;
public int strenght{
get{return _strenght;}
set{_strenght = value;}
}
public int constitution{
get{return _constitution;}
set{_constitution = value;}
}
}
then I receive the skill I want to train by string and I want to use this string to find the struct I want to change. I have tried it like this:
if(meleeSkillToTrain == "strenght"){
XPPlus("strenght", xpToGoStrenght, nextLvlStrenght); }
void XPPlus(XPStatsList skill, int xpToGo, int nextLvlXP){
if(skill.strenght > nextLvlXP){
xpToGo = Mathf.RoundToInt(xpToGo * 1.5f);
nextLvlXP += xpToGo;
}
}
Can someone please explain me a way how to do this? Because it doesnt work
Answer by MakeCodeNow · Mar 09, 2014 at 07:52 PM
Create a dictionary where the key is the name of the skill and the value is your struct. You'll probably want to make the struct a class, though, for easier modification while in the dictionary.
Could you please give an example of what you mean with the dictionary?
In the dictionary case, you'd have something like Dictionary ins$$anonymous$$d of your current XPStatsList.
If you just want to get a C# property from a string, use reflection.
Structs are value types while classes are reference types. The tradeoffs are nuanced, but if you want data that is "cheap" to access but copied on assignment, use a struct. If you want data that is "expensive" to access but shared on assignment, use a class.
Your answer