Archived

This topic is now archived and is closed to further replies.

byork

Event Handler Question

Recommended Posts

int Paused;void FSAPI EventHandler (ID32 event, UINT32 evdata, PVOID userdata) {if (event == KEY_PAUSE_TOGGLE){Paused = !Paused;}}In this snipped, I'm using the Event Handler to determine if the Sim is paused.This is evaluated twice per second, right?My problem appears to be that because it's evaluated twice per second, Pause remains at 0... whereas it should cycle between 1 and 0 each time I pause and unpause the sim.Any way around this? This looks like a pretty good way to determine if the sim is paused (assuming I can get it to work).P.S. This snipped is based on a sample published by Patrick Waugh... so all credit or blame goes to him :( Ciao,

Share this post


Link to post
Share on other sites
Help AVSIM continue to serve you!
Please donate today!

I created this little one way back not C but XML -- You'll get it though :(

<Element><Select><Value>(P:Absolute time, seconds) (G:Var1) == if{ 1 (>G:Var2) } els{ 0 (>G:Var2) } (P:Absolute time, seconds) (>G:Var1)</Value>      </Select></Element>

G:Var2 gives the paused bool.Hope it helps,Roman

Share this post


Link to post
Share on other sites
int Paused;void FSAPI EventHandler (ID32 event, UINT32 evdata, PVOID userdata) {if (event == KEY_PAUSE_TOGGLE){Paused = !Paused;}}In this snipped, I'm using the Event Handler to determine if the Sim is paused.This is evaluated twice per second, right?My problem appears to be that because it's evaluated twice per second, Pause remains at 0... whereas it should cycle between 1 and 0 each time I pause and unpause the sim.Any way around this? This looks like a pretty good way to determine if the sim is paused (assuming I can get it to work).P.S. This snipped is based on a sample published by Patrick Waugh... so all credit or blame goes to him :( Ciao,
you don't seem to have initialised the variable Paused. As you code stands Paused could have any random value initially, so repeatedly toggling it won't cause it to change between 0 and 1.

Share this post


Link to post
Share on other sites

Hmmm, well, I think the problem though is that it's evaluated twice.As a test I changed Pause = !Pausedto:Paused++And then returned Paused as a string to see what it was doing...Each time I pause/unpaused the sim, Paused went up in increments of 2...2...4...6... 8 etcSo it's definitely reading the Pause key event, it's just the counter that's the issue.

Share this post


Link to post
Share on other sites
Hmmm, well, I think the problem though is that it's evaluated twice.As a test I changed Pause = !Pausedto:Paused++And then returned Paused as a string to see what it was doing...Each time I pause/unpaused the sim, Paused went up in increments of 2...2...4...6... 8 etcSo it's definitely reading the Pause key event, it's just the counter that's the issue.
It is typical that a key has a 'down' and an 'up' event.

Share this post


Link to post
Share on other sites

Hmmm, I thought the Event Handler reads the Event itself and not the actual associated keypress... mais non?

Share this post


Link to post
Share on other sites

Try the declaration:bool Paused;I may be picky but the !-operator in principle works on boolean values not integers. I know that C isn't too particular about this but you never know!

Share this post


Link to post
Share on other sites

From a recent update of my internal version of sd2gau....There is a downside to the event handler, particularly if you are looking for a one-shot event such as KEY_GEAR_TOGGLE. Once an event is placed on the system, it remains live until superceded by another event. So, in the case of the G (default gear) key, you will be unable to detect a demand to reverse the action of the gear unless another event has occurred inbetween your keyboard demands. Even then, it doesn’t always work… Oddly enough, one reliable way I found to break the recognition of the current event when trying to track a toggle sequence is to use a timer.

int gear_handlepos=2;			//0=up 1=neutral 2=down(default)double gear_handle_timer=-1;//-----//Gear lever from keyboardvoid Gearlever(ID32 event){  if(event==KEY_GEAR_TOGGLE && gear_handle_timer==-1)  {	lookup_var(&currtick18);	gear_handle_timer=currtick18.var_value.n+1;	if(!gear_handlepos)gear_handlepos=2;	else if(gear_handlepos==2)gear_handlepos=0;  }  return;}//-----  case	PANEL_SERVICE_PRE_UPDATE:	if(gear_handle_timer!=-1)	{	  lookup_var(&currtick18);	  if(currtick18.var_value.n==gear_handle_timer)gear_handle_timer=-1;	}

-Dai

Share this post


Link to post
Share on other sites
Try the declaration:bool Paused;I may be picky but the !-operator in principle works on boolean values not integers. I know that C isn't too particular about this but you never know!
Hey MGH,My 5 hours of precious time for an Integer!You were right... bool did the trick :( Thanks,

Share this post


Link to post
Share on other sites
Try the declaration:bool Paused;I may be picky but the !-operator in principle works on boolean values not integers. I know that C isn't too particular about this but you never know!
The ! operator performs a boolean operation of inverting each bit in the value. It doesn't care one bit what type of value it's looking at in C. However, it's important to remember that in a logical decision involving a boolean result that false is equal to 0, but true is equal to absolutely anything but 0. Since his value isn't initialized to any known value... if it's not a zero, then inverting the bits of the value will not return a 0 value no matter what.As example:Value of 5 in binary is 0101Invert that value and it is now 1010 which is 10.Either value of 5 or 10 would evaulate as a 'true' condition because they're not zero.

Share this post


Link to post
Share on other sites

There's still an inherant problem with a handler to determine whether FS is paused or not. What happens if a user starts an external program while FS is paused -or- Starts FS paused -or- Pause on task switch is checked ? Your pause register is wrong.When FS is paused,,, time stands still.Roman

Share this post


Link to post
Share on other sites
There's still an inherant problem with a handler to determine whether FS is paused or not. What happens if a user starts an external program while FS is paused -or- Starts FS paused -or- Pause on task switch is checked ? Your pause register is wrong.When FS is paused,,, time stands still.Roman
I believe TICK18 does not update any time the sim is paused.

Share this post


Link to post
Share on other sites
I believe TICK18 does not update any time the sim is paused.
Ed, TICK18 still updates with the sim paused, but NOT when FS is in any menu system.Tom

Share this post


Link to post
Share on other sites
Ed, TICK18 still updates with the sim paused, but NOT when FS is in any menu system.Tom
Ok... ELAPSED_SECONDS does not update when paused.

Share this post


Link to post
Share on other sites
The ! operator performs a boolean operation of inverting each bit in the value. It doesn't care one bit what type of value it's looking at in C. However, it's important to remember that in a logical decision involving a boolean result that false is equal to 0, but true is equal to absolutely anything but 0. Since his value isn't initialized to any known value... if it's not a zero, then inverting the bits of the value will not return a 0 value no matter what.As example:Value of 5 in binary is 0101Invert that value and it is now 1010 which is 10.Either value of 5 or 10 would evaulate as a 'true' condition because they're not zero.
The ! operator doesn't perform a boolean operation of inverting each bit in the value. The ! operator toggles the operand between true (non-zero) and false (0). According to "The C Programming Language" by Brian W Kernighan and Dennis M Ritchie (who originally designed and implemented and so know something about it):"The unary negation operator ! converts a non-zero or true operand into 0, and a zero operand into 1."As a consequence your example is incorrect because !5 = 0 and !0 = 1.What you are refering to is the one's complement operator ~ which, according to the same source:"The unary operator ~ yields the one's complement of an integer; that is it converts each 1-bit into a 0-bit and vice-versa."! and ~ are two different operators and are easily confused.

Share this post


Link to post
Share on other sites