Construct variable name from Strings.

Greetings,

I want to call a variable using a String.

ie:

var skillCode : String = InfectedBurst;

skillShopManager.lvl+"skillCode";
//Should be read by the compiler as skillShopManager.lvlInfectedBurst

How to achieve such a result?

I just found this again in my mailbox. First you have to understand that .NET / Mono is a compiled language. Your comment “Should be read by the compiler as skillShopManager.lvlInfectedBurst” doesn’t make much sense. The compiler compiles the code at development time. All variables that you have declared only have a value when the game / program runs, but at this time the code is already compiled.

However like already mentioned .NET / Mono offers Reflection which is a RTTI(runtime type information) system which allows you to access fields / properties / methods of classes dynamically. Here’s an example how to read / write an int variable:

//C#
// this is an instance of the class which contains the variable we want to access.
MyScript script;  

// If you know the type of your class use this:
System.Type T = typeof(MyScript);

// If you don't know the type get the System.Type like this:
System.Type T = script.GetType();


// Get the FieldInfo descriptor for the variable via it's name as string
// here you can use a dynamic generated string
string VarName = "lvlInfectedBurst";
System.Reflection.FieldInfo FI = T.GetField(VarName);

// To read the current value use GetValue().
//Note: the function returns always an `object` which have to be casted into the right type
int tmp = (int)FI.GetValue(script);

// To write a value back, use SetValue():
FI.SetValue(script,tmp);

As also mentioned, Reflection is very slow. If you just do it a couple of times it doesn’t matter. Another big downside is that it can break through visibility limitations (e.g. access a private member) which is a hell to debug.

You should avoid reflection whereever you can. It’s nice as debugging tool, but for not for daily use.

// The same in UnityScript (Javascript)
// this is an instance of the class which contains the variable we want to access.
var script : MyScript;

// Not sure about this one, could also be `var T = MyScript`:
var T = typeof(MyScript);

// If you don't know the type get the System.Type like this:
var T = script.GetType();


// Get the FieldInfo descriptor for the variable via it's name as string
// here you can use a dynamic generated string
var VarName = "lvlInfectedBurst";
var FI = T.GetField(VarName);

// To read the current value use GetValue().
// In UnityScript the variable gets casted automatically if a type is given
var tmp : int = FI.GetValue(script);

// To write a value back, use SetValue():
FI.SetValue(script,tmp);

ps. Don’t nail me down on the correct syntax in UnityScript. I don’t use this language.

If you insist on not using a dictionary, you can still do it using reflection (same as in the other answer).

But beware that this will come with a rather high performance penalty and the resulting code will be harder to read / maintain.