Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Zephyr on Sat 28/12/2019 11:40:40

Title: Parse error in Global Script
Post by: Zephyr on Sat 28/12/2019 11:40:40
Hi again everyone!

So I've reached a point in my game where my main character shoots and kills another character (simply called cVictim), but I want the main character to interact differently with cVictim, depending upon whether she's alive or dead.  So I put in GlobalScript.ash:

Code (ags) Select
bool IsVictimDead = false;

and in GlobalScript.asc:

Code (agsfunction cVictim_Look()
{
      if (IsVictimDead == false)
(     
         cChristian.FaceDirection (eDirectionUp))
         cChristian.LockViewFrame (CHRISTIAN_NORMAL, 3, 1, eStopMoving);
         cChristian.SayAt (15, 191, 300, "It appears to be a female but it's making a horrible sound...");
         Wait (5);
         cChristian.SayAt (15, 191, 300, "There's something very wrong here!");
         Wait (5);
         cChristian.UnlockView (eKeepMoving);
         cChristian.LockView (1);
}
    else
(
         cChristian.Walk (150, 68, eBlock, eWalkableAreas);
         cChristian.FaceDirection (eDirectionUp);
         Wait (5);
         cChristian.ChangeRoom (64);
}
}
) Select


But when I try to run the game, I'm getting a parse error at the line " if (IsVictimDead == false)".
I just can't seem to work out where I'm going wrong.  Can anyone help, please?  Remember - simplified instructions only please!  :cheesy:

- Zephyr
Title: Re: Parse error in Global Script
Post by: eri0o on Sat 28/12/2019 11:58:40
I don't think you can define a variable in the header like you did. You can though go into the Global Variables panel and create one there.

I think you mistyped when writing the code inside the global script in your post (it's empty).
Title: Re: Parse error in Global Script
Post by: Crimson Wizard on Sat 28/12/2019 12:52:26
Quote from: eri0o on Sat 28/12/2019 11:58:40
I don't think you can define a variable in the header like you did. You can though go into the Global Variables panel and create one there.

You can declare a variable's import in the header, with the "import" keyword.

in GlobalScript.ash:
Code (ags) Select

import bool IsVictimDead;

^ this will tell every other script that such variable exists (somewhere), so they can use it.

in GlobalScript.asc:
Code (ags) Select

bool IsVictimDead = false;
export IsVictimDead;

^ this will actually create such variable, and also export it for use in other modules.

Global Variables panel simplifies this process, but in the end it automatically generates same code.
Title: Re: Parse error in Global Script
Post by: Khris on Sat 28/12/2019 13:03:43
You're getting a parse error because you're opening your if blocks with ( instead of {. The syntax is

Code (ags) Select
  if (condition) // parens for condition
  { // curly brace to open block of statements
    ...
  } // curly brace to close block


You also have a missing semi-colon near the start.

Unrelated but not less important: if you declare a variable in the header, you will create a separate one for each script, which is not what you want, usually.

Final note: given that the variable is a bool, you can do this:
Code (ags) Select
  if (IsVictimDead) {
    ...
  }
  else {
    ...
  }
Title: Re: Parse error in Global Script [SOLVED]
Post by: Zephyr on Sun 29/12/2019 10:57:44
Hi everyone,

Thanks again so much for your help. Sorry about the missing script - I must have mistyped as you said! 

@Crimson Wizard - Your suggestion about "import bool" is working perfectly, so many thanks for that.  I still get SO confused about what goes where and about the correct syntax!

@Khris - Yep! You got me there! It turns out that I'd mistakenly used ( instead of { in a couple of places.  Can't believe I made such a silly mistake (it just goes to show that scripting is NOT a good idea when you're tired!).

- Zephyr  :-[ :-*
x
Title: Re: Parse error in Global Script
Post by: Khris on Mon 30/12/2019 13:40:39
Btw, the code is in your post (that's how I knew what the parse error was) but you accidentally deleted the ] from the opening code tag.