Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: grandfatherparadox on Sun 26/04/2009 19:27:23

Title: Problem with an 'if' statement- character repeats same line (SOLVED)
Post by: grandfatherparadox on Sun 26/04/2009 19:27:23
I've not been using AGS long so forgive me if this is a naive question... I have searched high and low for the answer but can't find it so here goes:

I want to program the game so that when my player leaves the room, one of two outcomes will occur depending on whether he has visited another room. Simple enough, and the script I wrote looks like this:

function room_LeaveBottom()
{
if(HasPlayerBeenInRoom(4) ==0)

cChar1.Say("I should check my emails first.");
cChar1.Walk(69,  132);

if(HasPlayerBeenInRoom(4) ==1)

  cChar1.ChangeRoom(1,  1000);
}

So if the player has visited room 4, he will change room, if he hasn't, it will display the dialog and move him to another location in the same room. Only problem being that when I run the game, he keeps repeating the same line of dialog over and over unto infinity. Why is this happening and what do I need to change? I'm aware that this is probably a really simple question but I'd really appreciate some help!

Thank you.
Title: Re: Problem with an 'if' statement- character repeats same line over and over.
Post by: OneDollar on Sun 26/04/2009 19:41:51
You need to enclose your if functions with parenthesis "{" and "}". Also because you're dealing with an 'either-or' situation (either the player will have visited room 4, or they won't have) you can replace the second if with an "else". Finally you may or may not need to make the walk blocking...

function room_LeaveBottom()
{
  if(HasPlayerBeenInRoom(4) ==0) {
    cChar1.Say("I should check my emails first.");
    cChar1.Walk(69, 132, eBlock);
  }else{
    cChar1.ChangeRoom(1,  1000);
  }
}
Title: Re: Problem with an 'if' statement- character repeats same line over and over.
Post by: grandfatherparadox on Sun 26/04/2009 19:48:29
Thank you for your help and the new script works fine, but the same problem persists; the character walks to the edge of the screen but just keeps saying the same line over and over (and doesn't move). If HasPlayerBeenInRoom IS 1 then he enters the next room no problem, it's just when it's 0 that this happens. All I want is a single line of dialog. What else could be going wrong?
Title: Re: Problem with an 'if' statement- character repeats same line over and over.
Post by: grandfatherparadox on Sun 26/04/2009 19:50:11
The eBlock made the difference :) Thank you so much!
Title: Re: Problem with an 'if' statement- character repeats same line (SOLVED)
Post by: monkey0506 on Mon 27/04/2009 00:45:41
Just to clarify the reason your code wasn't working:

if (HasPlayerBeenInRoom(4) == 0) cChar1.Say("I should check my emails first.");
cChar1.Walk(69, 132);


if (HasPlayerBeenInRoom(4) == 0)
  cChars1.Say("I should check my emails first.");
  cChars1.Walk(69, 132);


AND

if (HasPlayerBeenInRoom(4) == 0) {
  cChars1.Say("I should check my emails first.");
}
cChars.Walk(69, 132);


Are all equivalent. The braces associated with an if-statement are optional, however if omitted, it is implicit that there are braces around the very next statement. If you want to execute multiple statements conditionally you must enclose them all within braces (explicitly). The same principle applies to else-statements. So essentially we can even gather that an else if-statement:

else if (HasPlayerBeenInRoom(4) == 1) cChar1.ChangeRoom(1, 1000);

Is really just the same as:

else {
  if (HasPlayerBeenInRoom(4) == 1) {
    cChar1.ChangeRoom(1, 1000);
  }
}


Clearly the first is less complicated, but it's always important to check your braces! ;)
Title: Re: Problem with an 'if' statement- character repeats same line (SOLVED)
Post by: Trent R on Mon 27/04/2009 03:34:28
Quote from: Pumaman on Wed 03/12/2008 20:16:59
Be careful of this one, if you use it in the current room it will always return true.
Don't know if this is relevant to you...

~Trent