Running code in SugarCube's scope from an external script.
See original GitHub issueIs your feature request related to a problem?
I think importScript()
and Tweego’s module/head options make it really easy to add JavaScript to games while keeping out of player’s ways (e.g., filling up their Story JS) but it has the limitation of all the code being run out of context. It would be nice to be able to run code in context. For example, if I could create a “wrapped” version of my macros that can be included via importScripts()
.
I envision say, creating “wrapped” versions of my macros that users can add via Tweego modules or via importScripts()
to clean up their projects a bit.
Describe the solution you’d like. From the brain-splintering conversation we had on Discord with Cy, the solution we came to was the following wrapper:
SugarCube.evalJavaScript((function () {
// code to run in-scope goes here
}).toString());
This does work, however, I’m always nervous about relying on SugarCube.*
methods given they are typically debug features and undocumented, so is this safe for production code? Is there a better way to do this? Would you rather add a specific method just for this so you can add more sanity checks and such?
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Doesn’t have to be a
SugarCube.*
function though… Could very well be a function onwindow
, and SC could do theevalJavaScript()
.In my (limited) experience the time when access (to SugarCube internals) is generally required by an external module is during startup, so the Module has a chance to ‘register’ itself in some way within the SugarCube scope. eg. initialise variables, define setup object references, etc…
After which such ‘registered’ references to the Module can be used by code that is run within the SugarCube scope.