I need help with this Lesson

Hello, I really struggled to keep up with this lesson, is anybody able to explain me why in this lesson we use booleans functions in the first place and why do we place it false in SetupGame() and true in EndGame().

How do I decide what happens in the code when the condition is satisfied or not, is this not kind of similar to if statements? ( I am literally confused)

Thank you for any help.

Riccardo

That is assigning a true/false value to bGameOver which is used in OnInput.

void Example()
{
    if (bGameOver)
    { 
        // reset the game
        bGameOver = false;
    }
    else 
    {
        // play the game
        if (/* the game over e.g out of lives */)
        {
            bGameOver = true;
        }
    }
}

So it’s initially false, and then when the game ends it’s set to true so that the first part can be done to reset the game and then it’ll assign the value false for the next time it’s called.

I am sorry Dan, I am not sure that I entirely understood.

So we set

bool bGameOver 

in the header file to make it a member variable (meaning it can be used anywhere in the cpp file).
Then we call this variable using an if statement in the OnInput function. We clear the screen, reset the game and we assing a false value.

Why do we assign a value in the first place? Is it to determine what bGameOver has to do when called and a certain condition is met?
I am getting confused with the purpose of the value in booleans and the if statements…is it me or they sort of do the same thing?

My goal here is to understand the logic behind how, why and what bGameOver does to set the end of the game. Also, If you can explain more in normal text rather than in codeblocks (when not required) its much appreciated. :pray: :pray:

Thank you for the help to the community btw and sorry if I am particularly needy :sweat_smile: I just want to make sure I make the most out of this course.

Riccardo

Anywhere in the class, specifically. There’s nothing tying a .cpp to a class.

class Example
{
    int x;
public:
    void Foo();
};

// Non-member Foo()
void Foo()
{
    x = 10; // what's x?
}

void Example::Foo()
{
    x = 10;  // okay
}

“Call” isn’t appropriate here. “use” or “read the value of”. You call functions not variables.

That code within that block only executes If and only if bGame is true.

if (bGameOver)
{
    // code in here only done if bGame == true
    ClearScreen();
    SetupGame();
}

So that the right part of the code executes when is first run.

They do not. if statements only execute code if a condition is true; booleans just hold a true or false value.

It’s used to control the flow of the the function, see the code in my last post.

1 Like

Thanks a lot for the patience, its much clearer now. I eventually got my head around it and it was way simpler than I imagined. :sweat_smile: :sweat_smile:

Also, I appreciated the clarifications and explanations on using the terminology, they do make a difference.

Riccardo

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms