HTML Having Twine/SugarCube read cotents from file (patch), or check for existence of file?

i107760

Sistersitting / Housesitting Developer
Modder
Game Developer
Nov 1, 2016
849
1,477
Hello everyone,
I've been googling around, but can't really find an answer. I'm trying to start distributing my patch for my Twine/SugarCube game as a file, that contains the patch variables. Right now I'm using a built in patch, but I would prefer having some actual separation between game and patch content.

Ideally, I would be able to load variables/a passage from a file on disk, and if that is not possible, I'd like to be able to check if a file "patch.txt" or whatever exists, and then apply the patch.

If anyone knows if this is possible with JS sorcery, please let me know.
 

DbatRT

Newbie
Apr 8, 2018
60
28
As far as I know, JS in principle can't access the file system bypassing the browser and Windows API (dialog boxes), this is done for security reasons, and also because JavaScript code is also executed in the sandbox and is always isolated.
This would be possible with Electron, but it's like firing a cannon at sparrows.
 

HiEv

Member
Sep 1, 2017
384
779
I'm not sure what DbatRT is talking about, since it doesn't sound like you actually need to "bypass the browser" or "access the file system" here.

In any case, yes, you can just use the SugarCube to open a JavaScript file (which should be placed in the same folder as the HTML file). The importScripts() function returns a , and you can use that to detect whether it succeeds or fails at importing the JavaScript file. I'd recommend taking a look at my " " sample code to see how to use that function properly (click "Jump to Start" in the UI bar to see other sample code there).

Note that, if you need to access SugarCube objects from that external JavaScript file, then you'll need to put "SugarCube." in front of them, such as "SugarCube.State.variables" or "SugarCube.setup", since it runs in a different scope. If you want the external script to add functions that your Twine code can access, then it's probably best to add them to the SugarCube.setup object, so the Twine code can use setup.functionName() to access them.

I have some more in-depth code for accessing files using a dialog box, if you want something like that, but it doesn't sound like you actually need it.

Oh, and yes, this all works from a regular browser. You don't need Electron or Node to do something like this.

Hope that helps! :)
 

i107760

Sistersitting / Housesitting Developer
Modder
Game Developer
Nov 1, 2016
849
1,477
I'm not sure what DbatRT is talking about, since it doesn't sound like you actually need to "bypass the browser" or "access the file system" here.

In any case, yes, you can just use the SugarCube to open a JavaScript file (which should be placed in the same folder as the HTML file). The importScripts() function returns a , and you can use that to detect whether it succeeds or fails at importing the JavaScript file. I'd recommend taking a look at my " " sample code to see how to use that function properly (click "Jump to Start" in the UI bar to see other sample code there).

Note that, if you need to access SugarCube objects from that external JavaScript file, then you'll need to put "SugarCube." in front of them, such as "SugarCube.State.variables" or "SugarCube.setup", since it runs in a different scope. If you want the external script to add functions that your Twine code can access, then it's probably best to add them to the SugarCube.setup object, so the Twine code can use setup.functionName() to access them.

I have some more in-depth code for accessing files using a dialog box, if you want something like that, but it doesn't sound like you actually need it.

Oh, and yes, this all works from a regular browser. You don't need Electron or Node to do something like this.

Hope that helps! :)
Thanks a lot, I will start reading this in detail soon. Your "Loading External Scripts" link does not work for me, perhaps something went wrong when posting it.

EDIT: Found loads of useful things in your signature, reading through things right now ^^ Especially Keyboard navigation is totally awesome.
 
Last edited:

DbatRT

Newbie
Apr 8, 2018
60
28
HiEv
I may be wrong, but I thought that TS asks how to implement the mechanics when the main game automatically checks for a patch and applies it without the user's participation. Because it is easy to load external scripts via a dialog box (Windows API), but problems arise on the user's side, because you will have to explain how to apply the patch, and not everyone will be able to follow the steps.

Because no matter how many times I've played HTML games, I've never met this method of updating, and so there may be problems with this, users just get used to the fact that they can download the new version > launch it and play without any extra movements.

P. S You made a good guide for beginners, I'm amazed.
 

i107760

Sistersitting / Housesitting Developer
Modder
Game Developer
Nov 1, 2016
849
1,477
My plan for the patch was to just have them drop a "patch.twee" or "patch.txt" or similar in their folder with the game, and then have it be activated when they start the game indeed. Seems this is possible from what I'm reading with the above, but still going through it and other things on his post/site. Quite incredible, and as I work on implementing them, I just get more questions ^^ But I'll try to figure out things myself first.
 

i107760

Sistersitting / Housesitting Developer
Modder
Game Developer
Nov 1, 2016
849
1,477
Okay, but keep in mind that users may not understand this process.

