# Subsystem

The Quillscript Subsystem is an Unreal Engine's Game Instance Subsystem. This object
manages the plugin as a hole, it holds state properties, and stores Quillscript's data.


# Data Storage

The Quillscript Subsystem centralizes all Quillscript relevant data in a single object.

# Variables

Quillscript variables are stored here as a map of key-value. The Key is of type Name, and the value is of type Text. The value is marked as a non-cultural variant by default.

# History

The Quillscript history stores relevant data from scripts. Primarily the number of times this script played and the Save State list containing the sequence of statements played when this script was run.

# References

This is where Quillscript stores the map of object references to use in script.


# Events

The Subsystem also centralizes some important assignable events.

# On Script Play

This event fires when a script starts to play.

#include "Core/QuillscriptSubsystem.h"
...

if (const TObjectPtr<UQuillscriptSubsystem> Subsystem{ WorldContextObject->GetWorld()->GetGameInstance()->GetSubsystem<UQuillscriptSubsystem>() })
    Subsystem->OnScriptPlay.AddDynamic(this, &UMyObject::ScriptPlay);
void UMyObject::ScriptPlay(AQuillscriptInterpreter* Interpreter)
{
    // Implementation.
}

# On Script End

This event fires when a script finishes to play.

#include "Core/QuillscriptSubsystem.h"
...

if (const TObjectPtr<UQuillscriptSubsystem> Subsystem{ WorldContextObject->GetWorld()->GetGameInstance()->GetSubsystem<UQuillscriptSubsystem>() })
    Subsystem->OnScriptEnd.AddDynamic(this, &UMyObject::ScriptEnd);
void UMyObject::ScriptEnd(FName ScriptId)
{
    // Implementation.
}

# On Notified

This event fires when the Interpreter's Notify built-in function is called from script or code.

$ Notify Message
$ Notify point,data,round
$ Notify 'Hello world!'

#include "Core/QuillscriptSubsystem.h"
...

if (const TObjectPtr<UQuillscriptSubsystem> Subsystem{ WorldContextObject->GetWorld()->GetGameInstance()->GetSubsystem<UQuillscriptSubsystem>() })
    Subsystem->OnNotified.AddDynamic(this, &UMyObject::Notified);
void UMyObject::Notified(FString Message)
{
    // Implementation.
}
#include "Core/QuillscriptAsset.h"
#include "Utils/Quill.h"
...

const TObjectPtr<UQuillscriptAsset> Script{ UQuill::GetScriptById("MyScriptId") };
const TObjectPtr<AQuillscriptInterpreter> Interpreter{ UQuill::PlayScript(this, Script) };

Interpreter->Notify("Message");

# On Variable Set

This event fires when a variable is created, its value is changed, or a variable is deleted.

#include "Core/QuillscriptSubsystem.h"
...

if (const TObjectPtr<UQuillscriptSubsystem> Subsystem{ WorldContextObject->GetWorld()->GetGameInstance()->GetSubsystem<UQuillscriptSubsystem>() })
    Subsystem->OnVariableSet.AddDynamic(this, &UMyObject::VariableSet);
void UMyObject::VariableSet(FName VariableName, FText OldValue, FText NewValue)
{
    // Implementation.
}