Skip to main content
gamedev.net
🔒 Locked

Direct Input -- continous input?

Started by nobird Sep 9, 2003 at 8:24 AM 7 replies 650+ views
Original Post
nobird
nobird
Hi all, If I want to control a character in a action game... when the users press the "left" button, the character walk to the left, if the users press the left button twice, the character run to the left, how to code it? If I just check the state of the key(true/false), it will only know that the user pressed left button at that moment... How to distinguish the users press once or twice? Similarily, a sequence of pressed key? (e.g. left, left, a => running kick, left, left, b=>running punch) Thanks ~nobird
Pipo DeClown
Pipo DeClown
Answer one:
Put a timer system on all your input.

if( now > last )
{
Input();
}



Answer two:
Put an update function and make an array of ints, to store the time in. Update and check if the key can be pressed, if not, then when the user presses the key, ignore!

if( TimeForKey[key] < 0 )
{
//DO THE KEY STUFF
TimerForKey[key] = 500; // 0.5 second
}

bool Update()
{
static int last=0;
int dif = GetTime() - last;
//loop
TimeForKey[ i ] -= dif;
//endloop
last += dif;
}

.lick
SGreth
SGreth
DirectInput can buffer input. It sounds like that''s what you want. Rather than picking the key-states as a snapshot using GetDeviceState() you should look into using GetDeviceData() and setting up buffers. Check out the SDK for examples. By using buffered input the only way you''ll ''lose'' keystrokes is by setting your buffer size too small and not checking it often enough.
"The difference between insanity and genius is measured only by success."~Bruce Feirstein
JoeyBlow2
JoeyBlow2
quote:
Original post by SGreth
DirectInput can buffer input. It sounds like that''s what you want. Rather than picking the key-states as a snapshot using GetDeviceState() you should look into using GetDeviceData() and setting up buffers. Check out the SDK for examples. By using buffered input the only way you''ll ''lose'' keystrokes is by setting your buffer size too small and not checking it often enough.



Took the words from my mouth. GetDeviceData and GetDeviceState can both be used in your program, and both can be used depending on your needs. One if buffered, the other unbuffered. You want buffered. Meaning only changes since the last call will be returned. The other is instant, tell you what the keys are at the moment of the function call.
nobird
nobird
Hi all,
I still don''t quite understand the concept how to make it...

Now, eg, if I have 4 actions:
1. left(press without releasing it) => walk to left
2. left, left(quickly press and release twice within 0.2s) => run to left
3. left, down, left (quickly press and release within 0.2s) => super punch A
4. left, down, left, left => super punch B

How could I make it?

In case if I use GetDeviceState only, I can''t do action 2.
If I use GetDeviceData only, how can I know action 1?
If I use both methods together, is that for action 1, I check for the result in GetDeviceState; for action 2, I check for GetDeviceData?

*And the most difficult part I don''t understand: how to distinguish between action 2 and 3?
Eg. if I press left, down, left within 0.2s, the action should be action 3, but how can I avoid action 2 to be executed? Because the conditions in both actions are valid...
Similarily, left, down, left, left => action 4 but not action 3?

~nobird
SGreth
SGreth
quote:
Original post by nobird
*And the most difficult part I don''t understand: how to distinguish between action 2 and 3?
Eg. if I press left, down, left within 0.2s, the action should be action 3, but how can I avoid action 2 to be executed? Because the conditions in both actions are valid...
Similarily, left, down, left, left => action 4 but not action 3?

~nobird


It sounds like you''re going to need to create a command tree.
Simply put, you''re going to create a tree (AVL, Black/white, whatever...) that contains all of the keystrokes that lead to a move being executed.

Each time you process a key event you see what node you''re currently at in the tree.

(1) See if the key you''re about to process will continue a string of commands (i.e. up, up, down is hyper-punch, and you''re at the second up....down is a valid option, anything else resets you to the top of the command tree)

(2) See how much time has elapsed since a button/key has been pressed. *Note: You''ll need to keep a timestamp and update it each time a key is pressed.

(2.1) If too long of time has elapsed, consider the current move/tree-traversa invalid and start at the beginning of the tree *OR* execute the current command.
Note: This solves the problem you''re having when you''re trying to differentiate between command 2 and command 3. If the user wants to run command 2 then they''ll stop hitting buttons for a small period of time, and then you know that''s the command they''re trying to execute.

Good luck!
~S''Greth
"The difference between insanity and genius is measured only by success."~Bruce Feirstein
nobird
nobird
Hi,
Thanks all~~
That''s sound great... I should try this out first... And, is there any "standard/typical" method for doing this kind of stuff? Tree+Timer? Timer only is enough?

~nobird
SGreth
SGreth
I wish I could tell you what''s standard or not, but the tree solution is just something that came off the top of my head It''s not something I''ve coded up, but it seems like a logical way to do what you needed.
"The difference between insanity and genius is measured only by success."~Bruce Feirstein

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.