Script modules interacting with one another

Started by Erpy, Sun 22/11/2009 19:10:25

Previous topic - Next topic

Erpy

I'm currently fiddling around with a project that has its code divided among multiple script modules. There's two structs being used in the project and each struct has its own script module. Things kinda look like this:

Module with struct 1 header:

Code: ags
struct Apple
{
  int ID;
  import int Init(int index);
};

Apple Apples[10];


Module with struct 1 script:

Code: ags
function Apple::Init(int index)
{
  this.ID=index;
}


Module with struct 2 header:

Code: ags
struct Orange
{
  int ID;
  import int Init(int index);
};

Orange Oranges[10];


Module with struct 2 script:

Code: ags
function Orange::Init(int index)
{
  this.ID=index*2;
}


In the "room_load" section of the starting room, the following code is executed:

Code: ags
int i=0;
while (i<10)
{
  Apples[i].Init(i);
  Oranges[i].Init(i);
  i++;
}


Now there's a function I'd like to use that uses data from both structs looking something like this:

Code: ags
function Compare(int applenr, int orangenr)
{
  if (Apples[applenr].ID==Oranges[orangenr].ID) return true;
  else return false;
}


This function works fine in the room code, but when I tried putting it in a separate module (I wanted to create one specifically for all misc functions that use data from both structs), things malfunction. The misc-module is placed beneath the two struct-modules, so it can see the public variables of both structs just fine. However, the function when placed in the misc module always returns true and when looking at the values it retrieves, the only results were straight 0's for all ID-vars in both structs as if the init-process never took place. Any idea on how to fix this?


monkey0506

#1
Quote from: Erpy on Sun 22/11/2009 19:10:25Module with struct 1 header:

Code: ags
struct Apple
{
  int ID;
  import int Init(int index);
};

Apple Apples[10];


Module with struct 2 header:

Code: ags
struct Orange
{
  int ID;
  import int Init(int index);
};

Orange Oranges[10];

Please don't put variable definitions in a script header. What happens when you do:

Code: ags
Apple Apples[10];


In a script header is that for every single script a new array is created. Change it to be like:

Code: ags
// HEADER FILE (*.ash)
import Apple Apples[10];

// SCRIPT FILE (*.asc)
Apple Apples[10];
export Apples;


Okay now I'll read the rest of your post. :P

Edit: Okay now that I've read your post, the problem is what I described above. You need to import the arrays into the header files, then define and export them in the actual script file.

That should resolve your issue. ;)

Basically what was happening is like this. Think of each header as a template for every script that follows it. Every script file after this header will have everything in this header copied into it. So if you start every script with "Apple Apples[10];" you're just defining a new array each time. But if you put in every script "import Apple Apples[10];" then AGS knows to just use the existing array. For more information look up import and export in the manual. :)


SMF spam blocked by CleanTalk