My code will not work when it is watched

Started by
13 comments, last by LAURENT* 8 years, 3 months ago

My code will not work when watched. I programmed my character to auto regenerate it's health but it only seem to regenerate when I pull up another window over my game's window. Does anyone have some kind of idea why this happens?

The scenario is this in a nutshell.

1)Start Game

2)Jump

3)Minus health with every jump

4)Waits for health to regenerate.

5)Nothing.

6) Clicks on another window

7) returns to game window

8)character regenerate health.

I thought I was being impatience but I ran several test, this code is acting funny.

Advertisement

Sounds like it is regenerating, but the screen isn't redrawing like it's supposed to.

When the window gets covered, it invalidates the region, and when the window is uncovered again, it forces a repaint which happens to show the health now full.

Debug the program using a debugger and see for sure.

Sounds like it is regenerating, but the screen isn't redrawing like it's supposed to.

When the window gets covered, it invalidates the region, and when the window is uncovered again, it forces a repaint which happens to show the health now full.

Debug the program using a debugger and see for sure.

Doesn't seem like that the problem. I put a break point in a spot that should alert me if values were changing. Does a break point count as debugging?


Doesn't seem like that the problem. I put a break point in a spot that should alert me if values were changing. Does a break point count as debugging?

Well.... does the breakpoint hit when you switch to another window? If so, what leads to that code being called? What does the callstack look like? Then try to figure out why that code wouldn't get called when you don't switch windows.


Doesn't seem like that the problem. I put a break point in a spot that should alert me if values were changing. Does a break point count as debugging?

Well.... does the breakpoint hit when you switch to another window? If so, what leads to that code being called? What does the callstack look like? Then try to figure out why that code wouldn't get called when you don't switch windows.

Yes it does, only when I click on another window does the break point activate. I will post up the function.

[


void  Character::Character_Stats_Controller(Uint32 deltaTicks)
{
	if(Game_Mode == GAMEPLAY_MODE)
	{
		/////////////////////////////////
		// HEALTH REGENERATION.
		/////////////////////////////////
		Character_Health_Timer += 20 * ( deltaTicks / 1000.f ); 
		if (Character_Health < 20)
		{
			

			if (Character_Regeneration == false)
			{
				// Set wait time
				Character_Health_Time = rand()% 3 + 1;
				// Set health recovery
				Health_Plus = rand() % 3 + 1;
				//Ready to regenerate
				Character_Regeneration = true;
				//Start at zero
				Character_Health_Timer = 0;
			
			}
			
			// If the timer passes the wait time
			if (Character_Health_Timer >= Character_Health_Time)
			{
				//Give the character health
 				Character_Health += Health_Plus;
				//Set the health to false to be ready to recover again. 
   				Character_Regeneration = false; 			
			}
		}
		//Max health, magic number. 
		if (Character_Health > 20)
		{
			Character_Health = 20;
		}
	}
}

This is my code.

(New member here, I'll try to be as helpful as possible.)

Like Servant previously stated, it sounds like there is a problem with how the program is redrawing. Can you show us how you render/update your character class?

Yes, breakpoints count as debugging.

Are you saying Character_Stats_Controller() only breaks when another window has focus?

Keep going back. What function calls Character_Stats_Controller()? Does that function also refuse to trigger the breakpoint when the game window has focus?

What about the function before that? Keep going farther back (up the stack of calls i.e. "up the callstack") until you find the function where it always runs, regardless of window focus.

What's the main loop look like? Post the entire main loop.

What parts of the code react to the window losing focus/gaining focus?
Is Game_Mode accidentally changing value (perhaps to PAUSE_MODE or something) when the window loses focus?

Congrats on the clean-looking code, by the way. It looks clear, which helps with debugging.

A new kind of Heissenbug! smile.png

My guess would be that Character_Health_Timer is an integer and that the term:

Character_Health_Timer += 20 * ( deltaTicks / 1000.f );

Is implicitly rounding, and ends up adding zero each frame when you play normally.

When your app is off-screen, deltaTicks is large enough that a non-zero value can actually be added.

This topic is closed to new replies.

Advertisement