My code will not work when it is watched

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

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.

When I froze the game with a debug, Character_Health_Timer was almost 8 second higher then character_Health_time. It should have triggered way before it reached 18 seconds. This code won't cooperate. Maybe it's the computer.

Advertisement

Look like the only option is to make a debugger class made for showing me whatever I need to see when the game is running. I really wanted to program my first enemy :(

Sounds like you have a case where health is regenerating and your function is being called. So put a breakpoint there, and alt-tab away to get it to trigger.

Now look at your call stack. Go to the function that called the health regen function and put a breakpoint there. Is that function hit when the game window has focus? If yes, step through the function in the debugger to find out why it isn't calling your health regen function. If not, repeat the process by putting a breakpoint in the function that calls that one.

Rinse and repeat until you find the place where a function isn't being called that you thought should have been called.

Maybe it's the computer.

No; always suspect your code first.

Personally, I think Columbo is correct about the problem.

Second, look at:


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

How large is deltaTicks each frame?

Let's pretend it's 40 millaseconds:

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

Character_Health_Timer += 20 * ( 0.04 );

Character_Health_Timer += 0.8 // '0.8' gets rounded down to 0.0

Character_Health_Timer += 0.0 //Nothing changes

It only gets large enough to affect things if your deltaTime is larger than 50 millaseconds.

This means, if your framerate is larger than 20 FPS, your regeneration doesn't work!

So you need to do two things:

First, make sure your update functions doesn't get called until you accumulate a significant amount of 'deltaTime' (e.g. 1/30th of a second).

Second, for areas of code like this, you need to accumulate the millaseconds locally until you know you have enough time to do something with it.

This means something like:


void Character::Character_Stats_Controller(Uint32 deltaTicks)
{
	if(Game_Mode == GAMEPLAY_MODE)
	{
		/////////////////////////////////
		// HEALTH REGENERATION.
		/////////////////////////////////
		Character_Health_Accumulated_Time += deltaTicks;

		// If the timer passes the wait time
		while(Character_Health_Accumulated_Time >= Character_Health_Required_Time)
		{
			//This can just be a local variable.
			int amountToHeal = (rand() % 3) + 1;

			//Give the character health
			Character_Health += amountToHeal;
                                
			//Subtrack the amount of time we used.
			Character_Health_Accumulated_Time -= Character_Health_Required_Time;

			//Set the amount of time we need to wait until we get healed again.
			//Note: I'm keeping your time in millaseconds, hence the '* 1000'
			Character_Health_Required_Time = ((rand() % 3) + 1) * 1000;
		}

		if(Character_Health > Max_Health)
		{
			Character_Health = Max_Health;  /* Not a magic number */
		}
	}
}

Yup. It was the use of int type. Should have used a float type for "Character_Health_Timer". Nothing but zero when it was rounding..... absolutely, devastatingly terrible at fast math but this is a happy ending regardless.

Thank you all for you help and suggestion, Much appreciated and much love.

This topic is closed to new replies.

Advertisement