Now my question is, how did you intend to pin changes to the main file? I mean, Yes, you will load variables there and update the game, but all this will only work until the game is reloaded, and after that you will have to do everything again.

Of course, I can offer a solution and store the patch data in LocalStorage after the first download, and download it from there a second time, and a third time, and so on.

But it all seems strange. And there is a problem if there are more than one patches.
There will be only one patch file. It's strictly intended so that the against Patreon ToS elements of my game, are more separated from the game. I want to have the values the variables initialize to ("sister", "brother", etc.) be set outside of the game's actual .html, so Patreon is less likely to complain and I can pretend it was a modder who made the patch, not me. There are too many variables changes by the patch, to simply have the user enter everything themselves.
 

DbatRT

Newbie
Apr 8, 2018
60
28
There will be only one patch file. It's strictly intended so that the against Patreon ToS elements of my game, are more separated from the game. I want to have the values the variables initialize to ("sister", "brother", etc.) be set outside of the game's actual .html, so Patreon is less likely to complain and I can pretend it was a modder who made the patch, not me. There are too many variables changes by the patch, to simply have the user enter everything themselves.
Oh I understand, by the way, forget what I wrote about LocalStorage, I misunderstood your idea.

Now about ToS and Patreon, this has already been discussed many times, but in the end I think that they will ask you, and not from the one who created the patch, because it was not the user who modified the game file and posted it separately, it was you who provided the opportunity to apply the patch in the game and make changes. Thus, Patreon will accuse you and ask you to remove the possibility of applying the patch if It ever notices violations of the ToS. Because it's pretty obvious why you made it possible to apply a third-party patch.

In addition, you need to somehow distribute information about the patch, you need to leave somewhere a hint to the user that they can change the game as they like. In the end, it will be much better to upload a separate version of the game, on behalf of another "evil and naughty" user, so Patreon will have fewer questions for you, because in this case There will be no way to apply the patch inside the game that you did, and not another user.

In General, I don't see any problem letting the user change the identifiers themselves, there are only two of them, right? Brother and sister.
 

i107760

Sistersitting / Housesitting Developer
Modder
Game Developer
Nov 1, 2016
849
1,477
Yeah that's a concern for sure. But there's too many variables, I'm early on in the story and there's 10+ already. ("dad", "mom", "parents", "siblings", etc.) so I really don't want to have the users have to do it.

I can also, make an official file as an alternative to be included, an easter egg mode or something stupid like that, that gives me an excuse to be looking for a certain file to import it. Say I wanted it to be for patrons only, hence distribute it as a separate file. I'm just trying to give myself some shielding against Patreon's ToS, some separation is better than none I hope.
 

DbatRT

Newbie
Apr 8, 2018
60
28
Yeah that's a concern for sure. But there's too many variables, I'm early on in the story and there's 10+ already. ("dad", "mom", "parents", "siblings", etc.) so I really don't want to have the users have to do it.

I can also, make an official file as an alternative to be included, an easter egg mode or something stupid like that, that gives me an excuse to be looking for a certain file to import it.
You see, the problem is that if you want to be clean, you don't have to have anything to do with another version of the game. You can't make an official alternative, or make a hidden mode, there are no idiots sitting there, they almost certainly know about F95Zone and they just need to ask a question in the branch of your game or read the first post to find out everything.

In General, this is a difficult question, on the one hand Patreon does not care about their ToS and there are many games with a clear violation of everything that is possible. On the other hand, you need to approach this wisely, and do not advertise that your game has prohibited content. In any case, do not mention it on the Patreon page.
 

i107760

Sistersitting / Housesitting Developer
Modder
Game Developer
Nov 1, 2016
849
1,477
Yeah, I would not mention anything on the Patreon page. It would simply serve for people from F95Zone to go to, if they are uncomfortable with subscribing on SubscribeStar. Distributing an alternative version might be possible as well, but is more work. So I'd prefer a patch.
 

HiEv

Member
Sep 1, 2017
384
779
I may be wrong, but I thought that TS asks how to implement the mechanics when the main game automatically checks for a patch and applies it without the user's participation. Because it is easy to load external scripts via a dialog box (Windows API), but problems arise on the user's side, because you will have to explain how to apply the patch, and not everyone will be able to follow the steps.
As long as they just unzip the JavaScript patch file into the game's directory and don't change its filename or anything, then it's merely a matter of the code importing that file with the expected filename if it can. You don't even need to open a dialog box for it to work.

I have an (unreleased) game that I'm working on currently which successfully uses that method to make user-editable content.

