I like the idea of Autonomous Inputs but in this programming context (Windows DX9 Game) I don't see it solving any major problems that I haven't already solved with other methods.
There's also a number of other small issues this thread didn't cover.
I'd like to thank Libervurto since I find this method of condensing everything into one line quite clever. I'd like to thank Comet since the bitwise details are insightful.
Getting started.
Quote: "One of the most common needs for interactive programs is the ability to detect distinct inputs; that is to detect the entire duration of a key/button press as a single event.
Previously this required additional variables, for example"
Using more than one variable isn't all that big of a concern where inputs are involved. Memory and CPU resources vastly out supply the needs here. Your example is a bit crude, but completely valid.
Quote: "You can simplify it even more with bitwise operators. This here is code from my micro controller project, which can handle 8 buttons with just one calculation."
Now if this was at a low hardware level like a micro controller (which I have done and certainly appreciate) then this sort of detail and expended effort would be crucial. While impressive skill is on display here, doing this won't exactly get me any meaningful performance boost in a typical DBP game.
An Example.
I'll provide an alternative method, but I'll also mention some overall organizational structure at the same time. You could squeeze Autonomous inputs into this example, but like I said it's really not too necessary for most purposes.
`Written By Mage Aug 2013
`Create A Button Map
Dim ButtonMap(5,1)
Button_Init()
Do
`Handle One of the mapped buttons for demo purposes
Jump = Button_Jump()
If Jump = -1 Then Print "Jump Released"
If Jump = 1 Then Print "Jump Pressed"
If Jump = 2 Then Print "Jump Held"
Wait 500
Loop
End
`Jump Button
Function Button_Jump()
Button_Poll(0)
retVal = ButtonMap(0,1)
EndFunction retVal
`Move Player Up
Function Button_MoveUp()
Button_Poll(1)
retVal = ButtonMap(1,1)
EndFunction retVal
`Move Player Down
Function Button_MoveDn()
Button_Poll(2)
retVal = ButtonMap(2,1)
EndFunction retVal
`Polls Mapped Buttons and records state to memory
`-1 Released, 0 Not Pressed, 1 Pressed, 2 Held
Function Button_Poll(bIndex)
bVal = KeyState(ButtonMap(bIndex,0))
If bVal = 1
If ButtonMap(bIndex,1) => 1
ButtonMap(bIndex,1) = 2
Else
ButtonMap(bIndex,1) = 1
EndIf
Else
If ButtonMap(bIndex,1) > 0
ButtonMap(bIndex,1) = -1
Else
ButtonMap(bIndex,1) = 0
EndIf
EndIf
EndFunction
`Maps Keys to Button Map for demo purposes
Function Button_Init()
ButtonMap(0,0) = 57 `Jump - Space Key
ButtonMap(1,0) = 17 `Move Up - w key
ButtonMap(2,0) = 31 `Move Down - s key
EndFunction
Now that's a lot more code then just one or two lines but there are some advantages. You can easily change and swap the buttons, especially if you make a menu in your game for remapping the controls. You can add mouse support by mapping mouse buttons as negative numbers. Gamepad and axis' can be supported similarly by adding a "controller mode" variable and using float variables instead of integer for states.
When you do it this way, the entire program can access player input and it's easy to read. The framework and button scheme are consistent across the entire program.
Button States.
The button states are arranged better...
1 and 2 are Pressed and Held so you can do things like
If Button_Jump() > 0 Then ... `If you want action when the button is held.
If Button_Jump() = 1 `If You want just single action press
The Released state is -1 which is more convenient since you can group it better with 0.
If Button_Jump() < 1 `Button is not being pressed or was just released.
Wrapping Up.
I mostly use a Gamepad but I felt most people don't do this and that adding it into the example would just complicate things. I also wanted to give some practical suggestions. If you were really designing a game for other people to use, they will want to remap the controls.
Just to summarize, I feel that while Autonomous Inputs are interesting, but they really don't help in the long run for the type of programming that this is. It's better to design a flexible but efficient system than to try to squeeze logical operators all onto a single line or space.