Every Tuesday, Mike Sylvester brings you REVOLUTIONARY, a look at the wide world of Wii possibilities.

In the coming weeks, I'll be detailing the process of writing a GlovePIE script, from concept, through testing, to completion. You will see that it's not so tough to get something running, and I hope you'll also get a better understanding of the mechanics of the hardware inside the Wii Remote and its accessories.

This time around, I'll be deconstructing the simplest of the GlovePIE scripts I've written to date, but also one of the most rewarding -- Alien Hominid.

The story of the Alien Hominid franchise's development is inspirational. A team of indie developers releases a free game to the PC gaming masses, then forms an indie company with a few other ambitious individuals, and they turn the IP into a commercial console success. The original game prototype is still freely available to play, but next to its console successors, its flaws become glaringly obvious. Being only one level long, one would think the length of the game is the biggest problem, but to complete that level you must suffer the controls. The keyboard is less than ideal for the twitch movements this game requires, and it's made even more nightmarish by forcing us to use the right-hand oriented arrow keys for movement and left-hand oriented keys for actions. It's grandma's recipe for carpal tunnel syndrome.


What does not kill us leaves us crippled


This is where the Wiimote flies in to save your weary digits and the day. GlovePIE makes it a sinch to orient simple keyboard controls in a way that's familiar and comfortable, and makes this PC game feel more akin to its console brethren.



Doesn't that look better? There's a number of ways to get the keyboard configuration to translate to those buttons in GlovePIE, but the code below is tested and proven to work. Not every game will respond to your commands or statements the way you expect, so you may have to try a few different methods of doing what logically amounts to the same thing. Something to look for in scripts and other program code is developer comments. Comments are notes inserted by the script writer or programmer to explain how something works, or how it will affect the running of the program if changed. In GlovePIE // signifies the beginning of a comment on a line of code. When GlovePIE is running the code, it will not attempt to run anything written after the doubled forward-slashes on a line. You can also comment multiple lines by placing /* where you want to start your comment, and */ where you want to end it.

Keyboard.A = Wiimote.One //Wiimote button 1 acts like keyboard key A -- shoot
Keyboard.S = Wiimote.Two //Wiimote button 2 acts like keyboard key S -- jump



Context color coding
GlovePIE automatically colors code it recognizes

Now we're going to set the Wiimote's D-pad to emulate the keyboard's arrow keys. We could do it the same way as above, where a Wiimote input is set to equal a keyboard input, but after testing, I found using that for the directional controls made the game play slightly sluggishly. Like I said, you never know exactly how a game or program is going to respond to your code until you test things out.

If Wiimote.Right //checking to see if the Right direction on the D-pad is "true" (tapped or pressed)
Up = True //keyboard key Up is held in this condition
Else //if the condition is not met, the following is performed
Up = False //keyboard key Up is not held
EndIf //ends this statement


The Remote's primary orientation is vertical like a TV remote control, and GlovePIE has the D-pad's directions mapped accordingly. The program doesn't change the labeling when you turn the controller sideways, so we compensate in our code. Right is up, left is down, up is left, and right is down.

If Wiimote.Left
Down = True
Else
Down = False
EndIf
If Wiimote.Up
Left = True
Else
Left = False
EndIf
If Wiimote.Down
Right = True
Else
Right = False
EndIf


And that right there is a complete script. You can copy and paste those lines of code into GlovePIE, run it, and play through the game as I have. It's important to understand that every game played with the Wiimote must have waggle, or rumble, or fancy LED shows, but you're free to add in any of that if you like. It's your game now, so do whatever you can to enrich it.

Does it get any better than this?

I made the above video immediately after pairing my Remote with my computer. Since, I hadn't run the script prior to filming and I didn't include any code to turn off or otherwise stop the LEDs from their "discoverable mode" flashing, they flashed throughout the video like a beacon drawing forth skeptics to yell "FAKE!" I've noticed that any normal LED activity (1 LED on, no LEDs on, or LEDs steadily flashing) is something that will often be cited by the "fake callers" as evidence to their cause. Right now, we're going to guard against that by putting in some code to make the LEDs do something they wouldn't under normal operation. You see, we have complete control over the LEDs, so whether they're on, off, flashing, or "KITT scrolling" is up to us.

If 0 = 0 //trick GlovePIE into starting a loop, like saying "if the sun is hot, do this"
If var.KITT = 0 //checking to see if the value of this variable is equal to 0
Wiimote.Leds = 1
//lighting LED1
EndIf
//end the nested statement
If var.KITT = 1
//checking to see if the value of this variable is equal to 1
Wiimote.Leds = 3
//lighting LED1 and LED2
EndIf
//end the nested statement
If var.KITT = 2
//checking to see if the value of this variable is equal to 2
Wiimote.Leds = 6
//lighting LED2 and LED3
EndIf
//end the nested statement
If var.KITT = 3
//checking to see if the value of this variable is equal to 3
Wiimote.Leds = 12
//lighting LED3 and LED4
EndIf
//end the nested statement
If var.KITT = 4
//checking to see if the value of this variable is equal to 4
Wiimote.Leds = 8
//lighting LED4
EndIf
//end the nested statement
If var.KITT = 5
//checking to see if the value of this variable is equal to 5
Wiimote.Leds = 12
//lighting LED3 and LED4
EndIf
//end the nested statement
If var.KITT = 6
//checking to see if the value of this variable is equal to 6
Wiimote.Leds = 6
//lighting LED2 and LED4
EndIf
//end the nested statement
If var.KITT = 7
//checking to see if the value of this variable is equal to 7
Wiimote.Leds = 3
//lighting LED1 and LED2
EndIf
//end the nested statement
Wait 100ms
//wait 0.1 seconds before processing the next line
var.KITT = (var.KITT + 1) % 8
//incrementing (by 1) the value of this variable that is checked throughout the statement; restarts from 0 when it reaches 8
EndIf
//end the statement


Perhaps the #1 reason why I'm so enamored with writing my own scripts for game controls is that I can enhance the games with features that may not have been included by the developers. This script above is complete, and the game will play (more or less) as the developers intended with it as is, but why stop here when we can add auto-fire?

If pressed(Wiimote.A) //checking to see if the A button is pressed and released
toggle(var.AF)
//flip this variable between "True" or "False" if the above condition is met
EndIf
//end the statement

If Wiimote.One //checking to see if the 1 button is "true" (tapped or pressed)
If var.AF
//checking the "on" or "off" status of this variable we affected in the previous statement
Key.A = True
//keyboard key A is held
Wait 100ms
//waiting 0.1 seconds before processing the next line
Key.A = False
//keyboard key A is released
Wait 100ms
//waiting 0.1 seconds before processing the next line
EndIf
//end the nested statement
EndIf
//end the statement


Auto-fire and KITT-scrolling in effect!

And there you have it. Although Alien Hominid remains brutally difficult, you can now lay down a steady spray of weapon fire without taking your finger off the 1 button. This was a case of a game desperately needing a new control scheme just to become playable, but we can write scripts for any reason. Maybe you want to make a game more challenging to play, or feel more authentic. We'll cover more possibilities next week in Revolutionary.

Updated: Thanks to James for the correction

This article was originally published on Joystiq.