Hi there guys. I’m currently writing a very basic, high level language for some specific behaviours in Unity. I’m reading the commands from a text file and the user declare his variable in the form of:
name,type,value; e.g. xPosition,float,50
I’m breaking this up into seperate strings (for now), using c#, but I need to know how I can create a variable from this user text file.
Thanks
You can’t really create a new variable at runtime in the sense of:
float newFloat = 5;
However, what you can do is create a dictionary for each supported variable type - and add the newly created variables to the dictionary at runtime. Example:
public class ExampleClass {
Dictionary<string, float> _userFloats;
Dictionary<string, string> _userStrings;
void AddString(string id, string value){
}
void AddFloat(string id, float value){
}
float GetFloat(string id){
// Obviously you would want to do some sort of validation here to disable errors, and return a default value if the id is invalid so that you're whole app doesn't fail - log the error though so you know to fix the missing values
return _userFloats[id];
}
void Parse(string stringToParse){
// Split up the string as you currently are
string valueType = // get your type from your split string
string valueId = // get your id from your split string
string valueValue = // get your value from your split string - this is a stupid name for a variable though :smile:
switch(valueType){
case "string":
AddString(valueId, valueValue);
break;
case "float":
AddFloat(valueId, float.Parse(valueValue)); // Obviously you want to catch Parse errors
break;
default:
Debug.LogError("Attempted to create an unsupported value type of: " + valueType);
break;
}
}
}
Obviously that’s not a perfect way to handle it, but it should give you the general idea.
1 Like
Thanks Kyle
Seems like that is what I need. I will chew on this a bit, but i get the general idea.
Thanks again
Assuming you have the exact inverse problem, how would you do it?
You have the 3 strings, you create the composite string (the name matches precisely to a declared and loaded constant) and apply it in a Vector3.
It doesn’t work…but could it work?
I know, my question is unbelievebly late and not quite on topic but , perhaps, interesting
Are you asking how to convert X, Y, and Z coordinates stored in strings to a Vector3? Because that’s a solved problem.
It is unbelievably late, it is extremely off topic, and unless you’re still working through 101-level programming tutorials, it is utterly uninteresting.