observeVariable method

void observeVariable(
  1. String variableName,
  2. VariableObserver observer
)
When the named global variable changes it's value, the observer will be called to notify it of the change. Note that if the value changes multiple times within the ink, the observer will only be called once, at the end of the ink's evaluation. If, during the evaluation, it changes and then changes back again to its original value, it will still be called. Note that the observer will also be fired if the value of the variable is changed externally to the ink, by directly setting a value in story.variablesState. The name of the global variable to observe. A delegate function to call when the variable changes.

Implementation

void observeVariable(String variableName, VariableObserver observer) {
  ifAsyncWeCant('observe a new variable');
  _variableObservers ??= ListMultimap<String, VariableObserver>();

  if (!state.variablesState.globalVariableExistsWithName(variableName)) {
    throw Exception("Cannot observe variable '" +
        variableName +
        "' because it wasn't declared in the ink story.");
  }
  _variableObservers!.add(variableName, observer);
}