Wednesday, June 10, 2009

It's been a little while since my last update, I got distracted by a different XNA project that I haven't yet documented. Im going to finish up the InputWrangler discussion, then move on to things from this new project, pretty heavily involved with HLSL.

So according to the previous post we have defined a Control which has a name and one or more inputs it has to poll to know it's status. The messy part here is that while Microsoft has made it easy to poll a single input to know it's status, such as a game pads left shoulder button using GamePad.GetState(players[1]).Buttons.LeftShoulder() this doesn't translate well to polling a control state dynamically. The method required is very specific, but we need a way to tell a controller to pole this input specifically without wanting to go through a huge switch statement of all possible inputs every time to do it. We need a dynamic way to assign the polling method to something.

One basic way we can do this is to create an Interface which poles an input, then create an implementing Class for each input which we want to make available.

public interface InputTrigger
{
public static float getInput(GamePadState gpState);
}
public class LeftShoulderButton : InputTrigger
{
public static float getInput(GamePadState gpState)
{
return gpState.Buttons.LeftShoulder();
}
}

Than the control would only need a List<InputTrigger> initialized at startup with the right InputTrigger implementing class instances, and it could test all of its triggers by walking the list:

List<InputTrigger> inputTriggers = new List<InputTrigger>();
public void InitInput()
{
inputTriggers.Add(new LeftShoulderButton());
}
public float Pole(){
float value = 0.0f;
foreach (InputTrigger it in inputTriggers)
{
float input = it.getInput();
if (value == 0.0f)
value = input;
}
return value;
}

This works just fine, the control only has to pole the input triggers it's interested in without having to walk an entire list of all possible inputs to get the ones it wants. But it's still inefficient, we have to create a class for each input, and a class instance for each input we wish to pole. There must be a way to assign the getInput() method directly to something without needing the class surrounding it. And there is!

Most modern languages support a programming pattern called Anonymous Functions (not Methods, because Methods are a part of a class which these are not). In short Anonymous Functions allow a program to assign code to a variable dynamically at run time. In pseudo code an anonymous function would look something like this adsf

var aMethod = { return x + x; }
print aMethod( 5 );
aMethod = { return x * x; }
print aMethod( 5 );

The results of running this code would unsurprisingly be:sdf

5
25

But for strongly typed languages such as Java and C# this simple syntax wont work. Among other problems, how is the return type defined, and what defines the parameters? In version 3 of C# the languages developers solved these problems by introducing delegates. To use delegates and assign them to variables you must first define a delegate type:

delegate float TriggerType(GamePadState gpState);

This solves the problem of how to define the return type and parameters for our Anonymous Function. Once you have a type you can create variables that will hold Anonymous Functions which conform to that type:

private TriggerType triggers;

And assign code to them:

triggers = delegate(GamePadState gpState)
{
if (gpState == null || !gpState.IsConnected) return -1.0f;
if (gpState.Buttons.LeftShoulder == ButtonState.Pressed)
{
return -1.0f;
}
};

Now the control can test it's trigger by simply calling trigger(gamePadState) and it doesn't have to know anything at all about what input is being poled. But what if we have more than one trigger, do we still need to create of a List of trigger delegates? No, that's one of the cooler things (and more dangerous things) about triggers, they stack. You can add another trigger to be eveluated after the first one like this:

triggers += delegate(GamePadState gpState)
{
if (gpState == null || !gpState.IsConnected) return -1.0f;
if (gpState.Buttons.RightShoulder == ButtonState.Pressed)
{
return -1.0f;
}
};

Now both LeftShould and RightShoulder will be poled on one call to triggers(gamePadState). I call this dangerous because it's an example of hidden functionality, By simply looking at the trigger variable there is no way of knowing how many delegates will be assigned to it, or what all of them might do, this can cause unexpected behaviors that would be a pain to debug. Use with caution. There is a problem here, we will only ever see the last value returned, the test for RightShoulder, LeftShoulder results are forever hidden (another example of hidden functionality and the problems it can cause). To solve this we're going to have to pass our return value along to any fallowing triggers so that they can decide if their results are more important than results already rendered. For the purpose of this example we'll make it so that if the previous results are > 0 we'll leave them alone, but if they are 0 we'll overwrite it with our results.

The problem is there is no way I know of that a delegate can see the return value of a delegate that executed before it. So we're going to have to use a reference parameter to pass the return value. The code changes like this:

delegate void TriggerType(GamePadState gpState, ref float value);

triggers = delegate(GamePadState gpState, ref float value)
{
if (value > 0 || gpState == null || !gpState.IsConnected) return;
if (gpState.Buttons.LeftShoulder == ButtonState.Pressed)
{
value = 1;
}
};
triggers += delegate(GamePadState gpState, ref float value)
{
if (value > 0 || gpState == null || !gpState.IsConnected) return;
if (gpState.Buttons.RightShoulder == ButtonState.Pressed)
{
value = 1;
}
};


Notice the use of += on the second assignment, the new delegate is added to triggers without removing the last one. Note that C# does not guarantee in which order delegates chained like this will be executed. Now we pole our triggers by passing in a variable that will be set to the resulting value: triggers(gamePadState, ref ourValue). We'll get the value of the first pole that returns something other than 0. We only have '1' as a possible return value in these sample, but analog stick triggers can return float values between 0.0 and 1.0 and mouse triggers can return any value from 0 to the height and width of your current screen.

There's one last special case we need to deal with. Even Microsoft wasn't big on the idea of creating a poling method for each and every possible key hit on all possible international keyboards, so the poling method for them accepts a string input of what you want to pole. You can pole a keyboard key like this: keyState.IsKeyDown(“Down”). This is great, we only need one delegate to handle all possible keyboard keys and we just pass it in the Trigger value from our configuration file. We create a method to do this for us:

static TriggerType BuildKeyTriggerDelegate(Keys keyIn)
{
return delegate(KeyboardState keyState, ref float value)
{
if (keyState == null) return;
if (keyState.IsKeyDown(keyIn))
{
value = 1;
}
};
}

This exposes something particularly tricky about delegates. Notice how the keyIn parameter is never passed directly to the delegate, but the delegate uses it via the context of the BuildKeyTriggerDelegate method that creates it, and that context survives the life of the delegate! You might think that because the BuildKeyTriggerDelegate method is static multiple key trigger delegates might share the same context and collide with one another, but such is not the case, each call to BuildKeyTriggerDelegate creates it's own unique context for the delegate it builds.

There, now our initialization code can read the configuration file and build controllers with delegate trigger chains that execute quickly and efficiently. We essentially moved the huge switch statement from the runtime code to the initialization code, which is much better.

All that being said, while doing a little research for this post I found that behind the scenes the C# compiler is very likely creating wrapper classes around all our delegates anyway, so there is probably no runtime advantage between our version that created class instances for each trigger and the one with delegates. Oh well.