Another unresolved import - kinda SOLVED

Started by TM, Tue 05/04/2011 09:55:48

Previous topic - Next topic

TM

I hope someone can help me. Here's the situation:
I have Thing.ash that defines the struct Thing with a bunch of methods in it.
Then below that in the Scripts list, I have Instancer.asc which declares and exports a Thing-instance called mything. This is then imported in Instancer.ash.
This is all jolly. But GlobalScript.asc apparently can't find mything and I get an unresolved import error. I don't understand why this doesn't work.

My website & blog

Khris

The import line always goes into the header of the script where the instance is declared, not the struct itself.
Just move it to Instancer.ash and you're set.

Also, this makes it kinda pointless to have two different scripts in the first place; I'd recommend using a single one with struct definitions and imports in the header and global var declarations and functions in the main part.

TM

Quote from: Khris on Tue 05/04/2011 10:17:49
The import line always goes into the header of the script where the instance is declared, not the struct itself. Just move it to Instancer.ash and you're set.
Thanks for your reply! That's what I do, though...

Code: ags

//Thing.ash
  struct Thing {
    import function DoIt();
  };


Code: ags

//Instancer.asc
  Thing mything;
  export mything;


Code: ags

//Instancer.ash
  import Thing mything;


Code: ags

//GlobalScript.asc
  mything.DoIt();


The GlobalScript is what causes the error then.


Quote
Also, this makes it kinda pointless to have two different scripts in the first place; I'd recommend using a single one with struct definitions and imports in the header and global var declarations and functions in the main part.

Can I create a script that declares a struct in it's header and then instances the struct in the script body and then imports that instance in the header? That would indeed be simpler. I tried that and got another error so I thought no go?

My website & blog

TM

Hm, it seems that I got it to work the second way, struct definition and instance declaration in one script file. I don't get why it wouldn't work the other way, as that seems equally viable, but who am I to complain? :D Thanks again for your help, Khris!

My website & blog

monkey0506

I just tested this, just to be absolutely sure, and having a struct defined in one script (header), an instance defined in another script (body), and said instance imported into that script (header) works absolutely fine. You said you got an unresolved import error, but you didn't specify what the unresolved import was. Was the error for the mything instance, or was the error an unresolved import for Thing::DoIt^0?

Because the way you've described it (with the code snippets you've given), you haven't provided a definition for the Thing::DoIt method at all, which would result in an unresolved import when called.

From a purely technical standpoint, the import doesn't even have to be associated with the same script header as the script which defines what it is importing. The import doesn't have to appear in a script header. This has to do with how both script headers and the import keyword are handled.

Every script header gets copied into every script file (in the order which they are defined, and only the headers above this script file) when your game is compiled. That's why if you have a variable or struct instance definition in a script header it creates a new instance for every script. So when you put "import Thing mything;" into Instancer.ash, that line is copied into GlobalScript.asc, your room script files, dialog script files, and any other script files between Instancer and GlobalScript, all done when your game is compiled (the actual files themselves are not changed, only the compiled script does this). That's how script headers in AGS work.

The import keyword in AGS tells AGS that from this point onward in this script file, to use the already existing item (exported variable(s), exported struct instance(s), or function(s) (which don't have to be exported)) whenever we reference the item (in this script). If said item doesn't exist yet, the import will still work the same, but you can't use the item until it is actually defined. If you reference an imported item between the import and the definition of the item, you will get an "Already referenced name as import" error. An import statement is valid within a script header or script file, so long as an item with that name has not already been imported. If more than one item has been exported with the same name (or if more than one function exists with the same name), then the import keyword will only reference the first of these.

Putting these together, it doesn't matter where your import is placed, so long as you don't reference the imported item until it has been defined. If the import is in a script header then it will be automatically copied into every subsequent script, otherwise it won't.

If you ever get an "Unresolved import" error, then that means that you have imported an item, and then later referenced it, but it was never actually defined (or, in the case of variables or struct instances, never exported) (as I said, if it was defined and you reference it between the import and definition it would throw an "Already referenced name as import" error).

So since your mything instance was definitely defined, and definitely exported, an unresolved import error is not an error related to the mything instance. As I said, it's more likely that you forgot to define the Thing::DoIt method.

Khris

Yeah, sorry, I wasn't thinking clearly. A struct declaration can of course be used in all scripts below it.

Please always post the exact error message you get though; it makes things much easier for us.

SMF spam blocked by CleanTalk