Author Topic: basic if statement  (Read 163 times)

basic if statement
« on: 09 Nov 2018, 10:55 »
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

    • I can help with play testing
    •  
    • I can help with story design
    •  
    • I can help with voice acting
    •  
    • CaptainD worked on a game that was nominated for an AGS Award!
      CaptainD worked on a game that won an AGS Award!
Re: basic if statement
« Reply #1 on: 09 Nov 2018, 11:08 »
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

  • The dinosaur guy
    • I can help with AGS tutoring
    •  
    • I can help with animation
    •  
    • I can help with characters
    •  
    • I can help with play testing
    •  
    • I can help with scripting
    •  
    • I can help with translating
    •  
    • CrashPL worked on a game that was nominated for an AGS Award!
Re: basic if statement
« Reply #2 on: 09 Nov 2018, 11:11 »
If you're wondering why it doesn't work - you're using the assignment (=) operator, when you should use the comparison (==) instead.

Code: Adventure Game Studio
  1. function oCar_Look()
  2. {
  3. if (caropen == false) player.Say("That's my car, it's locked");
  4. else player.Say("It's unlocked!");
  5. }

Also be sure if the bool value 'caropen' exists and is set.
« Last Edit: 09 Nov 2018, 11:15 by CrashPL »

Cassiebsg

  • Cavefish
  • Fleeing the Cylon tyrrany...
    • Cassiebsg worked on a game that was nominated for an AGS Award!
Re: basic if statement
« Reply #3 on: 09 Nov 2018, 11:13 »
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

  • slasher
    • I can help with AGS tutoring
    •  
    • Lifetime Achievement Award Winner
    •  
    • I can help with scripting
    •  
    • I can help with story design
    •  
    • Slasher worked on a game that was nominated for an AGS Award!
Re: basic if statement
« Reply #4 on: 09 Nov 2018, 11:14 »
Yes, double == to check if true/false...    Regardless of that... show us your if player has inventory script...

Re: basic if statement
« Reply #5 on: 09 Nov 2018, 11:25 »
Great thanks for your swift responses I will use the == and clean up my {}

 

Khris

  • having to deal with what games are going through
    • Lifetime Achievement Award Winner
    •  
    • I can help with play testing
    •  
    • I can help with scripting
    •  
    • I can help with translating
    •  
    • Khris worked on a game that was nominated for an AGS Award!
Re: basic if statement
« Reply #6 on: 09 Nov 2018, 12:08 »
Here's the shortest version:

Code: Adventure Game Studio
  1. function oCar_Look()
  2. {
  3.   if (caropen) player.Say("It's unlocked!");
  4.   else player.Say("That's my car, it's locked");
  5. }

CaptainD

    • I can help with play testing
    •  
    • I can help with story design
    •  
    • I can help with voice acting
    •  
    • CaptainD worked on a game that was nominated for an AGS Award!
      CaptainD worked on a game that won an AGS Award!
Re: basic if statement
« Reply #7 on: 09 Nov 2018, 12:23 »
Wow I never realised there were so many different ways to achieve the same thing!

Crimson Wizard

  • AGS Project Tracker Admins
    • Best Innovation Award Winner 2013, for spearheading the AGS 3.3.0 project
    •  
    • Lifetime Achievement Award Winner
    •  
    • Crimson Wizard worked on a game that was nominated for an AGS Award!
      Crimson Wizard worked on a game that won an AGS Award!
Re: basic if statement
« Reply #8 on: 09 Nov 2018, 12:54 »
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.
« Last Edit: 09 Nov 2018, 12:59 by Crimson Wizard »

Retro Wolf

  • Hello this is uhhh... Steve... from Microsoft...
    • Retro Wolf worked on a game that was nominated for an AGS Award!
Re: basic if statement
« Reply #9 on: 09 Nov 2018, 20:26 »
Very informative, thankyou very much CW!