Using "if" and "else" in scripts (Solved)

Started by weleavefossils, Sun 31/03/2013 13:37:01

Previous topic - Next topic

weleavefossils

First off, thanks in advance for the help. Just started to play within AGS over the last month and came across my first scripting question. I have been able to find the answers to most questions within the forum or manual, but seem to be stuck on this one.

Problem: when I enter this specific room, I want to run this if/else statement:

Code: AGS
if (oHallwayDoor1.Visible = true) {
  RemoveWalkableArea(2);
}
  else {
    RestoreWalkableArea(2);
}


Normally when i want a script ran when the room is loaded i place it within:

Code: AGS
function room_AfterFadeIn()
{
}


But I seem to get an unexpected "if" error message. I'm assuming this is a simple solution, but I seem to be missing it.

Thanks!

johanvepa

Perhaps the script is trying to remove the walkablearea every time you enter the room. If the walkablearea is already removed and you enter the room again, perhaps the game stalls on trying to remove the same walkablearea once again.

Have you tried using the room_Load function?
Have you clicked the room function initializer at the room editor screen?

Slasher

#2
Quoteif (oHallwayDoor1.Visible = true) {
  RemoveWalkableArea(2);
}
  else {
    RestoreWalkableArea(2);
}

To start with there appears to be a missing closing brace and the = should be == and it can be placed in the Room Load if required before Room Fade In.

Code: AGS

if (oHallwayDoor1.Visible == true) // Run event if oHallwayDoor1 is Visible
{
  RemoveWalkableArea(2);
}
  else 
{
  RestoreWalkableArea(2); // Run event if oHallwayDoor1 not visible
}
} // You missed this one unless you have not shown it.




Khris

If you did place the first snippet within { and } of the second one, that should work fine, except for the "=" instead of "==".

This is how it's supposed to look like:
Code: ags
function room_AfterFadeIn()
{
  if (oHallwayDoor1.Visible == true) {
    RemoveWalkableArea(2);
  }
  else {
    RestoreWalkableArea(2);
  }
}


Note that you can shorten this quite a bit:
Code: ags
function room_AfterFadeIn() {
  if (oHallwayDoor1.Visible) RemoveWalkableArea(2);
  else RestoreWalkableArea(2);
}

(Doing an "== true" check is redundant, since if x is true, then x == true is true, and if x is false, x == true is also false.
Also, a single command doesn't need to be put within { }.)

weleavefossils

Thanks Slasher and Khris. I definitely did miss the extra =, forgot about that one. Also i appreciate the simplified script, I'm still getting used to script writing in general.

Works perfectly now. Thanks again!

johanvepa

Oh, how embarassing. I thought I could be there with a solution. Better luck next time.

SMF spam blocked by CleanTalk