Booleans - true and false values

Tracking Boolean State Changes

How do I handle situations where I need to track the history of boolean state changes?

3D art showing a wizard character

When you need to remember previous boolean states, you can store the old value in a separate variable before updating the current state. This is especially useful for detecting changes or transitions in your program's state.

Basic State Tracking

Here's a simple example of tracking a state change:

#include <iostream>
using namespace std;

int main() {
  bool wasAlive{true}; // Previous state
  bool isAlive{true}; // Current state
  int Health{100};

  // Store the previous state before updating
  wasAlive = isAlive;

  // Update current state based on health
  Health = Health - 150; // Take massive damage
  isAlive = Health > 0;

  // We can now check if the state just changed
  bool justDied{wasAlive && !isAlive};

  if (justDied) {
    cout << "Game Over - you just died!\n";
  } else if (!wasAlive && !isAlive) {
    cout << "Still dead\n";
  } else if (wasAlive && isAlive) {
    cout << "Still alive\n";
  }
}
Game Over - you just died!

Tracking Multiple State Changes

You can track multiple state changes by keeping multiple previous states:

#include <iostream>
using namespace std;

int main() {
  // Track battle status
  bool wasInCombat{false};
  bool isInCombat{false};
  bool hasWeapon{true};

  // First update
  wasInCombat = isInCombat;
  isInCombat = true;

  bool justEnteredCombat{
    !wasInCombat && isInCombat};
  if (justEnteredCombat) {
    cout << "Combat started!\n";
    if (!hasWeapon) {
      cout << "Warning: Entered combat without "
        "a weapon!\n";
    }
  }

  // Second update
  wasInCombat = isInCombat;
  isInCombat = false;

  bool justLeftCombat{
    wasInCombat && !isInCombat};
  if (justLeftCombat) {
    cout << "Combat ended!";
  }
}
Combat started!
Combat ended!

By storing the previous state, we can detect important transitions in our program, like when a player enters or leaves combat, when they gain or lose an ability, or when they cross important thresholds.

This is particularly useful in games and interactive applications where you need to trigger specific actions when states change.

Answers to questions are automatically generated and may not have been reviewed.

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved