2 change locations in same function?

Started by Jojowl80, Sun 12/07/2020 00:26:58

Previous topic - Next topic

Jojowl80

Code: ags



function hHotspot1_AnyClick()
{
  if (Lamp == true)
player.Walk(63, 38,  eBlock);
player.ChangeRoom(27, 140, 55); {
     {
  if (Lamp == false)
player.Walk(63, 38, eBlock);
player.ChangeRoom(21, 140, 55); {
    }
   }
  }
 }
   


but its saying I can't do the second location cause I already have the first location. any way around this?

Privateer Puddin'

Your IF statement / brackets aren't quite right


Code: ags

function hHotspot1_AnyClick() {
    if (Lamp == true) {
        player.Walk(63, 38, eBlock);
        player.ChangeRoom(27, 140, 55);
    } else {
        player.Walk(63, 38, eBlock);
        player.ChangeRoom(21, 140, 55);
    }
}   


We check if the lamp is true, otherwise we do something else



Jojowl80

thanks! everytime I try to put else statements I always get parser error's so I have been trying to avoid them.

Khris

That's because you've placed the brackets arbitrarily; with the exception of the function block ones, none of them has any effect.
Code: ags
  if (...)  // no brackets, so will only affect a single statement
    statement1;  // affected by if
  statement2;  // not affected by if, just regular statement
  else  // thus: parse error: else without if
    statement3;


Just follow this basic syntax:
Code: ags
  if (...) {  // open if-block
    statement1;
    statement2;
    statement3;
  }  // end of if-block
  else {  // works fine, since else is right after if-block
    ..
  } // end of else-block


(also: the player.Walk command is executed regardless of the variable's value so should be outside the if/else code.)

SMF spam blocked by CleanTalk