question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Running code in SugarCube's scope from an external script.

See original GitHub issue

Is 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:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
cyrusfirheircommented, Feb 20, 2021

Doesn’t have to be a SugarCube.* function though… Could very well be a function on window, and SC could do the evalJavaScript().

0reactions
greyelfcommented, Feb 25, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Accessing story variables from external JS file : r/twinegames
Scope and Javascript Question -- Accessing story variables from external JS file. I'm working on a project in SugarCube 2.36.1 (and Twine ...
Read more >
Please add an option to place user scripts in external file #3
SugarCube executes scripts inside a closure (?) using eval() and that significantly limits what can be used in user .js files.
Read more >
Include External Javascript Scripts Sugarcube - Twine Forum
So there's no easy way to include an external script from a CDN? For example if I need <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.
Read more >
SugarCube v2 Documentation - Motoslave.net
Silently executes its contents as pure JavaScript code—i.e., it performs no story or temporary variable substitution or TwineScript operator ...
Read more >
The SugarCubes v2.0 Reference Manual - Inria
Reactive Scripts gives a very flexible and powerful mean to program over the Internet especially using code migration facilities. Actually, SugarCubes ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found