Problems with if scripts Again....

Started by Jakerpot, Thu 08/01/2009 17:40:57

Previous topic - Next topic

Jakerpot

Code: ags
Hi again script experts! I wanted to write a script that say; If background is frame 1, then remove tint from object1.
I writed then:
[Code]
function Rope_Interact() 
{
  light_is_on = 1 - light_is_on;  // toggle light
  SetBackgroundFrame(light_is_on);    // update background
     if (int.light_is_on == 1);
  oPurplepaint.RemoveTint;
  
  }


The room start w lights off, when interact to rope, its turn on, changing background frame. So i tint the object (oPurplepaint) when the lights are off to look more dark. But when i create a condition to remove the tint when background frame is 1 (lights on) an error pop up, saying that:

Failed to save room room2.crm; details below
room2.asc(34): Error (line 34): '.light_is_on' is not a public member of 'int'. Are you sure you spelt it correctly (remember, capital letters are important)?

Can you help me? light_is_on is a variable but i cant use it there! Thanks.[/code]



Ghost

#1
It's a matter of style, but for simple two-state switches I prefer booleans. What's the point of light_is_on = 1 - light_is_on? With a bool you'll get much more readable code:

if (light_is_on) {
   ---
} else {
   ---
}

Where did you define int light_is_on? In your global script? Then you need to IMPORT it in the script header, or (also possible) make it a Global Variable. Syntax, too, is important,
you...

Ah, CJ beat me to it.

Pumaman

You're very close:

Quote
function Rope_Interact()
{
 light_is_on = 1 - light_is_on;  // toggle light
 SetBackgroundFrame(light_is_on);    // update background
 if (light_is_on == 1)
   oPurplepaint.RemoveTint();
}

ie. remove the "int." and the semicolon from that line.

Vince Twelve

In general, if statements work like this:

One command to be run if the expression in the parenthesis is true:
Code: ags

if(light_is_on==1) //put command to be run here followed by semicolon


More than one command to be run if the expression in the parenthesis is true
Code: ags

if(light_is_on==1){
  //command to be run followed by semicolon
  //another command to be run by semicolon
}


So, if you have only one command, it can go on the same line as the if statement.  If you have more than one, you need to put {} around them.

Make sure that the expression to be checked in the if statement uses "==" instead of "=".  "==" compares the two parts to see if they're equal.  "=" sets the left side to be equal to the right and will produce a "parse error" if used in an if statement.

Also, notice that the "if(...)" part is never followed by a semicolon.  But each command to run inside the statement (or on the same line in the one command example) must be followed by a semicolon.

More complicated "if" statements involve "else" or "else if" lines.

Code: ags


int variableName=3;

if(variableName<=0){
  //do this
}
else if(variableName>=10){
  //do that
}
else{
  //do something else
}


since variableName is not less than or equal to 0, it will skip the first part.  Since it is not greater than or equal to 10, it will skip the second part, then it will do run the third block of commands (where it says "do something else") instead.  You do not need to use "else if" if you have only two results, or you can use an unlimited number of "else if"s to add as many results as you want.

I hope that gets you 100% up to speed on if statements.

Keep reading that Scripting tutorial to help you further.


Jakerpot

thanks very much folks! This really helped me! Now i will do the same for the other object in the room, and i added a else case to the function, so if the lights are off, the tint is applied, i used this in the first enter room event, but then, when yopu turn the lights off again, the tint is not applied, so then i applied the tint to an else case. Thanks! Now i can solve most of my scripting problems! Thanks again.  ;)



Khris

Just want to add a tidbit:
The compiler doesn't care about line breaks, so if you want to run just one command depending on whether a condition is true, it doesn't matter if the command is in the same line or not. Sometimes readability even "demands" putting it in the next line. (Still, you mustn't put a semicolon anywhere except the end of said command.)

Also note that in your original code you've indented the if line but not the RemoveTint command although proper indentation is doing it exactly the other way around.

SMF spam blocked by CleanTalk