Generic object translator

posted in XXChester's Blog
Published August 29, 2011
Advertisement
This is the long overdue followup article to this journal entry about my generic object translator. This new object translator is extremely simple to use and add to if necessary. It replaces the hacked together object translator of said previous journal entry.

New object translator;

///
/// Translates a specific value to the specified data type
///
/// type to translate to
/// string version of the data
/// Object translated to
public static object translate(Type translationType, string value) {
object translatedObjects = null;
if (translationType == typeof(string)) {
// strings need to be converted to char arrays for some reason
translatedObjects = value;
} else {
// This works for most types, it will look through its constructors etc to find the best suited to create the object
List parms = new List();
ConstructorInfo[] constructorInfos = translationType.GetConstructors();
bool foundMethod;
string[] componenets = value.Split(',');
if (constructorInfos != null && constructorInfos.Length > 1) {
MethodInfo[] constructionMethods = null;
ParameterInfo[] constructorParamInfos = null;
object[] constructorsParamTypeValues = null;
foreach (ConstructorInfo constructorInfo in constructorInfos) {
if (constructorInfo != null) {
constructorParamInfos = constructorInfo.GetParameters();
if (constructorParamInfos != null && constructorInfo.GetParameters().Length == componenets.Length) {// does this constructor match the amount of data we have
constructorsParamTypeValues = new object[componenets.Length];
for (int j = 0; j < constructorParamInfos.Length; j++) {
constructionMethods = constructorParamInfos[j].ParameterType.GetMethods();
foundMethod = false;
foreach (MethodInfo method in constructionMethods) {
if (method.Name == "Parse") {
foreach (ParameterInfo paramInfo in method.GetParameters()) {
if (paramInfo.ParameterType == typeof(string)) {
constructorsParamTypeValues[j] =
method.Invoke(paramInfo.ParameterType, new object[] { ScriptUtils.scrubStringParameter(componenets[j], translationType) });
foundMethod = true;
break;
}
}
if (foundMethod) {
break;
}
}
}
}
translatedObjects = constructorInfo.Invoke(constructorsParamTypeValues);
break;
}
}
}
} else {
// no constructors present for this type so try to directly parse it
MethodInfo[] typesMethods = translationType.GetMethods();
foundMethod = false;
foreach (MethodInfo method in typesMethods) {
if (method.Name == "Parse") {
foreach (ParameterInfo paramInfo in method.GetParameters()) {
if (paramInfo.ParameterType == typeof(string)) {
translatedObjects =
method.Invoke(paramInfo.ParameterType, new object[] { ScriptUtils.scrubStringParameter(componenets[0], translationType) });
foundMethod = true;
break;
}
}
if (foundMethod) {
break;
}
}
}
}
}
return translatedObjects;
}


The way this object translator works is very simple. It looks up the constructors for the type you pass in and invokes them in turn to create the type. If the type does not have a constructor it will try to directly invoke the objects "Parse" method if one exists. If no object is created the method will return null in which case you are responsible for handling the null (I throw a custom exception on the caller which bubbles up to the users command prompt to inform them of the error).

Say you are placing objects in your game and you are trying to get them to fit perfectly. Well in the past you had to shut down the game, change the floar by 1, re-compile, replay and see that it is still not in the right spot. With this scripting integration (and generic object translator) you can change things on the fly and shut down once you got the values figured out and change them at that time. Personally when designing levels I have seen a drastic improvement in efficiency. Note that this scripting engine is not just for placement, I use it for figuring out sprite animation rates and all kinds of other details such as scales, rotations etc etc.

How to invoke this translator;
If we have 2 objects on our screen ship1 and ship2 (registered names in the scripting engine) and we wanted to move ship1 you would want to invoke the ships position property this;

// Scripting engine is case insensitive
ship1.Position = Vector2(10,400)


The ship would move from here;
***NOTE** I cannot upload images to my journal still so you have to visit the links sorry;
http://imageshack.us/photo/my-images/233/prechange.png/

To here;
http://imageshack.us/photo/my-images/850/postchange.png/

How does this command work?
The command tells the scripting engine to find an object named "ship1" in its registered objects list and find and invoke its Position property. The property invocation within the scripting engine in turn realizes we have an assignment operator in the command so we want the setter accessor of the property and not the getter. The setter property will look at its parameter types and pass Vector2 and "Vector2(10,400)" to the object translator. The object translator will look up the Vector2''s constructors and find one that matches and invoke it.


I hope this object translator helps you with your XNA development or even raw C# development. Again, I appologize for the long delay in this posting and I am still trying to figure out why I cannot post files in my journal to put a direct link up in my previous post.

As always, thank you for reading my journal.
Previous Entry Move on....rewind
0 likes 3 comments

Comments

Washu
...
Or just call
[b]Activator.CreateInstance Method (Type,Object[])[/b]

[url="http://msdn.microsoft.com/en-us/library/wcxyzt4d.aspx"]http://msdn.microsoft.com/en-us/library/wcxyzt4d.aspx[/url]
September 07, 2011 03:01 AM
Washu
You should really write a real parser, they're not that hard and it would take care of things like... string handling (embedded quotes with commas).
September 07, 2011 03:24 AM
XXChester
I was going to use the Activator.CreateInstance method but than you have to know namespaces etc for the objects (if I remember correctly). This is just an object translator, the actual parsing of the input is not included in this post so I am not sure what you are referring to with "real parser". I am using a huge chunk of regex which pulls apart the input string and figures out the parameter number etc.
September 07, 2011 01:02 PM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement
Advertisement