-
Notifications
You must be signed in to change notification settings - Fork 9
ImportingFunctionsAndVariables
Let's say you have a function or a variable in one script, but would like to use them in another. Sharing functions and variables between scripts is known as import/export in AGS. You export them from the script where they are defined, and let other scripts use these by declaring an import. An import should be declared where a script can "see" it: usually the most convenient place is a header of a script which does the export.
You also need to remember that currently in AGS a script may only see things declared in its own header and in any header above in the list of scripts.
NOTE: Room and Dialog scripts can see declarations from all the regular script headers, but not from other rooms or dialogs.
All script functions are exported automatically, so they only need an import declaration to let other scripts know that they exist. This is done by declaring a function with an import
keyword. The best practice is to place them in the owner script's header.
For example, suppose you have the following function in script MyScript.asc:
function ScriptAFunction(int param1, int param2)
{
// some actions here
}
Then the import declaration in the MyScript's header (MyScript.ash) should looks like:
import function ScriptAFunction(int param1, int param2);
The name, type and arguments of the function must be the same, otherwise there will be errors either during compilation or at runtime.
Script variables are not exported by default, so that has to be done explicitly inside the script which has them declared, using the export
keyword.
Let's suppose that you have the following variable inside MyScript.asc:
int public_variable;
If you want to share this variable with the other scripts, you have put the export
statement inside the same script where you have declared the variable (MyScript.asc), somewhere below the declaration:
export public_variable;
The export
statement needs only the variable's name and nothing else. It must not be put inside a function.
You can export several variables in one statement if you separate their names with commas, for example:
export public_variable1, public_variable2;
Now, import declaration in the MyScript's header (MyScript.ash) should looks like:
import int public_variable;
Both the name and type must match the variable's own declaration exactly.
This bit is important. If the variable is int
then it should be imported as int
, if it's of the type Character*
then the import should also be declared as Character*
.
Imported arrays must have the correct size, for example if you have int arr[10];
then the import declaration must be:
import int arr[10];
(NOTE: the export command still requires its name only: export arr;
)
Similar to the regular variable declaration, you can declare the import of several variables of the same type in one statement if you separate them by commas:
import int var1, var2;
NOTE: Most commonly import declarations are put in the header of the same script where these variables are defined. But in theory you may not do that and instead place imports inside each particular script where you intend to use these variables.
So, for a final example, you have this in MyScript.asc:
int public_int1, public_int2;
int arr[10];
String someString;
Character* RandomChar;
export public_int1, public_int2, arr, someString, RandomChar;
And in MyScript.ash:
import int public_int1, public_int2;
import int arr[10];
import String public_str;
import Character* RandomChar;
To sum this up, the import declaration lets other scripts see this variable, and the export command actually connects existing variables to that declaration.
NOTE: The AGS Editor has a special menu called "Global Variables" meant for easier creation of public variables which could are accessible everywhere. This may be a better alternative for beginners, as it takes care of all this import/export managing behind the scenes.
See also: import keyword, export keyword
Getting Started in AGS
Editor
- New Game templates
- Editor Preferences
- General Settings
- Default Setup
- Colours Editor
- Room Editor
- Character Editor
- Cursor Editor
- Dialog Editor
- Font Preview
- GUI Editor
- Inventory Items Editor
- View Editor
- Sprite Manager
- Music and sound
- Voice speech
- Script Modules
- System limits
- Log Panel
- Plugins
- Other Features
Engine
Scripting
- Scripting Tutorial
- Scripting Language
-
Scripting API
- Script API Overview
- Standard Constants
- Standard Enumerated Types
- Standard Types
- Game variables
- Global arrays
- Global event handlers
- repeatedly_execute / repeatedly_execute_always
- Custom dialog options rendering
- Global functions: general
- Global functions: message display
- Global functions: multimedia actions
- Global functions: palette operations
- Global functions: room actions
- Global functions: screen effects
- Global functions: wait
- AudioChannel functions and properties
- AudioClip functions and properties
- Camera functions and properties
- Character functions and properties
- DateTime functions and properties
- Dialog functions and properties
- DialogOptionsRenderingInfo functions and properties
- Dictionary functions and properties
- DrawingSurface functions and properties
- DynamicSprite functions and properties
- File functions and properties
- Game functions and properties
- GUI functions and properties
- GUI control functions and properties
- GUI Button functions and properties
- GUI InvWindow functions and properties
- GUI Label functions and properties
- GUI List Box functions and properties
- GUI Slider properties
- GUI Text Box functions and properties
- Hotspot functions and properties
- Inventory item functions and properties
- Maths functions and properties
- Mouse functions and properties
- Object functions and properties
- Overlay functions and properties
- Parser functions
- Region functions and properties
- Room functions and properties
- Screen functions and properties
- Set functions and properties
- Speech functions and properties
- String functions
- System functions and properties
- TextWindowGUI functions and properties
- ViewFrame functions and properties
- Viewport functions and properties
- Obsolete Script API
- Event Types
- Key code table
- Audio in script
Legal Notice
Getting in touch
Misc