• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
  • entries
    14
  • comments
    28
  • views
    25453

Generic object translator

Sign in to follow this  
Followers 0
XXChester

1547 views

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.
0
Sign in to follow this  
Followers 0


3 Comments


...
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]
0

Share this comment


Link to comment
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).
0

Share this comment


Link to comment
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.
0

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

Important Information

By using GameDev.net, you agree to our Terms of Use and Guidelines.