Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: morph100 on Fri 09/11/2018 10:55:54

Title: basic if statement
Post by: morph100 on Fri 09/11/2018 10:55:54
So i have one working IF statement where I am using if player has inventory item but now i am trying to do another against a Bool variable setting, here's what I have so far:

function oCar_Look()
{
if (caropen = false) {player.Say("That's my car, it's locked");}
else {player.Say("It's unlocked!");}
}
Title: Re: basic if statement
Post by: CaptainD on Fri 09/11/2018 11:08:38
Not 100% sure what the actual question is, but I think the following should work:

if (caropen==false) player.Say("That's my car, it's locked");
else if (caropen==true) player.Say("It's unlocked!");

(You don't need to nest the player.Say insice {} because you're only using a single action for the IF statement.
Title: Re: basic if statement
Post by: CrashPL on Fri 09/11/2018 11:11:52
If you're wondering why it doesn't work - you're using the assignment (=) operator, when you should use the comparison (==) instead.

Code (ags) Select
function oCar_Look()
{
if (caropen == false) player.Say("That's my car, it's locked");
else player.Say("It's unlocked!");
}


Also be sure if the bool value 'caropen' exists and is set.
Title: Re: basic if statement
Post by: Cassiebsg on Fri 09/11/2018 11:13:08
I'll add that you can change caropen==false to !caropen as putting a ! in front of a bool will make it negative.

Title: Re: basic if statement
Post by: Slasher on Fri 09/11/2018 11:14:19
Yes, double == to check if true/false...    Regardless of that... show us your if player has inventory script...
Title: Re: basic if statement
Post by: morph100 on Fri 09/11/2018 11:25:30
Great thanks for your swift responses I will use the == and clean up my {}

Title: Re: basic if statement
Post by: Khris on Fri 09/11/2018 12:08:31
Here's the shortest version:

function oCar_Look()
{
  if (caropen) player.Say("It's unlocked!");
  else player.Say("That's my car, it's locked");
}
Title: Re: basic if statement
Post by: CaptainD on Fri 09/11/2018 12:23:16
Wow I never realised there were so many different ways to achieve the same thing!
Title: Re: basic if statement
Post by: Crimson Wizard on Fri 09/11/2018 12:54:04
Quote from: CaptainD on Fri 09/11/2018 12:23:16
Wow I never realised there were so many different ways to achieve the same thing!

Well, some results above are achieved with redundant operations.

"If" statement tests the result of boolean expression.
When you do "if (var)" that expression is calculated as boolean value of a variable.
When you do "if (var == true), then two operations are performed: 1) boolean value of a variable, 2) comparing boolean value to another boolean value, and result is tested by "if".

It's like, you may do "x = 2 + 2;" or "x = 4", the result is same but in one case you are doing excessive calculation.

Same with "if/else if", if your condition in the "else if" is strictly the inverse of the first condition under "if", then its redundant and "else" will suffice.
Title: Re: basic if statement
Post by: Retro Wolf on Fri 09/11/2018 20:26:42
Very informative, thankyou very much CW!