public void observeVariable ( String variableName , VariableObserver observer ) throws StoryException , Exception { ifAsyncWeCant ( "observe a new variable" ) ; if ( variableObservers == null ) variableObservers = new HashMap < String , List < VariableObserver > > ( ) ; if ( ! state . getVariablesState ( ) . globalVariableExistsWithName ( variableName ) ) throw new StoryException ( "Cannot observe variable '" + variableName + "' because it wasn't declared in the ink story." ) ; if ( variableObservers . containsKey ( variableName ) ) { variableObservers . get ( variableName ) . add ( observer ) ; } else { List < VariableObserver > l = new ArrayList < VariableObserver > ( ) ; l . add ( observer ) ; variableObservers . put ( variableName , l ) ; } }
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 .