Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: cliffwhite on Wed 06/05/2020 20:00:52

Title: Having problems getting started with IF Statement
Post by: cliffwhite on Wed 06/05/2020 20:00:52
I have created two rooms and I want to move from Room 1 to Room 2. I used the default room names and I've set a Global Variable called "axe" to the value of 0. I have the following code in the Room 1 script,

Code (ags) Select

// room script file
if (axe == 0){
player.ChangeRoom(2);
}


When I try to run the game, I get the following error messages.

Failed to save room room1.crm; details below
room1.asc(2): Error (line 2): Parse error: unexpected 'if'

I'm not sure what I'm doing wrong. I can't add any IF statement without getting the error.
Title: Re: Having problems getting started with IF Statement
Post by: Crimson Wizard on Wed 06/05/2020 20:03:49
It's not about "if" statement, it looks like you put your code outside of any functions. In AGS all the commands should be inside functions.

Functions themselves are run by game events, or from other functions in script.

So first of all, you need to determine at what event should this code run, and find or make appropriate function for it.
Title: Re: Having problems getting started with IF Statement
Post by: Khris on Wed 06/05/2020 21:07:05
Even if that had worked as intended, doesn't this code move the player straight to room #2, given that axe is initialized to 0?

Please tell us how your game is supposed to work; at which point is the player supposed to move from room 1 to room 2?
Title: Re: Having problems getting started with IF Statement
Post by: cliffwhite on Thu 07/05/2020 01:31:42
I realize the code (if it worked) would move the character straight to room 2. I was just trying to display that I couldn't get the IF statement to work at all no matter how simple it was. I'll try Crimson Wizards suggestion to create a function and see if I can get that to work. Thanks for the assistance!!!!
Title: Re: Having problems getting started with IF Statement
Post by: Lewis on Thu 07/05/2020 07:01:57
You always need something to 'trigger' a script to fire. You need to establish the conditions under which, if axe equals zero, the player changes rooms. Your if statement asks the game to check something - while the function you nest that statement within tells the game when to check it. That, in simple terms, is what your functions are for.

So let's say you want the player to change rooms when they click a hotspot, but only if axe is 0. You would bring up that hotspot's event table and select 'player interacts with hotspot' to add the relevant function to your room script, then add your script inside that function:

Code (ags) Select
hotspot1_interact()
{
  if (axe == 0)
  {
    player.ChangeRoom(2)
  }
}


If you want the game to be constantly checking an if statement, and firing a script if it is true, then you need to put that script in releatedly_execute.

If you want this only to be the case when the player is in the current room, then in your room's events table, click repeatedly execute to add this function to your room script, then input exactly the script you wrote in your OP within it:

Code (ags) Select
function repeatedly_execute()
{
  if (axe == 0)
  {
    player.ChangeRoom(2);
  }
}


If you wanted the game to constantly check throughout the game, you would instead add this to repeatedly_execute in the global script (and for best practice, check the player isn't already in the right room when this fires):

Code (ags) Select
function repeatedly_execute()
{
  if ((player.Room != 2) && (axe == 0)) //checks player isn't already in room 2 and that axe==0
  {
    player.ChangeRoom(2);
  }
}