AngelScript

Shared script entities

If the application uses multiple script modules to control different parts of the application it may sometimes be beneficial to allow parts of the scripts to be shared between the script modules. The main benefits of shared entities is the reduced memory consumption, and the fact that the type of the shared entity is the same for the modules, thus simplifying the information exchange between modules where this is used.

Shared entities have a restriction in that they cannot access non-shared entities because the non-shared entities are exclusive to the script module in which they were compiled.

Once a shared entity has been compiled in one module it will not be compiled again in another module. This can be taken advantage of as a module that is to use a shared entity already compiled in a previous module doesn't have to provide the complete implementation in the script. If this is done, the application must make sure the module that has the complete implementation is always compiled first.

## How to declare shared entities

To declare a shared entity simply put the keyword 'shared' before the ordinary declaration, e.g.

      shared class Foo
      {
        void MethodInFoo(int b) { bar = b; }
        int bar;
      }

If the script tries to access any non-shared entity from within the shared entity then the compiler will give an error message.

Obviously, in order to work the scripts in all modules that share the entity must implement the entity the same way. If this is not done, the compiler will give an error in the scripts that are compiled after the first script that implemented the shared entity.

The easiest way to guarantee that the implementation is the same is by using the same source file, but this is not a requirement.

## What can be shared

Currently only the class, interface, function, and enum entities can be shared.

Future versions may allow global variables to be shared too.