Import/Export question.

Started by Pax Animo, Thu 26/05/2022 16:25:00

Previous topic - Next topic

Pax Animo

Greetings,

I'll try my best to explain what i'm trying to do and what the problem i'm having is:

So, i'm trying to break up my global script into smaller scripts as it's getting a bit messy, i've made a new script and within it i've created a new function.

Code: ags
function death () {
  if (food[0].stock >0 && Timer.IsExpired(TenSec)) {
    food[0].stock -=population[2].citizens;
  }
  else if (food[0].stock <= 0 && population[2].citizens >0 && Timer.IsExpired(FiveSec)) {
    population[2].citizens--;
    finances[0].income -= finances[0].tax;
    cCitizensUpdate.SayBackground("-1[Citizen");
  }
}


I've imported the function into this scripts header:

Code: ags
//header

import function death();


The function is called from a room script which seems to be called with no problems, the issue i'm having is the function can't access the data types, such as:

Death.asc(6): Error (line 6): Undefined token 'food'

I've tried importing the structured array to the new script which didn't help, or at least i'm doing it wrong, i've looked over the manuel several times as well as similar posts on the forums but i can't get my head around it.

Cheers for any help.

PS: I'm now wondering if this is due to the "Food food[20]" been assigned it's values in the global script. :|
Misunderstood

Snarky

In this case, if you want to reference Food in other scripts, you should put the Food struct declaration in the module header. (In the header you should only put things that are part of the API: declarations of functions and data structure formats, and import statements for variables you wish to share. A struct declaration is a description of a data structure format.)

You should put the declaration of the food array in the module script (not in the header, since it's data). It needs to be in the script because you're referencing it in the death() function.

Now, if you're also referencing food in the global script, you need to export the array in the module script, and put an import statement in the module header. I think the format for this is something like:

Code: ags
//Module script
export food;

Code: ags
// Module header
import Food food[20];


(But I usually get the format for exporting/importing arrays wrong and have to look up the correct way, so don't blame me if it doesn't work without modification.) This will make the array visible to other scripts outside of the module.

However, if the only reason you're referencing food in the global script is to assign the initial values, it may be better to move this assignment to inside the module. For example, you could create an InitFood() function, and call that from the global script.

eri0o

You declare it in the script, then you export it since only methods are automatically exported, and then in the header you import.

SMF spam blocked by CleanTalk