Because no matter how many times I've played HTML games, I've never met this method of updating, and so there may be problems with this, users just get used to the fact that they can download the new version > launch it and play without any extra movements.
Yeah, I've never seen anyone use this trick before either, but I can tell you that it works (tested in numerous browsers) and it's pretty hard for the user to screw it up, since you just need to add a single file to the game's folder.

P. S You made a good guide for beginners, I'm amazed.
Thanks. :)

---

Got the patch to work, thanks so much HiEv!
Glad I could help! :)
 
  • Like
Reactions: i107760

DbatRT

Newbie
Apr 8, 2018
60
28
As long as they just unzip the JavaScript patch file into the game's directory and don't change its filename or anything, then it's merely a matter of the code importing that file with the expected filename if it can. You don't even need to open a dialog box for it to work.
I wonder if you can show me a sample code? Because I'm interested in how you upload a file without opening the dialog box.
At the moment, I use this .
Yeah, I've never seen anyone use this trick before either, but I can tell you that it works (tested in numerous browsers) and it's pretty hard for the user to screw it up, since you just need to add a single file to the game's folder.
You know, when someone hacks a game and puts a modified file in a branch of that game, and I think it's obvious to everyone what needs to be done, right? This is the Foundation of the basics...
Only I keep coming across comments like this:
2020-09-14 143144.png
 

HiEv

Member
Sep 1, 2017
384
779
I wonder if you can show me a sample code? Because I'm interested in how you upload a file without opening the dialog box.
Ah, I think I see the point of confusion here.

We're talking about playing a local HTML file, while you're thinking of an online HTML file.

Yeah, it would be different if the game was online.

Only I keep coming across comments like this:
Well, yeah. You need to tell some people where to put it. It's not that hard to give the instructions I gave. If you tell them, " unzip the JavaScript patch file into the game's directory and don't change its filename or anything," then there really isn't any way they could screw that up, other than by ignoring those instructions.

It's not that difficult to do.
 
  • Like
Reactions: i107760

DbatRT

Newbie
Apr 8, 2018
60
28
We're talking about playing a local HTML file, while you're thinking of an online HTML file.
No, I'm talking about a local HTML file, I understand what you're thinking, many security policies are disabled when using the localhost, or file Protocol, but this doesn't mean anything to me, because I don't know how to upload a file, bypassing the dialog box, accessing the file system directly.
 

i107760

Sistersitting / Housesitting Developer
Modder
Game Developer
Nov 1, 2016
849
1,477
No, I'm talking about a local HTML file, I understand what you're thinking, many security policies are disabled when using the localhost, or file Protocol, but this doesn't mean anything to me, because I don't know how to upload a file, bypassing the dialog box, accessing the file system directly.
Did you take a look at his site? has a good explanation, I was able to make it work following that.
 

HiEv

Member
Sep 1, 2017
384
779
No, I'm talking about a local HTML file, I understand what you're thinking, many security policies are disabled when using the localhost, or file Protocol, but this doesn't mean anything to me, because I don't know how to upload a file, bypassing the dialog box, accessing the file system directly.
Then I'm confused by what you mean when you say "uploading" then, since there's no "uploading" happening when you're only working with local files (i.e. both the HTML file and the "patch file" are on the player's computer in the same directory). ("Uploading" means transferring a file from your local system to elsewhere across the Internet.)

Also, it's not like the code gets to see the "file system directly", it just says, "Hey, does file 'X.js' exist in the current directory?" and then it either gets the file or it doesn't. If it works, then it just sticks the content inside of the <script> element it's using and executes the code. If it doesn't work, then it triggers an error, which can be ignored, and displays an error message in the console, which can't be trapped or hidden due to security reasons.

Now, there are some security restrictions about exactly where the code can look for such files, which is why it's best to put the "patch file" in the same directory as the HTML file, since in that case the browser will assume that that directory is a safe location for this HTML file to use.

Basically, it works the same as if you had:
HTML:
<script src="X.js"></script>
already embedded within your HTML file, except it's done dynamically via JavaScript.

Thus, I'm not really sure what's so confusing about how this is possible.

Am I missing something here?
 
Last edited:

DbatRT

Newbie
Apr 8, 2018
60
28
Did you take a look at his site? has a good explanation, I was able to make it work following that.
Yes, I watched it, I already wrote about it, and it's cool, but the problem is that I don't use Twine / SugarCube.

HiEv
Yes, I'm sorry, my English is not very good, and I probably explained my idea incorrectly.

Your discussion gave me the right idea: instead of importing raw data, it is better to compile it while the game is running, passing the calculations to an isolated web worker thread, so as not to clog the main thread for heavy calculations (they can be heavy for mobile devices).

In any case, thank you for your help, I will explore this direction this week.