Managed Scripting Post 3
by Johnathon · 07/26/2009 (5:44 pm) · 0 comments
Someone asked me a question with my last blog post wanting to know why I had planned two different versions of Managed Scripting (.NET & XNA) and why they couldn't use my previous engine DotScript to work with their XNA/Torque X projects. In reply to that, Managed Scripting .NET will work with xna and torque script if users just compile standard C# source files. However Managed Scripting allows you to build scripts like lego's, creating methods and properties as seperate objects and inserting them into classes. For this reason there needs to be a custom version built for XNA so that the classes can inherit from GameComponent. The video demonstration shows a custom built class generator that uses Managed Scripting for piecing together classes and compiled using the Hybrid Compiler. The scripts are stored in a library that is serialized out to an xml file allowing for them to be re-loaded and edited. It then shows an XNA project loading the compiled script assembly and using it.
Developers can build custom generators or editors that can create their script files how ever they want. They can give more control to the 3rd party users or less control. Using the hybrid compiler and building a graphical script editor such as this, allows developers to control how 3rd party users create additional content.
And finally I thought I'd show you the code that I used to load the script assembly, scan it for GameComponents and add them to the XNA components list. Something along these lines would be done for Torque X Projects as well.
The following code was placed inside of the Game1.cs Initialize method
Developers can build custom generators or editors that can create their script files how ever they want. They can give more control to the 3rd party users or less control. Using the hybrid compiler and building a graphical script editor such as this, allows developers to control how 3rd party users create additional content.
Video #1
Shows a class being created that adjusts the games window title. In a later version of this example I moved the code from the scripts Update() method to the Initialize() method, since this wasnt something that needed to be updated every game loop.Video #2
This one shows a Frames Per Second component being added, and the frames per second being shown in the games window title. These are simple examples, and since my XNA experiance isnt that great and my time limited i wasnt able to put together something that was really solid.And finally I thought I'd show you the code that I used to load the script assembly, scan it for GameComponents and add them to the XNA components list. Something along these lines would be done for Torque X Projects as well.
The following code was placed inside of the Game1.cs Initialize method
// TODO: Add your initialization logic here
engine = new ManagedScripting.ScriptingEngine();
if (System.IO.File.Exists(@"C:UsersJohnathonDocumentsVisual Studio 2008ProjectsC#ManagedScriptingWindowsGame1binx86DebugScripts.dll"))
{
engine.LoadAssembly(@"C:UsersJohnathonDocumentsVisual Studio 2008ProjectsC#ManagedScriptingWindowsGame1binx86DebugScripts.dll");
Type[] types = engine.GetAssembly.GetTypes();
foreach (Type t in types)
{
if (t.BaseType.Name == "GameComponent")
{
engine.InstanceObject(t, new object[] { this });
ManagedScripting.ScriptObject obj = engine.GetObject(t.Name);
GameComponent gc = (GameComponent)obj.Instance;
this.Components.Add(gc);
}
}
}About the author
