Custom functions - request and help needed

Started by Mr Jake, Sat 07/08/2004 10:37:22

Previous topic - Next topic

Mr Jake

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.

strazer

Just do
import function displayline (int button);

Mr Jake

I get
'Error (line 33): Already referenced name as import'

which is
'function displayline (int button)'

strazer

Put the function in the global script, not the script header.

Mr Jake


strazer

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?

Mr Jake

Ive removed it all. and line 33 is in the global script.

strazer

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.

Mr Jake


strazer

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?

Mr Jake

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);

strazer

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.

Mr Jake

the end of the rep_exe section and the script header is empty.

strazer

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.

Mr Jake

/ 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);

strazer

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

...

Mr Jake



frederik

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?

strazer

#19
Something like this has been requested before and is on the tracker: http://www.adventuregamestudio.co.uk/tracker.php?action=detail&id=476

SMF spam blocked by CleanTalk