Problem with an 'if' statement- character repeats same line (SOLVED)

Started by grandfatherparadox, Sun 26/04/2009 19:27:23

Previous topic - Next topic

grandfatherparadox

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.

OneDollar

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...
Code: ags

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);
  }
}

grandfatherparadox

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?


monkey0506

Just to clarify the reason your code wasn't working:

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


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


AND

Code: ags
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:

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


Is really just the same as:

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


Clearly the first is less complicated, but it's always important to check your braces! ;)

Trent R

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
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

SMF spam blocked by CleanTalk