- Home /
How to find a struct variable with a string
Hi there,
I have a string wich is given a value by another script.(this name is equal to 1 of the struct variable names) and I have a struct wich stores 3 variables called: strenght, constitution and agility(these are all an int)
What I want is next: I want to pick 1 of the struct variables with my string so that I can change the value of 1 of the struct variables. I have tried dictionary but I dont understand (a rabbit turd)/anything of it.
I appreciate any help :)
Answer by whydoidoit · Mar 12, 2014 at 01:49 PM
If it's long lived it shouldn't be a struct in the first place, it should be a class. Classes are more performant and provide you with easier ways to set variables by reflection.
using System.Reflection;
public class ReflectableClass {
public object this[string key] {
get {
return this.GetType().GetField(key, BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic).GetValue(this);
}
set {
this.GetType().GetField(key,BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic).SetValue(this, value);
}
}
}
Then your class looks like this:
public class Attributes : ReflectableClass {
public int strength;
public int agility;
}
And you can access it like this:
var instance = new Attributes();
instance.strength = 10; //Both update strength
instance["strength"] = 20;
If other scripts need to access it you could also consider making an Attributes singleton.
Nice, thanks for your answer. I think I'm going to use this, but not for now because I have already found another solution on my problem.
Answer by goibon · Mar 10, 2014 at 09:15 PM
Dictionaries aren't so hard to use. The way they work is you determine which datatype you will like to use for the keys and which datatype you would like to use for the values.
You set it up like this:
// The dictionary to store the information in
public Dictionary<string, int> dictionary = new Dictionary<string, int>();
This dictionary uses string for keys and int for values. The data in the dictionary is as key:value pairs, where you only need to know the key to retrieve the value from the dictionary. Here is an example of storing three different key:value pairs and retrieving them.
// Use this for initialization
void Start () {
// Add values to the dictionary under different keys.
dictionary.Add("Strength", 100);
dictionary.Add("Constitution", 50);
dictionary.Add("Agility", 75);
// Retrieve values by their key.
int strengthValue = dictionary["Strength"];
print("StrengthValue: "+strengthValue);
print ("Constitution: "+dictionary["Constitution"]);
print("Agility: "+dictionary["Agility"]);
}
From the information given I'd say use a Dictionary ins$$anonymous$$d of a struct. But it really depends on whether you intend to extend the Struct to contain different types of values.
If they're all the same type, eg. ints, then I'd say just use a Dictionary ins$$anonymous$$d of a Struct.
Answer by nicolasjr · Mar 10, 2014 at 09:18 PM
This should do it:
int value = typeof(Struct).GetProperty("propertyName");
Can you also use this to get the value? Like: string example = typeof(Struct).GetProperty("propertyName");
@nicolasjr please do more homework before you post answers, make sure they work before you post :)
GetProperty
returns a PropertyInfo
, from which if you want to get its value, you have to call GetValue
and provide an an object to get the property's value from.
public class Test { public int intValue { get; set; } } // or struct for that matter
Test test = new Test {intValue = 10};
PropertyInfo pInfo = typeof(Test).GetProperty("intValue");
int intValue = (int)pInfo.GetGet$$anonymous$$ethod().Invoke(test, null);
print(intValue);
Ins$$anonymous$$d of GetGet$$anonymous$$ethod
method (which gets the 'getter' of the property) you could use the Get$$anonymous$$ethod
property:
intValue = (int)pInfo.Get$$anonymous$$ethod.Invoke(test, null);
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Returning a struct to another script 1 Answer