Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Jessiekah on Sun 14/11/2010 17:53:48

Title: "If" statement Syntax help requested
Post by: Jessiekah on Sun 14/11/2010 17:53:48
Hio all

I am a new user to AGS, i started about 1 week ago and have slowly made my way through the beinners tutorial throughout the past week. I finished the tutorial earlyer today and have gone onto reading everything inthe help section in order, i have just started on the "Scripting" section on "Scripting Tutorial Part 1".

I have followed the instructions and what its said and checked + re-checked, and also experimented with the code myself. However i can not figure out what is going wrong

Any help with this would be much appreciated. Details of the problem below

------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------
My hand written script
================
// room script file
int vDoor1Look ;

function hDoor1_Look()
//Displays messege when using the "look" tool on the specified hotspot

if (vDoor1Look == 0)
{
  Display("You see a door.") ;
}

if (vDoor1Look == 1)
{
  Display("On closer inspection you see a small keyhole on the large and omnouse door.") ;
}
if (vDoor1Look == 2)
{
  Display("You try to open the door only to find that it is locked.") ;
}
if (vDoor1Look == 3)
{
  Display("There is nothing left of interest") ;
}
if (vDoor1Look <3)
{
  vDoor1Look += 1 ;
}
----------------------------------------------------
Error Given By Consol On F5 Run Attempt
=============================
Failed to save room room1.crm; details below
room1.asc(4): Error (line 4): Expected '{'

------------------------------------------------------
Relevant sections given by Scripting tutorial
==============================
1.) "So, to declare a variable for use by one of the room interaction scripts, you need to place the definition above the main function body.  So, it should look something like this:
// room script file
int myCounter;

(other event scripts)

function hDoor_Look()
{
  Display("It's quite a large, ominous looking door.");   
}     

(rest of file follows)"

2.) "You can add to and subtract from a variable using the += and -= operators. So, to add 3 to the current value of myCounter, do the following:

  myCounter += 3;"

3.) " if (myCounter == 0)
  {
    Display("You see a bookshelf.");
  }
  if (myCounter == 1)
  {
    Display("Looking closer, you see a book called Hamlet."); 
  }
  if (myCounter == 2)
  {
    Display("There is also a book called Harry Potter.");
  }
  if (myCounter == 3)
  {
    Display("There is nothing else of interest on the shelf."); 
  }
  if (myCounter < 3)
  {
    myCounter += 1;
  }


myCounter starts off set to 0, so the first time this script is called it will run the first Display command, but not the others. Then, since 0 is less than 3, it will increase myCounter by 1, and since 0+1 = 1 it now holds the value 1.
Once the player has seen all the messages (myCounter == 3), it no longer increases the value so if they click again they will keep getting the final message."
------------------------------------------------------------------------------------------------------------------------------------------------------
===================================================================================

As previously stated i HAVE read all the relevant sections in the tutorial but i cannot figure out what is wrong with my syntax. I'm thinking im missing something due to the layout that the tutorial presents the code. This post is lengthy, but i pasted the relevant sections to save time for those who want to read the tutorial i am doing without having to go load it up themselves.

Thanks very much in advance

Jess
Title: Re: "If" statement Syntax help requested
Post by: mode7 on Sun 14/11/2010 18:10:46
You're missing a {

You wrote:
function hDoor1_Look()

You should write
function hDoor1_Look() {

Also don't forget to add a } at the end

the brackets you put are only meant for the ifs but you also need brackets for functions
Title: Re: "If" statement Syntax help requested
Post by: Matti on Sun 14/11/2010 18:11:40
All the code needs to be within the hDoor1_look function, so you forgot two brackets. They should be automatically added if you're properly linking the function via the lightningbolt and [...] - buttons on the panel.


function hDoor1_Look()
{   <--
if (vDoor1Look == 0)
{
 Display("You see a door.") ;
}
if (vDoor1Look == 1)
{
 Display("On closer inspection you see a small keyhole on the large and omnouse door.") ;
}
if (vDoor1Look == 2)
{
 Display("You try to open the door only to find that it is locked.") ;
}
if (vDoor1Look == 3)
{
 Display("There is nothing left of interest") ;
}
if (vDoor1Look <3)
{
 vDoor1Look += 1 ;
}
}   <--


Also, this piece of code could be significantly shorter if you write it like this:


function hDoor1_Look()
{
if (vDoor1Look == 0) Display("You see a door.") ;
if (vDoor1Look == 1) Display("On closer inspection you see a small keyhole on the large and omnouse door.");
if (vDoor1Look == 2) Display("You try to open the door only to find that it is locked.");
if (vDoor1Look == 3) Display("There is nothing left of interest");
if (vDoor1Look <3) vDoor1Look += 1;
}


You don't need the brackets when you're just using one line of code.

Also, in the future, use the code-tags when you're posting your script, it makes everything much more readable.
Title: Re: "If" statement Syntax help requested
Post by: Jessiekah on Sun 14/11/2010 18:14:55
Ah ha! Thank you very much, just tested and works perfectly, Much appreciated and thanks very much for the swift reply =]

I re-checked the tutorial as soon as you said to see if i had missed this coz its not something i would usually miss... and after rechecking it does not state that you need the extra pair... Iether that or it deffinatly does not state it clearly

Thanks again ^.^

Jess
Title: Re: "If" statement Syntax help requested
Post by: Khris on Mon 15/11/2010 04:31:58
First of all, a function body obviously needs something that denotes its start and end.

Secondly, if you add an event's function by clicking the ellipses button, AGS adds this to the relevant script:
function Name()
{

}

This is not an "extra pair".

Finally, to quote from the relevant section of the tutorial:
QuoteThe "function hDoor_Look" line defines that the script underneath will be run when this event occurs. In AGS, the curly brackets { and } are used to mark the start and end of a block of script. Anything that you type in between them will be run as part of this event.

Oh and, welcome to the forums :)