Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • harlowe harlowe
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 48
    • Issues 48
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Container Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar

April 05-07 - Mercurial Paris Conference 2023 - One day of workshop dedicated to Heptapod and Mercurial usage and workflow. There are still places available for both workshops and talks!

  • Games
  • harloweharlowe
  • Issues
  • #253
Closed
Open
Issue created Apr 20, 2022 by StiathirsTheProtogen@StiathirsTheProtogen

Using Story Variables in JavaScript

A minor (3.x) version increase usually shouldn't have significant incompatibilities with existing code, but there is something I must mention: arbitrary Javascript syntax embedded in Harlowe macro calls is no longer permitted, and will produce an error. This includes stuff like (if: (document.title = "Wowie") is 1)[] (which does nothing except change the window title to "Wowie") or writing (set: $a = 1) instead of (set: $a to 1).

Then it would be nice to simply have a (javascript:) macro, wouldn't it? Granted, you can use <script></script>, but you can't use story variables without also using a scope escalation hack, which gets really messy. It's nice to be able to just embed JavaScript into our code, and can allow someone to do very powerful things, like using a database for a leaderboard or backups for save files. I personally don't see why we're not allowed to access story variables in JS. While most users won't need it, and therefore won't use it, some will. Instead you have to use an escalation hack and do stuff like this:

<script>
// after the escalation hack, not gonna say how to do this lol
var variable1 = harlowe.State.variables['var1'];

// do stuff with variable1, like even just alerting it
alert(variable1);

// you could also directly reference that in the print statement, but...
alert(harlowe.State.variables['var1']); // just sucks

// and setting the story variable would be
harlowe.State.variables['var1'] = "whatever";
</script>
^As you can see, that is an absolute disaster.

Now the question is, how would we make this (javascript:) macro work?

  1. We could try using a CodeHook. Now, the question is: where do we put it? (javascript:[]) or (javascript:)[]? Now for the hardest part: how would we handle this? Javascript uses the brackets for arrays and such, so that wouldn't work. So can we really use a CodeHook?
  2. We could use a string argument. We could simply do something like (javascript:"alert($a)"), and just use regex to find story variables and replace them with harlowe.State.variables['a'] (or however it's actually accessed by the engine). This would also easily allow dynamic JS in the story. Problem: that's a terrible idea and is basically JS eval() all over again.
  3. We could just override the new macro-running code and treat it the old way. But that makes it into a syntax tree, then makes it into a string, then uses the funny eval() command, which this entire process is both slow as hell and sketchy.
  4. We scrap the idea of a (javascript:) macro and just allow the user to access story variables in <script></script> via, say, story.variables['var1'], like we do in the scope escalation hack, but without the need of doing the actual hack.
Edited Apr 27, 2022 by StiathirsTheProtogen
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information
Assignee
Assign to
Time tracking