Help with int variables

Started by TDBFW1, Sat 26/08/2017 12:41:09

Previous topic - Next topic

TDBFW1

I I have a variable that equals 3, such as a counter.
I have another variable called failure, which if it reaches 3 will end the game.
For every point in the counter, the failure variable should increase by 1. Is there a simple way to do this, other than say;
Code: ags

if (counter == 1)
{
failure += 1;
}
if (counter == 2)
{
failure += 1;
}

etc.


Matti

Yes, there is:
Code: ags
failure = counter;
;)

But if failure is always equal to counter, why even use two different variables? You could just do this:
Code: ags
if (counter == 3) QuitGame();

TDBFW1

Thanks.
The reason why there is a failure variable is that there will be more than one counter, and each one of them will contribute to the failure variable.
Is there a way to do this?

TDBFW1

Sorry, I've slightly changed my mind on the concept. I will still be interested in the answer tho ;).
However, I've got a very similar query:
I'm using bool variables instead. I have three variables called pink, blue and green. If they are all false, I need to add 1 to failure each time. Is there a way to do this simply?
Also, if:
Code: ags

if ((boolvariable == true) &&
(pink == true)) failure += 1;

Will an else if following this code need to include the boolvariable again, or will it simply accept that boolvariable is true.
I.e.
Code: ags

else if ((boolvariable == true) &&
(green == true)) failure += 1;

or
Code: ags

else if (green == true) failure += 1;


Khris

All of what you mentioned can easily be done.
I'm still not 100% sure how the original question's variables are supposed to work though.

Anyway, a condition in an if clause like "a == 3" usually evaluates to true or false, so if you have a boolean, comparing it to "true" is redundant.
Code: ags
  if (pink) failure++;

That's because "if" doesn't care about the condition's contents, it only cares about the final value; anything between if ( and ) is evaluated to the end before the actual "if" is processed.

As for else (if), AGS (or any other language) doesn't care about which variables you already tested; if any previous test checked out, further else ifs and elses are ignored.
So you either include all relevant variables in every test, or you nest them:
Code: ags
  if (boolvariable) {
    if (pink) failure++;
    else if (green) failure++;
  }
  else {
    // boolvariable == false
    ...
  }


Quote from: TDBFW1 on Sat 26/08/2017 13:22:40I have three variables called pink, blue and green. If they are all false, I need to add 1 to failure each time. Is there a way to do this simply?

Code: ags
  if (!pink) failure++;
  if (!blue) failure++;
  if (!green) failure++;


However if the actual goal is to check all three bools, you can simply do this:
Code: ags
  if (!pink && !blue && !green) die();


Final note: if you add bools, true becomes 1 and false becomes 0. So you can actually do this:
Code: ags
  if (pink + blue + green == 0) // all are false

TDBFW1

So the bool variable "pink" will always be seen as true, and "!pink" is always false?
Sorry, I'm a beginner at programming. Thanks a lot for the other answers

Khris

No, pink is a bool variable, meaning its value is either true or false.
The exclamation mark is the NOT operator which will turn true into false and the other way around.

if (!pink) ... is the same as if (pink == false) ...
You read it as "if not pink, ..."

Code: ags
  bool pink = false;
  if (!pink) Display("I'll show up.");
  else Display("I won't.");

TDBFW1


SMF spam blocked by CleanTalk