basic if statement

Started by morph100, Fri 09/11/2018 10:55:54

Previous topic - Next topic

morph100

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!");}
}

CaptainD

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.
 

CrashPL

#2
If you're wondering why it doesn't work - you're using the assignment (=) operator, when you should use the comparison (==) instead.

Code: ags
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.

Cassiebsg

I'll add that you can change caropen==false to !caropen as putting a ! in front of a bool will make it negative.

There are those who believe that life here began out there...

Slasher

Yes, double == to check if true/false...    Regardless of that... show us your if player has inventory script...

morph100

Great thanks for your swift responses I will use the == and clean up my {}


Khris

Here's the shortest version:

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

CaptainD

Wow I never realised there were so many different ways to achieve the same thing!
 

Crimson Wizard

#8
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.

Retro Wolf

Very informative, thankyou very much CW!

SMF spam blocked by CleanTalk