Showing posts with label xna. Show all posts
Showing posts with label xna. Show all posts

Embedding IronPython

I am trying my luck with embedded IronPython 2 using the DLR so I thought I would share some of the steps to make that happen.

Prereqs:

  • Microsoft.Scripting.dll
  • IronPython.dll
  • IronPython.Modules.dll

These all come from the IronPython2 beta 1 release--and yes you need all of them. Failing to add IronPython.Modules.dll as a reference results in strange errors when trying to get the scripting engine which can be confusing.

The interface to IronPython and presumably other DLR languages is through the Microsoft.Scripting.Hosting.ScriptEngine class. There seems to be about a billion ways to get the engine here is what worked for me:

ScriptEngine engine; engine = PythonEngine.CurrentEngine;

This gets/creates the ScriptEngine for IronPython. Next we create a Microsoft.Scripting.Hosting.ScriptScope which allows for variables to be bound on the embedder's end before the script is run. It also can be inspected after the script is run.

ScriptScope scope = engine.CreateScope();

Now all that is left is to load the script woooo. Doing this creates a Microsoft.Scripting.Hosting.ScriptSource object which can have fun things done to it.

ScriptSource source = engine.CreateScriptSourceFromFile("myScript.py"); source.Execute(scope);

This loads a script and then executes it using the scope that was created. The contents of the scope can be examined afterwards like I said which can be useful if you want to create factories that return an extended C#/CLR class that can then be used like normal.

TacticsGame game = scope.GetVariable("game");

This gets the global "game" form the script. In order for this to work right you need to add the assembly references to your script. This can be done in two ways. First you can do it the normal way from inside the script:

import clr clr.AddReference("MyAssembly.dll")

This works but its kind of annoying if MyAssembly.dll is actually the same assembly that the script is being hosted in or some other assembly that is known. Another way is to call engine.Runtime.LoadAssembly and feed it assemblies it should know about. See System.Reflection.Assembly for some ways of getting these assemblies.

Hopefully this works a bit better than boo did!

Embedding example
An informative but trying on the eyes tutorial.
IronPython Website
In all of its official and incompletely documented glory.

XNA ContentManager.RootDirectory Considered Annoying as Hell ( XCCAH)

Beware of older XNA code using code created by the new project templates! The newer code sets the root directory of Content to "Content/" which breaks all my old code and was very confusing. Which leaves me wondering if they are going to include that as the default why not change it in the class! Blargg. Now any "game libraries" which have content that gets loaded have to worry about what the RootDirectory setting is at unless they use their own ContentManager.

ContentManager.RootDirectory Property <-- boo

Rolling your own CSS

Having an easily data driven method for customizing style information can be invaluable for building nice user interfaces. For special purpose user interfaces like games it is even more important. Designers need to be able to easily skin and customize different UI elements with out having to alter source code.

UI Elements as Properties

To control the look of user interface elements a simple flat set of properties can be used such as "background-color" or "font". A style sheet is simply a class that you can get and set properties from: class Style { public object GetValue(IStyleable target, string propertyName); public void SetValue(StyleSelecter selecter, string propertyName, object value); } Just like in CSS a common way to refer to elements is needed. I used StyleID, a set of Classes and a Type to replicate the familiar id,class,elementType trio. This is formulated in a simple interface: interface IStyleable { string StyleID { get; } ICollection<string> { get; } string StyleType { get; } } A predicate for if a certain property definition applies is needed: abstract class StyleSelector { public abstract bool Matches(IStyleable target); }

Finding a Property

Every selector must at the very least support the ability to find a property by an O(n) search--this is what the Matches method is all about. Finding a property consists of a search over a list property definitions (StyleSelector,name,value). If a definition's name matches and StyleSelector.Matches is true then the search is terminated and value is returned as the value for that property.

In order for the cascading part to make any sense an ordering of property definitions needs to be defined. CSS solves this problem with the notion of specificity. Properties with more stringent criteria for being applied are allowed to float to the top and are used over the more generic definitions.

For my purposes I organize things simply by id > classes > type > *. So selectors that specify the id of its target are examined first. This conveniently allows for property searches to be culled to smaller sets that must be visited. Hashtables for each of these categories can be constructed to speed up search times so that for example (id,name) is indexed. This reduces linear searching only to custom and other miscellaneous selectors.

Combining Selectors

Aggregated selectors can be formulated based the AND logical operator. Since AND is commutative the selector can be rummaged through for any sub-selectors that are indexable. This can then be used as criteria for which index the aggregation should be inserted into. For example #myButton:hover can be inserted into the id indexing table since it contains an id selector.

I will probably post the code to this and the rest of my GUI toolkit when I am done / bored with it.