bobthemac
bobthemac

Reputation: 1172

I want to increment a counter by one in a while loop c++

I am trying to increment a lap counter in my game by one but because I have to put this code in the game loop my counter goes over every time by about 500 instead or moving up one. Here is my code. The checkpointPassed variable is only true when a checkpoint is passed through. I know this works and the checkpoint number is the current checkpoint and they start at 0.

if(checkpointNumber == 0 && checkpointPassed == true)
{
    lapNumber += 1;
}

I can't post the game loop because it is quite large.

Any Help is appreciated.

EDIT

Here is some more of the code so you can see what I am trying to do.

if(distance > carRadius && markerCounter < 5000)
{
    if(checkpointPassed == true)
    {
        markerCounter++;
    }
}
if(checkpointNumber == 0 && checkpointPassed == true)
{
    lapNumber += 1;
}
if(distance < carRadius)
{
    markerCounter++;
    cross->SetX(checkpointX);
    cross->SetY(checkpointY);
    cross->SetZ(checkpointZ);
    checkpointNumber += 1;
    checkpointPassed = true;
}
if(markerCounter > 4999)
{
    checkpointPassed = false;
    cross->SetPosition(0,-50,0);
    markerCounter = 0;
}

Upvotes: 0

Views: 4974

Answers (4)

user1202136
user1202136

Reputation: 11567

Add another two variable called inCheckpoint, which stores whether the user is currently "inside" the checkpoint or not. This allows you to detect when the user enters a checkpoint and only increment the lapNumber then. The code would look as follows:

if(checkpointNumber == 0 && checkpointPassed == true)
{
    if (inCheckpoint == false) /* previously not inside a checkpoint */
        lapNumber += 1;
    inCheckpoint = true;
}
else
{
    inCheckpoint = false;
}

UPDATE: Don't rely on checkpointPassed:

if(distance < carRadius)
{
    if (inCheckpoint == false) /* previously not inside a checkpoint */
        lapNumber += 1;
    inCheckpoint = true;
}
else
{
    inCheckpoint = false;
}

Upvotes: 1

J_D
J_D

Reputation: 3586

If I understand correctly what you said, your 'if' statement is inside the main loop and when you pass a checkpoint, 'checkpointPassed' becomes true. For how long?

If it stays 'true' for a few iterations, then each time your game loop does an iteration,your lap counter is incremented. In this case, you should either set checkPointPassed to false at the end of the iteration, or use a different variable, that you set to true at the same time that checkPointPassed becomes true and false after incrementing.

If this does not answer your question, can you give a little more context as with only this part of the code, it is hard to figure out what you want to do.

Upvotes: 0

Jonathan Leffler
Jonathan Leffler

Reputation: 753890

You might need to cancel the 'checkpointPassed` state.

if (checkpointNumber == 0 && checkpointPassed == true)
{
    lapNumber += 1;
    checkpointPassed = false;
}

This means that you won't be counting the lap again until the next time a checkpoint is passed, which is presumably when you need it counted.

However, if you need checkpointPassed true later in the loop, then you'll need to think whether you need yet another variable, such as lapCounted, which is set to false when checkpointPassed is set to true, and reset to true by the code above (instead of setting checkpointPassed, not as well as setting it).

Upvotes: 0

Attila
Attila

Reputation: 28762

You could set/pass a gueard value that indicates how many iterations in the game loop you are (or whether this is the first iteration). If it is the first iteration (within the current lap), increment the variable as you do now, otherwise don't

You will need to reset this guard value for each lap -- e.g. right after you increment lapNumber.

Upvotes: 0

Related Questions