importing/exporting struct

Started by HandsFree, Thu 02/02/2012 14:12:22

Previous topic - Next topic

HandsFree

I found a topic with the exact same question but still I seem to be missing something.  >:(

I have:
// main global script file
struct Hint{
  bool done;
  String description;
};

Hint Hints[50];
export Hints;

I can work with the struct as long as it's from the global script, but not from the room scripts.
I tried putting "import Hint Hints[50];" in the room scripts and in the GlobalScript.ash, but it keeps saying "expected variable or function after import, not 'Hint'."

What am I doing wrong?

thanks

Gilbert

Put the struct definition:
Code: ags

struct Hint{
  bool done;
  String description;
};

in the script header instead of in the global script.

DoorKnobHandle

You put the struct definition ("Hint { boold one; String desc; };") into a header file (global header file for example, although I like to create a new pair of .asc source and .ash header files per struct. Then you put the array declaration ("Hint Hints[50]") into the beginning of the source file (main/global source file if you put the definition into the global header file). At the end of the source file you put the export line and back in the header file, underneath the struct definition you import ("import Hint Hints[50];").

That should do the trick, hope this helps!

HandsFree

I moved the struct definition and "import Hint Hints[50];" to GlobalScript.ash and now it works!

The export line is still here:
// main global script file
Hint Hints[50];
export Hints;

and not at the end of the file. Is that important?

thanks

DoorKnobHandle

To the best of my knowledge, it needs to be at the end of the file if you are modifying it in the file. If not, you should be good.

Gilbert

Quote from: HandsFree on Thu 02/02/2012 14:40:33
and not at the end of the file. Is that important?

As long as the "export" is under the declaration of the variable in the global script and that the import is under the struct definition in the header, it will work anyway. It's not really important, but it is advisable for one to be consistent on where he/she puts these stuff for better organisation of the codes.

monkey0506

I think at one point in the history of the AGS engine it was important for any variables or struct instances that were being used globally for the export to appear at the end of the script. If such a limitation did exist, it's definitely gone now. The only thing that matters is that export comes after the definition for variables/pointers/instances.

I actually recommend including exports very near to the definitions just to make it clear that they are meant to be global. Like Gilbot said though, be consistent.

For the record, import can also be used in script files, which has two specific uses:

1) You can export something and then import it only to the scripts that need it. Typically there's not a reason why you should ever want or need to do this, but it's possible.

2) You can import a function into the same script that defines it (before it is defined). The practical upshot of this is it allows you to supply default parameters to local functions. Presumably you could use this logic combined with #1 if you wanted semi-global functions that you wanted to supply default parameters to. The parameter list must be the same across all scripts, but you could potentially supply different defaults for different scripts. Again, not sure why you'd want to...but you could. ;)

Khris

I'm too lazy to try but does 2) cause the function to show up in auto-complete, too? Because iirc, local ones don't.

monkey0506

I was specifically referring to local functions, not member functions of a local struct...I'm actually not sure if importing a struct instance would cause autocomplete to pick up a local struct. Local functions do show up in autocomplete though, even without an import.

SMF spam blocked by CleanTalk