exporting function from global script [solved]

Started by HandsFree, Fri 21/02/2014 12:56:30

Previous topic - Next topic

HandsFree

Sorry, I never fully grasped the import/export thing.
Normally I just put everything I need in GlobalScript.ash, but at the moment I'm adding things to the KQ4 project that uses a lot of additional scripts...

I'm trying to make a function Scare_Unicorn() (in GlobalScript) available to roomscripts and the cUnicorn.Look function which is in a separate script.

In the GlobalScript there's:
function cUnicorn_Look()
{
unicorn_look_handler();
}

then there's char_messages.asc that holds the function unicorn_look_handler().
And in char_messages.ash there's:
import function unicorn_look_handler();

I've tried different combinations of export and import options but every time I get 'unresolved import'. Is this because GobalScripts.asc is always at the bottom of the list?
How do I do this?

thanks


geork

Hmm...generally you shouldn't have to export functions - export is just for variables afaik (including struct objects).

In AGS, functions can only access other functions which are 'above' them, although I'm not sure what the technical terminology is. So if function A is above function B, then B can call A, but not visa versa. This goes the same for script order - if script Y is above script Z, then functions in script Z can call any function in script Y, but functions in script Y cannot call any function in script Z. Any function in GlobalScript should be able to call any function in other scripts, therefore, since it is at the bottom.

I'm not really sure what you meant when you said:
Quote from: HandsFree on Fri 21/02/2014 12:56:30
I'm trying to make a function Scare_Unicorn() (in GlobalScript) available to roomscripts and the cUnicorn.Look function which is in a separate script.
Does that mean there are two functions in GlobalScript which call unicorn_look_handler();? Are you attempting to import the cUnicorn_Look() function into GlobalScript.ash? On which line is the 'unresolved import' message highlighted? Perhaps cUnicorn_Look() is attempting to call Scare_Unicorn() which is below it, or visa versa...

HandsFree

I made function Scare_Unicorn() in the global script (cause I assumed that would be the logical place for it) and I want to use it in another script char_messages.asc, that is above it because all scripts are above GlobalScript.asc. AND I want to use it in room scripts...

I have no experience with multiple scripts but I think a function from the global script has to be imported in room scripts to be used there.

Crimson Wizard

#3
Quote from: geork on Fri 21/02/2014 14:32:49
Hmm...generally you shouldn't have to export functions - export is just for variables afaik (including struct objects).
To put this in different way: all functions are "exported" automatically. That's why you don't need to use "export" keyword for them.
You should use "import" command in the header to make them visible to other scripts though.

Quote from: HandsFree on Fri 21/02/2014 14:53:30
I made function Scare_Unicorn() in the global script (cause I assumed that would be the logical place for it) and I want to use it in another script char_messages.asc, that is above it because all scripts are above GlobalScript.asc. AND I want to use it in room scripts...
You can only use function in the script where it is located and lower scripts.
Consider your script modules:

A
B
C <-- function here
D
GlobalScript
Room scripts

The function will be usable from C, D, GlobalScript and room scripts.

Khris

Since this was only implicitly stated yet: if you want to call Scare_Unicorn() from within char_messages.asc, Scare_Unicorn() has to be declared in a script above char_messages.asc.

If you want to know why, read on:
Every module's header is put on top of all scripts below it.
Room scripts are at the bottom of this order and get put every header on top of them at compile-time.
To make a function from one script (A) available to another script (B) (assuming the order is already correct), you need to import the function. You can either do this within script_B.asc, that way only script B can call the function. Or you import the function in script A's header (the usual way), because that way it can be used by any other script below A (due to A's header becoming part of them).
The first method is preferable when you want to keep a tidy auto-complete window for example or are concerned about dependencies.

So the distinction between header and script is only relevant if you have multiple scripts, because the header is the part that ends up on top of other scripts, too. In fact, headers only exist because without them you'd have to import a function in every single other script that's using it.

HandsFree

OK, I put the function in char_messages.asc and imported it in room scripts. Now it works.
thanks

SMF spam blocked by CleanTalk