Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Mr Jake on Sat 07/08/2004 10:37:22

Title: Custom functions - request and help needed
Post by: Mr Jake on Sat 07/08/2004 10:37:22
so I have a custom function which is all fine and sound. But I have a problem with the script header.

If in the script header I put

#import displayline (int button)

As Jetxl told me too, I get the error 'unknown Preprocessor directive 'import'

If I put

#define displayline
or
#define displayline (button)
or
#define displayline (int button)

Which is how I thought it was done I get 'Parse error at '('' The line this is on reads ' displayline (2);'

Anyway, my Question.

How do I get it to work?

and my Request. Can custom functions be added to the manual please.
Title: Re: Custom functions - request and help needed
Post by: strazer on Sat 07/08/2004 11:07:18
Just do
import function displayline (int button);
Title: Re: Custom functions - request and help needed
Post by: Mr Jake on Sat 07/08/2004 11:10:27
I get
'Error (line 33): Already referenced name as import'

which is
'function displayline (int button)'
Title: Re: Custom functions - request and help needed
Post by: strazer on Sat 07/08/2004 11:15:02
Put the function in the global script, not the script header.
Title: Re: Custom functions - request and help needed
Post by: Mr Jake on Sat 07/08/2004 11:17:19
I have.
Title: Re: Custom functions - request and help needed
Post by: strazer on Sat 07/08/2004 11:20:38
And you have removed all
#import displayline (int button)
#define displayline
#define displayline (button)
#define displayline (int button)
?
Does the error message refer to line 33 in the global script or the script header?
Title: Re: Custom functions - request and help needed
Post by: Mr Jake on Sat 07/08/2004 11:21:38
Ive removed it all. and line 33 is in the global script.
Title: Re: Custom functions - request and help needed
Post by: strazer on Sat 07/08/2004 11:29:30
Strange, this works for me:

Global script

function displayline (int button) {
Display("test");
}

Script header

import function displayline (int button);

Make sure you don't have variables or defines named "displayline" or multiple declarations of the function or the import line.
Title: Re: Custom functions - request and help needed
Post by: Mr Jake on Sat 07/08/2004 11:31:27
yup, none of them.
Title: Re: Custom functions - request and help needed
Post by: strazer on Sat 07/08/2004 11:37:51
Hm, I'm not able to replicate your error message.
Can you post or pm the global script and script header for me to take a look?
Title: Re: Custom functions - request and help needed
Post by: Mr Jake on Sat 07/08/2004 12:03:39
Function in global script

function displayline (int button){
  if (button == 1){
    GetLocationName (savedx, savedy, name);
    GetObjectPropertyText (GetObjectAt (savedx, savedy), "Hand", display2);
    StrCat (display2, " ");
    StrCat (finish, display2);
    StrCat (finish, name);
    SetLabelText (0, 0, finish);
    }
  if (button == 2){
    GetLocationName (savedx, savedy, name);
    GetObjectPropertyText (GetObjectAt (savedx, savedy), "Eyes", display2);
    StrCat (display2, " ");
    StrCat (finish, display2);
    StrCat (finish, name);
    SetLabelText (0, 0, finish);
    }
  if (button == 3){
    GetLocationName (savedx, savedy, name);
    GetObjectPropertyText (GetObjectAt (savedx, savedy), "Mouth", display2);
    StrCat (display2, " ");
    StrCat (finish, display2);
    StrCat (finish, name);
    SetLabelText (0, 0, finish);
    }
 
  }


and
Script header:

import function displayline (int button);
Title: Re: Custom functions - request and help needed
Post by: strazer on Sat 07/08/2004 12:07:05
Looks ok at first glance.
I'm more interested in what is before and after the function and the import line.
There's obviously some conflicting line of code somewhere.
Title: Re: Custom functions - request and help needed
Post by: Mr Jake on Sat 07/08/2004 12:08:13
the end of the rep_exe section and the script header is empty.
Title: Re: Custom functions - request and help needed
Post by: strazer on Sat 07/08/2004 12:12:43
What does rep_ex have to do with anything? The displayline function should be seperate from every other function in the global script.

Please post everything before

function displayline (int button) {

from the global script.
Title: Re: Custom functions - request and help needed
Post by: Mr Jake on Sat 07/08/2004 12:14:52
/ main global script file
string name;
int savedy;
int savedx;
string display2;
string middle;
string finish;

#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() {
  // called when the game starts, before the first room is loaded
}
#sectionend game_start  // DO NOT EDIT OR REMOVE THIS LINE


#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() {
  // put anything you want to happen every game cycle here
 
  if (IsGUIOn (4) == 1){
    if (GetGUIObjectAt (mouse.x, mouse.y) == 0){
      displayline (2);
      }
    if (GetGUIObjectAt (mouse.x, mouse.y) == 1) {
      displayline (1);
      }
    if (GetGUIObjectAt (mouse.x, mouse.y) == 1) {
      displayline (3);
      }
}
#sectionend repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE

function displayline (int button){
  if (button == 1){
    GetLocationName (savedx, savedy, name);
    GetObjectPropertyText (GetObjectAt (savedx, savedy), "Hand", display2);
Title: Re: Custom functions - request and help needed
Post by: strazer on Sat 07/08/2004 12:28:19
The problem is you call the displayline function in rep_ex BEFORE you define it.
Always put custom functions at the top of the global script:

// main global script file
string name;
int savedy;
int savedx;
string display2;
string middle;
string finish;

// Put the displayline function here

#sectionstart game_start  // DO NOT EDIT OR REMOVE THIS LINE
function game_start() {
  // called when the game starts, before the first room is loaded
}
#sectionend game_start  // DO NOT EDIT OR REMOVE THIS LINE

#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute() {
  // put anything you want to happen every game cycle here

  if (IsGUIOn (4) == 1){
    if (GetGUIObjectAt (mouse.x, mouse.y) == 0){
      displayline (2);
      }
    if (GetGUIObjectAt (mouse.x, mouse.y) == 1) {
      displayline (1);
      }
    if (GetGUIObjectAt (mouse.x, mouse.y) == 1) {
      displayline (3);
      }
  } // was missing
}
#sectionend repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE

...
Title: Re: Custom functions - request and help needed
Post by: Mr Jake on Sat 07/08/2004 12:31:27
weee works now, thankies!
Title: Re: Custom functions - request and help needed
Post by: strazer on Sat 07/08/2004 12:34:37
No prob. :)
Title: Re: Custom functions - request and help needed
Post by: on Thu 04/11/2004 02:32:07
While we're at it: Does anybody know a work-around for this always having to define functions before they're called. I'm working on a strategy-adventure game in ags with several hundred user-defined functions, many of them calling each other (through GUI-buttons, repeatedly_execute or keypresses) in the sometimes rather twisty game-loop. This works fine, of course, but the code is a little hard to work with by now because of the "define-first - call-afterwards"-restriction. Being able to call all functions from anywhere (like classes and their methods in java) would be much of a relief, actually this was what I thought the "header" in ags was for. But importing functions in the header doesn't seem to make them accessible from any line in the global script - I can still only call them after they're defined.
Now I wont suggest this feature for implementation by CJ because it generally wont be needed in adventure games, but in this strategy-adventure mix-up of mine with many user-defined functions it sure would be nice: So does anyone know the trick?
Title: Re: Custom functions - request and help needed
Post by: strazer on Thu 04/11/2004 03:31:56
Something like this has been requested before and is on the tracker: http://www.adventuregamestudio.co.uk/tracker.php?action=detail&id=476