tmedwards / sugarcube-2

SugarCube is a free (gratis and libre) story format for Twine/Twee.
https://www.motoslave.net/sugarcube/2/
BSD 2-Clause "Simplified" License
177 stars 41 forks source link

:onvariableset #209

Closed SBUtltmedia closed 1 year ago

SBUtltmedia commented 1 year ago

I see variableSet in state.js I would like to trigger a callback whenever the game changes State.variables, I thought I could use an approach like https://javascript.plainenglish.io/lets-extend-console-log-8641bda035c3 but I don't think variableSet is exposed, could a listener like :onvariableset be made available?

tmedwards commented 1 year ago

It is exposed, but that's not going to help you very much. That static method is used only when setting r-values to an l-value that is likely to be either an expression itself or an identifier of uncertain provenance. It is not the normal method of mutating story variables.

The normal method of accessing and mutating story variables is to directly access their store. Detecting changes by wrapping said store in a Proxy object, or something similar, is unlikely to be feasible in v2.