[QUESTION] hasStarted = false & if (!hasStarted)

Hello guys,
I have 2 question related to the boolean variable:

  1. Why does hasStarted need to be = false at the beginning?

  2. When we say at the beginnign that hasStarted = false and then say later !hasStarted (which means once again that hasStarted = false) wouldn’t this actaully be a “double denial” and hence by writing!hasStarted we are actually saying that the game actually starts at this point?

I hope this question is not too confusing :smiley:.

Help is very mcuh appreciated!

Have a good one :slight_smile:

hasStarted= false; // has to be false because game didnt start yet, and after starting it will be true and until stopping the game it will be true. İf it is true dont start game over and over…

if(!hasStarted) // this means not true, I mean if it is false
GameBegin();
else
/// dont start again…do something different. dont repead game load music etc.


GameBegin()
{
hasStarted = true;
}

I am not sure if I can explain. My english is not good and I cannot express my thoughts nicely :frowning:

1 Like

Spot on - and do not worry about the English, your response came across very well @mlkyesmen.

The only thing I would add, for @Bryan_FP, is that setting hasStarted = false; is a little bit redundant, although not entirely bad practice.

A boolean value will default to false, so if you didn’t set this either in the Start method or as a class level variable (member), it wouldn’t really matter. It does, however, give a little more readability, e.g. “at the beginning, the game has not started”.

The only other point I would add is that in the first instance you are setting the value to false, whereas the if (!hasStarted) is checking the value of the variable. The ! implies “not”, so it is like saying “if not hasStarted equals true”… which is a short version of if (hasStarted == false).

Hope this helps :slight_smile:

2 Likes