Loading...
Skip to main content

Sharing data between Behaviors

The scripting runtime also implements the ability to share data and logic between different Behavior elements using the #import and #export preprocessor directives. These directives allow you to expose variables and functions from one Behavior to be accessed by another, enabling you to create modular and reusable scripts that can be easily linked together.

The #export Directive

The #export directive is used to expose variables or functions from a Behavior so they can be accessed by other Behaviors. This allows you to share data or logic between different parts of your project, promoting reusability and modularity.

When using the #export directive, it's important to understand that the execution order matters. Exports must be defined before they are imported. If you try to import a variable or function before it has been exported, the code will result in a compilation error.

This is because the exported data (variables, functions, etc.) is not available until the script defining the export has been processed and compiled. Therefore, the compiler needs to see the export definitions first to link them properly when they are later imported.

How to Use #export

When you define a variable or function, you can use the #export directive to make it available for use by other Behaviors. The exported functions or variables will be accessible in other scripts that import them. The #export directive must be placed at the bottom of your script.

The #import Directive

The #import directive is used to import variables, functions, or other exported data from other Behaviors into your current script. This allows different scripts to link together, sharing logic or state between different objects or components in the scene. It plays a key role in enabling modularity and reusability by allowing Behaviors to interact with each other, even if they are on different objects or at different levels in the scene hierarchy.

How to Use the #import Directive

The #import directive is placed at the top of your script and is used to specify the variables or functions you want to import from other Behaviors. These items must be previously exported from another script using the #export directive.

Exporting / Importing multiple items

You can export / import multiple variables or functions from other Behaviors in a single statement by separating them with commas. This reduces the need to write multiple lines and keeps your code compact and readable.

Example:

// Behavior 1 - Exports data
let playerHealth = 100;
function takeDamage(amount) {
playerHealth -= amount;
}
#pragma export(playerHealth, takeDamage)
//—----------------------------------------------------
// Behavior 2 - Imports data after it's been defined
#pragma import(takeDamage, playerHealth)
function attack() {
takeDamage(20); // Reduce player health by 20
}

Simplified Data Sharing and Abstraction of Dependencies

One of the key advantages of using the #import and #export directives is the simplified data sharing between Behaviors. A particularly powerful feature of this system is that you don't need to know where or how certain symbols (variables, functions, etc.) are defined, the scripting runtime takes care of managing these connections for you. This makes it much easier to build complex, dynamic systems where different Behaviors can interact without having to worry about their internal details or lifecycle.

Example:

Imagine you have multiple Behaviors: one for controlling player health, one for an enemy, and one for a game manager. Using #import and #export, the enemy can easily access the player's health, even if the player's script is on a different object in the scene.

Player Behavior:

let playerHealth = 100;

function takeDamage(amount) {
playerHealth -= amount;
if (playerHealth <= 0) {
console.log("Player has died!");
}
}

#pragma export(takeDamage)

Enemy Behavior:

#pragma import(takeDamage)
function attack() {
takeDamage(20); // Reduce player health by 20
}

In this case:

  • The Enemy Behavior does not need to know where the takeDamage action is defined or whether it exists in a specific part of the scene hierarchy. It just imports the takeDamage function and uses it.
  • The Player Behavior doesn't need to manage or track interactions with the enemy. It simply exports the necessary data and allows other Behaviors to access it.

The Benefit of Abstraction

This system allows you to focus on your logic and behavior design without worrying about the underlying details.