Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: HandsFree on Sun 14/02/2021 11:30:04

Title: export problem (possibly)
Post by: HandsFree on Sun 14/02/2021 11:30:04
Hope I can make this clear.
We have a generic way of showing player deaths.

Code (ags) Select

in global script:
function ShowDeathGui(CauseOfDeath cod) { //makes death gui appear
//stuff
  DisplayAtY(280, death_message[cod]);

}

function game_start(){
  death_message[eDeathTree] = "test";

}

GlobalScript.ash
enum CauseOfDeath {
  eDeathZombie,
  eDeathTree,
};
String death_message[80];
export death_message; //added this but it didn't help

In room scripts:
ShowDeathGui(eDeathTree);


So when the player dies you see the text "test".
This works.

But for the tree I want to have several random messages, so in de room script I added:
Code (ags) Select

void RandomTreeMessage(){
  int r = Random(4);
  r = 0; //testing
  if (r==0) death_message[eDeathTree] = "This is the first random text";
}

And then
RandomTreeMessage()
ShowDeathGui(eDeathTree);

The result is that the text "test" is still shown, instead of "This is the first random text"
I assume this is some kind of import/export thing?
Or should this be a function with parameters?

How do I do this?
Thanks
Title: Re: export problem (possibly)
Post by: Crimson Wizard on Sun 14/02/2021 11:40:19
You are placing variable declaration and "export" in the header, while you should normally place them inside script body, and place "import" in the header instead.

Here's explaination on how to export/import variables and functions:
https://adventuregamestudio.github.io/ags-manual/ImportingFunctionsAndVariables.html
Also for more information on why you should not place regular variable declarations in the header:
https://adventuregamestudio.github.io/ags-manual/TheScriptHeader.html
Title: Re: export problem (possibly)
Post by: HandsFree on Sun 14/02/2021 12:08:21
I now have in global script:
String death_message[80]; //this is line 57

in ash:
import String death_message[];

When compiling I get error:
Attributes of identifier do not match prototype (on line 57)

If I change the import statement to import String death_message[80]; I get another error (unresolved import)
Title: Re: export problem (possibly)
Post by: Laura Hunt on Sun 14/02/2021 12:21:55
This looks weird to me:

Code (ags) Select
function ShowDeathGui(CauseOfDeath cod)

Shouldn't that be simply function ShowDeathGui(int cod)?
Title: Re: export problem (possibly)
Post by: HandsFree on Sun 14/02/2021 12:26:44
The cause of death is an enum, so I believe that part of the script is correct. At least it has always worked until I tried to set the message from a room script.
Title: Re: export problem (possibly)
Post by: Crimson Wizard on Sun 14/02/2021 12:49:20
Quote from: HandsFree on Sun 14/02/2021 12:08:21
If I change the import statement to import String death_message[80]; I get another error (unresolved import)

This means the variable was not exported.

You need an export statement for each variable you export from script. For example:
Code (ags) Select

String death_message[80];
export death_message;



"Import" declares that such variable exist somewhere else. "Export" actually links particular variable to its "import" declaration.
Title: Re: export problem (possibly)
Post by: HandsFree on Sun 14/02/2021 13:55:09
I added the export, but I still get 'Attributes of identifier do not match prototype'.
Title: Re: export problem (possibly)
Post by: Crimson Wizard on Sun 14/02/2021 14:10:00
Quote from: HandsFree on Sun 14/02/2021 13:55:09
I added the export, but I still get 'Attributes of identifier do not match prototype'.

What line of script does this error point to? Is the import declaration match the variable declaration?
Title: Re: export problem (possibly)
Post by: HandsFree on Sun 14/02/2021 14:27:21
In GlobalScript.asc
String death_message[80];// line 57
export death_message;

in GlobalScript.ash
import String death_message[];

The error points at line 57.

When I removed the [] in the import statement, the error is 'death_message is not an array'.
Title: Re: export problem (possibly)
Post by: Crimson Wizard on Sun 14/02/2021 14:37:45
Well import has to be declared exactly as the variable. I thought you already fixed this error before.
Title: Re: export problem (possibly)
Post by: HandsFree on Mon 15/02/2021 09:34:22
OK, ik works with import String death_message[80];
I didn't expect I had to write that 80 there in the import statement.

thanks