Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: ZacksQuest on Wed 15/06/2011 22:24:49

Title: Help with "Parser Error in the ActiveInventory"
Post by: ZacksQuest on Wed 15/06/2011 22:24:49
Okay, I'm doing a graphical remake of Zork I (not enough to talk about on the in production forums, only one screen and such...), but I'm stuck in a problem zone. I'm a beginner, and when I tried to make it so you could put a leaflet back in a mailbox, I realised I didn't know the least bit about making inventory items interact with characters. So, I've checked the Help index, and furthermore have been blatantly confused. So, I have two questions.

1: Can someone spot the "parser error" in this piece of code?

function hHotspot1_UseInv()
{
  if (cEgo.ActiveInventory = iLeaflet
cEgo.Walk(123, 121, eBlock, eWalkableAreas);
cEgo.LoseInventory(iLeaflet);
Display("You put back the leaflet and shut the door.");)
}

2: Can I make it so multiple inventory items can be used on a character using ONE function, or do I have to copy and paste "function hHotspot1_UseInv() {}" over and over again?
Title: Re: Help with "Parser Error in the ActiveInventory"
Post by: Matti on Wed 15/06/2011 22:34:05
Quote from: ZacksQuest on Wed 15/06/2011 22:24:49
1: Can someone spot the "parser error" in this piece of code?

function hHotspot1_UseInv()
{
 if (cEgo.ActiveInventory == iLeaflet)     // this caused the error
 {
   cEgo.Walk(123, 121, eBlock, eWalkableAreas);
   cEgo.LoseInventory(iLeaflet);
   Display("You put back the leaflet and shut the door.")
 }
}

EDIT:

Quote from: ZacksQuest on Wed 15/06/2011 22:24:49
2: Can I make it so multiple inventory items can be used on a character using ONE function, or do I have to copy and paste "function hHotspot1_UseInv() {}" over and over again?

Of course you can (copying the function wouldn't work anyway):


function hHotspot1_UseInv()
{
  if (cEgo.ActiveInventory == iLeaflet)
  {
  // stuff happens
  }
  if (cEgo.ActiveInventory == iWhatever)
  {
  // stuff happens
  }
  // etc.
}

Title: Re: Help with "Parser Error in the ActiveInventory"
Post by: Hernald on Wed 15/06/2011 22:37:33
function hHotspot1_UseInv() {
  if (cEgo.ActiveInventory == iLeaflet ) {
    cEgo.Walk(123, 121, eBlock, eWalkableAreas);
    cEgo.LoseInventory(iLeaflet);
    Display("You put back the leaflet and shut the door.")
    }
  }

Note the closed bracket and the opened curly bracket.

To use the same function for open inventory items use else if ()
Title: Re: Help with "Parser Error in the ActiveInventory"
Post by: ZacksQuest on Wed 15/06/2011 23:26:10
Hang on, I have to check if this works....
Title: Re: Help with "Parser Error in the ActiveInventory"
Post by: monkey0506 on Thu 16/06/2011 02:18:08
Some things to keep in mind:

= is the assignment operator:

int i = 0; // create a variable 'i' and set its value to 0
i = 5; // set variable 'i' to value 5


== is the logical equals operator (does left side have the same value as right side):

if (i == 5) { /* do something... */ }

Any time you have an opening parenthesis '(', curly brace '{', or bracket '[', you must have a matching closing symbol:

if (i == 5) // open and close parenthesis
{ // opening brace
 if (player.InventoryQuantity[4] == 3) // opening parenthesis, opening brace, closing brace, closing parenthesis
 { // opening brace
   // some code
 } // closing brace
} // closing brace


If you forget the closing symbol to match the opening one, you'll get an error like this.

One final thing is that any time you have a condition or loop, the curly braces are optional. If you don't include them, then only the very first statement after the condition or loop is used:

if (i == 5) i = 12; // if 'i' is 5, change it to 12
i++; // 'i' will ALWAYS be increased by 1 here


If you include the braces, then you can run multiple statements as part of the condition or loop:

if (i == 12)
{
  i = 0; // this is run if 'i' is 12
  player.Say("Goosnargh."); // this is ALSO run if 'i' is 12, but not otherwise
}


This is really a matter of basic programming principles, so you should check out the scripting tutorial in the manual. ;)

Welcome to the forums though!