Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: rickious on Tue 01/10/2013 21:25:29

Title: Multiple 'if' with 'ChangeRoom' [SOLVED]
Post by: rickious on Tue 01/10/2013 21:25:29
I am using a multiple 'if' 'Bool' on a room transition.

Your in a cell, you can look at the sink to open a 1st person view room of the sink.  You can put the plug in and fill the sink, I have used 3 separate rooms for this, and when you put the plug in, it triggers the first 'Bool', fill the sink, the ssecond 'bool'....

The bools are so that if the player leaves the sink view and returns, they go to the correct view of the sink.

When I exit the sink at the plugin or filled point, the game crashes when I try to view it again...

'NewRoom: Cannot run this command, since there is a NewRoom command already queued to run'

Code (ags) Select
{
  if (PlugIn==true)
  {
    cEgo.Walk(575, 650, eBlock, eWalkableAreas);
    cEgo.ChangeRoom(8, 0, 0);
  }
  if (SinkFilled==true)
  {
    cEgo.Walk(575, 650, eBlock, eWalkableAreas);
    cEgo.ChangeRoom(9, 0, 0);
  }
  else
  {
    cEgo.Walk(575, 650, eBlock, eWalkableAreas);
    cEgo.ChangeRoom(2, 0, 0);
  }
}


Any ideas? Seems like its trying to run two ChangeRoom commands at once.

I have 2 bools setup for this in Global Variables.
PlugIn   bool   false
SinkFilled   bool   false
Title: Re: Multiple 'if' with 'ChangeRoom'
Post by: Crimson Wizard on Tue 01/10/2013 21:38:42
Yes, your code allows to run two ChangeRoom commands in a sequence.
First is run if PlugIn = true, and then the second runs if SinkFilled is either true or false.

To deal with situations like this you need to exit the function prematurely. This is what "return" command for ().

Code (ags) Select

{
  if (PlugIn==true)
  {
    cEgo.Walk(575, 650, eBlock, eWalkableAreas);
    cEgo.ChangeRoom(8, 0, 0);
    return; // this terminates function and returns back to where it was called
  }
  <...>
}


Another way is to use "else if" structure, in which case only one of the blocks can be processed at a time:
Code (ags) Select

{
  if (PlugIn==true)
  {
    cEgo.Walk(575, 650, eBlock, eWalkableAreas);
    cEgo.ChangeRoom(8, 0, 0);
  }
  else if (SinkFilled==true)
  {
    cEgo.Walk(575, 650, eBlock, eWalkableAreas);
    cEgo.ChangeRoom(9, 0, 0);
  }
  else
  {
    cEgo.Walk(575, 650, eBlock, eWalkableAreas);
    cEgo.ChangeRoom(2, 0, 0);
  }
}

I actually think that the second solution is better in this particular case, because you do not have anything else in this function.
Title: Re: Multiple 'if' with 'ChangeRoom' [SOLVED]
Post by: rickious on Tue 01/10/2013 21:55:48
Excellent.  Thanks mate... again :-D
Title: Re: Multiple 'if' with 'ChangeRoom' [SOLVED]
Post by: rickious on Tue 01/10/2013 22:21:26
Hmm, Almost solved.

PlugIn works fine, when you go to room 8, it runs...
Code (ags) Select
SinkFilled = true;
but when I leave the SinkFilled view and go back, it goes back to the PlugIn view... :-\
Title: Re: Multiple 'if' with 'ChangeRoom' [SOLVED]
Post by: rickious on Tue 01/10/2013 22:31:01
Ah sorted it now.  Had to add 'PlugIn = false' when entering the SinkFilled room.