Airship Scriptable Objects

AirshipScriptableObject is a class you can derive from to create objects that live independently of GameObjects. This allows you to save data as an asset to use at runtime.

Like ScriptableObjectarrow-up-right in Unity, they are accessible from scenes and assets within a project.

circle-info

Changing values in a Scriptable Object requires a full publish (as it is an asset) instead of a code publish to update, if you need a value that can be changed via a code publish, you should be storing it in code.

// This will show in Create -> ScriptableObjects -> Example Data Object by default
@CreateAssetMenu()
export default class ExampleDataObject extends AirshipScriptableObject {
    public message = "Hello, world!";
    
    protected Awake() {
         // This will be fired when the scriptable object is first referenced/created.
         // The properties of this object will be set up on Awake
         print(this.message, "from the example scriptable object awake!");   
    }
    
    protected OnDestroy() {
         // This will be called if:
         //  - A CreateInstance() ScriptableObject is destroyed
         print("This object was destroyed!");
    }
}

This allows you to save data as an asset to use at runtime. This can be useful for global data shared between objects, avoiding the necessity for duplicate data.

Referencing a ScriptableObject in your game

As an example, we can use a ScriptableObject to create "templates" for NPCs we want to spawn

Then we can create it using the menu item:

Since NPCTemplate has CreateAssetMenu on it, it will show up here by default.
The resulting ScriptableObject - we can now set a name, the health and even a custom prefab if need be.

Then to reference the ScriptableObject

You will see we now have a reference field for an NPC template

Then click on the None (NPC Template) circle-o button - and it will give you a prompt to select a scriptable object of that type, which we can then select -

The template is now provided

Then when you run the game - it spawns the NPC with the name and health!

The spawner in action
Example of using the same scriptable object with multiple spawners

Last updated