Export array combined with struct? [SOLVED]

Started by Atelier, Tue 21/06/2011 17:47:03

Previous topic - Next topic

Atelier

Hi there I made a quest system, and it works. However, all the code is in a separate script other than the globalscript.
So how can I export the struct and array I made so it is accessible to my global script and rooms?

Here's the array and struct..

Code: ags

//--------------- QuestScript.ash

struct Quests {
    String name;
    int progress;
    String description;
    int steps;
};


//------------ QuestScript.asc

Quests quest[Max_Quests];


//define stuff in game start

quest[1].name = "First Quest";
quest[2].steps = 4;                      //etc



So I can do something like this in room_OnCall:

Code: ags

if (quest[1].progress == 1) //do stuff


Apart from that, I'm not altogether sure how export and import really work. It appears I can put my struct in the .ash or .asc and it doesn't really matter. Thanks.

Khris

Struct definitions always go into the header, so go the import lines.
Then the struct body and imported variables are available to all subsequent and all room scripts.
(You can put them in the .asc file, but then they're only available to that script.)

You need to export all data types, not functions though, if you want to import them i.e. access them elsewhere, ideally after the declaration:

Code: ags
Quests quest[Max_Quests];
export quest;                  // add this, just the name is enough with export


Then import it in the header

Code: ags
// QuestScript.ash

import Quests quest[Max_Quests];   // just copypaste the declaration line and put import at the start



The reason for all this is actually quite simple, say you have two extra scripts, script1 and script2, in that order.
At compile time, script1 is put together like this:
script1.ash
script1.asc

script2 though will look like this:
script1.ash
script2.ash
script2.asc

The Global script:
script1.ash
script2.ash
Global.ash
Global.asc

And the room scripts:
script1.ash
script2.ash
Global.ash
roomX.asc

So basically, all the import lines from previous scripts end up on top of the script, and the global variables can thus be accessed.

Atelier

#2
Ah ok, that all makes sense now :)

I get an error on the import line

Code: ags

import Quests quest[Max_Quests];


Array size must be a constant value.
I think it has something to do with the way I declare Max_Quests?

Code: ags

#define Max_Quests 4


Edit Ok, put the definition in the ash. Now I really do understand ;) Thanks again Khris.

SMF spam blocked by CleanTalk