parser errors with using inventory items [resolved]

Started by Sirpunchula, Sat 17/10/2009 23:21:22

Previous topic - Next topic

Sirpunchula

I'm trying to make an object respond when I use an inventory item on it using this code:

Code: ags

function camera_UseInv()
{
if(cEgo.ActiveInventory = iblend;){

Display("Yes, it blends!");
}


My problem is that I keep having parser errors when i stick my parentheses to "cEgo", but when I remove them it asks me to place parenthesis on that line.
Is there some way i can fix this? I've scoured the manual but I can't seem to find the information I need.
Pardon my deadpan.

tzachs

Two problems here:
1. No semicolon inside a condition check.
2. A condition check should be with a '==' and not a '='.
Here's the code:

Code: ags

function camera_UseInv()
{
  if(cEgo.ActiveInventory == iblend)
  {
     Display("Yes, it blends!");
  }
}

RickJ

You need to delete the semicolon from this line:

     "if(cEgo.ActiveInventory = iblend;){"

and you also need to add another curly brace like this:

Code: ags

function camera_UseInv() {
   if(cEgo.ActiveInventory==iblend) {
      Display("Yes, it blends!");
   }
}


You should also learn how to indent your code to keep from having mismatched curly braces "{}".  Whenever you put an opening curly brace  "{" you are saying to the compiler here begins a block of code that goes with the preceding if (also else, while, and function) statement.   The compiler will know that whatever is in that block of code needs to be executed when the if statement is true.   You need to put a closing curly brace "}" to tell the compiler that the code block is ended.   

The compiler can count the braces and keep track of them much better than we feeble minded humans.  So by convention we humans indent any code that is supposed to go between the curly braces by a tab character or 2-3 space characters.  That way we don't get confused.  It's also a good idea to write the closing curly brace "}" whenever we first write the opening curly brace "{".  Please note that the opening brace can be placed on the next line after the if statement and at the same indent level or it can be placed at the end of the if statement.  Bioth ways are equally valid;  just pick whichever you like and be consistent

Hope this helps

SMF spam blocked by CleanTalk