Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Akumayo on Sun 12/06/2005 21:14:37

Title: Problem with functions [SOLVED]
Post by: Akumayo on Sun 12/06/2005 21:14:37
I have three functions currently, they are enemy_attack(); enemy_bite(); and enemy_constrict();

In my global scipt I have the following:

function enemy_attack() {
  if (GetGlobalInt(106)==1) {
    //You are fighting a sneak
    SetObjectGraphic(1, 0);
    if (GetGlobalInt(108)==1) {
    SetGlobalInt(70, 5);
    SetGlobalInt(71, 0);
    SetGlobalInt(72, 0);
    SetGlobalInt(73, 0);
    SetGlobalInt(74, 1);
    SetGlobalInt(75, 1);
    SetGlobalInt(76, 1);
    SetGlobalInt(108, 0);
  }
    SetGlobalInt(107, Random(1));
    if (GetGlobalInt(107)==0) {
      //Sneak will use bite
      enemy_bite();
      GUIOn(BATTLE);
    }
    if (GetGlobalInt(107)==1) {
      //Sneak will use constrict
      enemy_constrict();
      GUIOn(BATTLE);
    }
}

function enemy_bite() {
  SetObjectGraphic(2, 0);
  SetGlobalInt(105, (GetGlobalInt(1))-(GetGlobalInt(76)+1));
  SetGlobalInt(1, (GetGlobalInt(1))-(GetGlobalInt(76)+1));
  DisplayMessage(546);
  SetObjectGraphic(2, 0);
}

function enemy_constrict() {
  SetObjectGraphic(2, 0);
  SetGlobalInt(105, (GetGlobalInt(1))-(GetGlobalInt(76)+2));
  SetGlobalInt(1, (GetGlobalInt(1))-(GetGlobalInt(76)+2));
  DisplayMessage(546);
  SetObjectGraphic(2, 0);
}



And in my GUI script I call the enemy attack(); like so

enemy_attack();

I have already imported all the functions in the script header, but I get this message when trying to save the game:

function enemy_bite() {
  SetObjectGraphic(2, 0);
  SetGlobalInt(105, (GetGlobalInt(1))-(GetGlobalInt(76)+1));
  SetGlobalInt(1, (GetGlobalInt(1))-(GetGlobalInt(76)+1));
  DisplayMessage(546);
  SetObjectGraphic(2, 0);
}

Error (line 30): already referenced name as import; you must define it before using it



How do I fix this?
Title: Re: Error with functions
Post by: monkey0506 on Sun 12/06/2005 21:45:36
This would mean that you called the function before it was declared, i.e., the location in the script where the "enemy_attack()" line is written is physically located above the lines where enemy_attack() is declared (the function enemy_attack() is written).  If you can, just move the GUI script to AFTER the enemy_*() functions.
Title: Re: Error with functions
Post by: Akumayo on Sun 12/06/2005 21:47:39
The functions are located at the tip-top of the globabl script already, far before the GUI's even begin to emerge....
Title: Re: Error with functions
Post by: monkey0506 on Sun 12/06/2005 21:57:38
Perhaps you call it somewhere else than in the GUI's script?  And if so, where?

The only time I've gotten this type of error is when calling a global function in the global script before defining it...
Title: Re: Error with functions
Post by: Akumayo on Sun 12/06/2005 22:03:12
hey, you were right, I called it in enemy_attack();

but I defined eneymy_attack before defining the other two, thanks!