Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: weleavefossils on Sun 31/03/2013 13:37:01

Title: Using "if" and "else" in scripts (Solved)
Post by: weleavefossils on Sun 31/03/2013 13:37:01
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) Select
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) Select
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!
Title: Re: Using "if" and "else" in scripts
Post by: johanvepa on Sun 31/03/2013 13:46:30
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?
Title: Re: Using "if" and "else" in scripts
Post by: Slasher on Sun 31/03/2013 13:57:23
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) Select

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.



Title: Re: Using "if" and "else" in scripts
Post by: Khris on Sun 31/03/2013 13:58:52
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) Select
function room_AfterFadeIn()
{
  if (oHallwayDoor1.Visible == true) {
    RemoveWalkableArea(2);
  }
  else {
    RestoreWalkableArea(2);
  }
}


Note that you can shorten this quite a bit:
Code (ags) Select
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 { }.)
Title: Re: Using "if" and "else" in scripts
Post by: weleavefossils on Sun 31/03/2013 14:08:06
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!
Title: Re: Using "if" and "else" in scripts (Solved)
Post by: johanvepa on Sun 31/03/2013 14:24:13
Oh, how embarassing. I thought I could be there with a solution. Better luck next time.