Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: TDBFW1 on Sat 26/08/2017 12:41:09

Title: Help with int variables
Post by: TDBFW1 on Sat 26/08/2017 12:41:09
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) Select

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

etc.

Title: Re: Help with int variables
Post by: Matti on Sat 26/08/2017 12:49:17
Yes, there is:
Code (ags) Select
failure = counter; ;)

But if failure is always equal to counter, why even use two different variables? You could just do this:
Code (ags) Select
if (counter == 3) QuitGame();
Title: Re: Help with int variables
Post by: TDBFW1 on Sat 26/08/2017 13:02:39
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?
Title: Re: Help with int variables
Post by: TDBFW1 on Sat 26/08/2017 13:22:40
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:

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.

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

or

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

Title: Re: Help with int variables
Post by: Khris on Sat 26/08/2017 14:23:27
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.
  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:
  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?

  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:
  if (!pink && !blue && !green) die();

Final note: if you add bools, true becomes 1 and false becomes 0. So you can actually do this:
  if (pink + blue + green == 0) // all are false
Title: Re: Help with int variables
Post by: TDBFW1 on Sat 26/08/2017 14:52:16
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
Title: Re: Help with int variables
Post by: Khris on Sun 27/08/2017 18:33:00
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, ..."

  bool pink = false;
  if (!pink) Display("I'll show up.");
  else Display("I won't.");
Title: Re: Help with int variables
Post by: TDBFW1 on Thu 31/08/2017 12:21:40
Thanks, I understand now.