Quote:Original post by Zipster
Have complimentary events such as MOVE_UP_START, MOVE_UP_END, MOVE_RIGHT_START, MOVE_RIGHT_END, etc., and send the start event when a key is pressed, and the end event when a key is released.
So I tried that, using a keyboard event handler system I had built before instead of checking the state of the keys which I was using before.
I used
MOVE_LEFT_START
MOVE_LEFT_END
etc. and after some messing around it seems to now work fine for a player. I can't imagine any problems with an AI either so that seems good.
One question about the implementation though:
The way the keyboard system works, it has a map of function pointers associated with keys. To make a key perform a function, you put an entry in the table associating keys, a function and whether it's the key going up or down that it should be associated with. In the Window Procedure, it triggers the appropriate entry in the function table depending on what key has been pressed/released.
So, that means I can't exactly do things the way they were done before. Before I had the PlayerEntityController with an input function that checked the status of the keys and took appropriate action every frame, now because of the way the system works I've put the setting of the keyboard input handlers into the initialization function rather than input since there's obviously no need to put in a new entry of the same thing every frame.
Does this sound alright? It makes the controls seem a little distant from the PlayerEntityController class to me. I'm probably worrying about it for no reason though, since it still does control the setting of its associated keys. So what it does is, it sets the W key or whatever to call the input function of its controlled entities with some specific argument, e.g. MOVE_LEFT_BEGIN.
Another important thing though is that after doing this, if I held down 2 keys and moved diagonally then let go, the plane would often keep moving for a little bit before stopping like it should. So I figured there was some delay in processing the key events. I was confused for a while but today I noticed something. My messages in WinMain were handled by this:
//Handle messagesif( PeekMessage ( &msg, NULL, 0, 0, PM_REMOVE ) ){ TranslateMessage ( &msg ) ; DispatchMessage ( &msg );}
So I was curious and changed it to this:
//Handle messageswhile( PeekMessage ( &msg, NULL, 0, 0, PM_REMOVE ) ){ TranslateMessage ( &msg ) ; DispatchMessage ( &msg );}
Now it appears to work perfectly. Should it have always been while? That seems to make more sense to me, the old system looked like it was just handling a single event every frame no matter how many were on the queue. Now it keeps going til none are left which sounds like it should account for the input lag before.
Just want to make sure that I'm not potentially breaking something that I don't fully understand.
Quote:Original post by bzroom
struct Input{ Vec2 Movement; //[-1,1] representing joypad stick bool Jump; bool Fire;};class Character{public: void SetInput(const Input& i) { mInput = i; } void Update(float dt) { //utilize mInput }private: Input mInput;};
It's pretty easy to have ai control the entities when they're setup like this.
Yeah... that's much like passing a structure of the state like I was thinking earlier. I suppose it's a bit like the system of enums only instead of FIRE_BEGIN and FIRE_END you have bool fire or some such. Might be more elegant if the amount of commands gets high. I'll have to see about that.
So I take it that Update takes action based on the contents of mInput, correct? Does that mean that SetInput() is called every frame to fill mInput with a new updated set of variables?
[Edited by - Sean_Seanston on February 21, 2010 2:13:10 PM]