Brandon McCulligh's Portfolio
Generic Object Translator
Posted Date: 8/29/2011
Featured At: GameDev.net
Original Post
      This is the long overdue followup article to my XNA Scripting 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;
[code]
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<object> parms = new List<object>();
            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;
}
[/code]

      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;

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

The ship would move from here;


To here;


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.


Source;
      ObjectTranslator.cs

      As always, thank you for reading my journal.
Top

v2.0.0.2
Powered by: Great White North Productions© Technologies