Tutorial HTML How To Debug(Or Cheat) Twine{SugarCube} Variables

itsmbk

New Member
Jan 20, 2022
5
0
Twine supports a number of different Story Formats, SugarCube being one of them, and each story format is a self contained mini web-application that defines the features available to the Author.

Based on the meta-data stored in the project's <tw-storydata> element it was built using the Harlowe story format...
Code:
<tw-storydata name="X-Change Life" ... creator="Twine" creator-version="2.3.16" ... format="Harlowe" format-version="3.2.3" ...>
...which unfortunately doesn't include an equivalent of the SugarCube debug object/interface, nor does it have a documented JavaScript API.

There is a scope escalation hack that an Author can use to expose the Story Variable State/History in the Console, however after doing a quick review of the custom JavaScript included with this project it doesn't appear that the Author hasn't done that.
so its impossible for me to change variables?
 

greyelf

Well-Known Member
Nov 16, 2016
1,075
787
so its impossible for me to change variables?
You would need to add code like the following to the JavaScript section of the project...
Code:
if (!window.harlowe){
    window.harlowe = {"State": State};
}
...which would create a harlowe JavaScript variable that you could use to access the story format's State from the Console...
Code:
harlowe.State.variables
WARNING:
Harlowe's History system is based on , so it doesn't work the same as SugarCube's. Harlowe also uses its own custom implementations of the Array & Data-map objects internally, so they don't always behave the same as the Standard JavaScript implementation.
Because of this care needs to be taken when using JavaScript to altering an Array or a Data-map variable, because you can easily corrupt previous Moments in History if you update the object without first cloning it.

eg. if a DataMap variable was last updated in the project three Passage's ago, then the most recent instance of that object would exist in the Moment that represents three steps in the 'past'. And if you directly altered that object (without cloning it first) then you would be altering that past Moment as well as the ones between that one and the 'current' Moment.
 

Keil G

New Member
Jun 28, 2017
5
0
Hi , how to change "BuildingRenovation" value from 22 to 11 in:
SugarCube.State.active.variables.counters

counters: Map(3) { GymYearlyMembership → 36, CharlotteCollege → 16, BuildingRenovation → 22 }
_____size: 3
_____<entries>
_____0: GymYearlyMembership → 36
__________<key>: "GymYearlyMembership"
__________<value>: 36
_____1: CharlotteCollege → 16
__________<key>: "CharlotteCollege"
__________<value>: 16
_____2: BuildingRenovation → 22
__________<key>: "BuildingRenovation"
__________<value>: 22
 
Last edited:

greyelf

Well-Known Member
Nov 16, 2016
1,075
787
Hi , how to change "BuildingRenovation" value from 22 to 11
I don't know the project in question but based on the debug output you included the value of the $counters variable appears to be a JavaScript object, which as explained in the linked documentation requires the usage of the function to add/update the value of an associated property.

You would use code like either of the following to do that within the Console of your web-browser.
Code:
/* if the project is SugarCube 2.x based... */
SugarCube.State.variables.counters.set('BuildingRenovation', 11)

/* if the project is SugarCube 1.x based... */
SugarCube.State.active.variables.counters.set('BuildingRenovation', 11)
 
  • Like
Reactions: Keil G

Dropdude

Well-Known Member
Donor
Aug 5, 2016
1,191
1,152
Anyone know what code needs to be enabled to allow saving in sugarcube when the save menu/back menu has been removed
 

celtichawk

New Member
Mar 29, 2022
1
0
Hiya, first post here and I'm a bit stumped so figured I'd ask if anyone's got an idea what's going on?

I can use TWINE Hacker fine in Brave

The issue is I'm trying to tweak my money value in this game:



And I'm running into a brick wall. TWINE Hacker picks up the values fine, but the game throws an error if I change up the only one it spits out for credits (the game's form of currency)

So am I doing something wrong? I've not had any luck with the manual method either, since I'm not sure where to begin looking for the variables or how they are even stored on that game. Any ideas?
 

pzqmthm

Newbie
Jun 27, 2017
19
3
Hi, I'm trying to edit multiple variables at once on firefox, I was wondering if anyone knew how to fix my problem. I use
SugarCube.State.variables
to get the variables and I want to be able to copy and paste some of the edited variables for instance on the html game `Becoming Someone`
I have these values.
I want to be able to edit them like
charasmatic: true
good_in_school: true
quick: true
rich: true
smart: true
strong: false
and just pasting it back into the console, but it doesnt work
 

Lure of Chaos

Member
Apr 21, 2020
113
99
Hiya, first post here and I'm a bit stumped so figured I'd ask if anyone's got an idea what's going on?

I can use TWINE Hacker fine in Brave

The issue is I'm trying to tweak my money value in this game:



And I'm running into a brick wall. TWINE Hacker picks up the values fine, but the game throws an error if I change up the only one it spits out for credits (the game's form of currency)

So am I doing something wrong? I've not had any luck with the manual method either, since I'm not sure where to begin looking for the variables or how they are even stored on that game. Any ideas?
could you give more info about error?

it seems working just fine, take a look:
 

Lure of Chaos

Member
Apr 21, 2020
113
99
Hi, I'm trying to edit multiple variables at once on firefox, I was wondering if anyone knew how to fix my problem. I use
SugarCube.State.variables
to get the variables and I want to be able to copy and paste some of the edited variables for instance on the html game `Becoming Someone`
I have these values.
I want to be able to edit them like
charasmatic: true
good_in_school: true
quick: true
rich: true
smart: true
strong: false
and just pasting it back into the console, but it doesnt work
didn't understand you what exactly do you want to do?
 

pzqmthm

Newbie
Jun 27, 2017
19
3
didn't understand you what exactly do you want to do?
instead of writing each variable i want to change line by line like this
SugarCube.State.variables.player.charismatic=true
SugarCube.State.variables.player. good_in_school =true
SugarCube.State.variables.player. quick =true
SugarCube.State.variables.player. smart =true
i was wondering if its possible to copy the text the way it shows on the gyazo and edit it like that?
 

greyelf

Well-Known Member
Nov 16, 2016
1,075
787
The issue is I'm trying to tweak my money value in this game:

...since I'm not sure where to begin looking for the variables or how they are even stored on that game.
Based on the comment contained within the <head> element of the linked to HTML file the project was created using Twine v1.4.2 and SugarCube v0.9.6, along with many 'hacks'.

I don't know if TWINE Hacker supports both v1.x and v2.x of Twine, or older versions of SugarCube.

In that specific series of SugarCube you access all the currently known Story Variables by using the following in your web-browser's Console.
Code:
SugarCube.state.active.variables
And based on a little trial and error I was able to change the number of 'credits' I had using the following...
Code:
SugarCube.state.active.variables.attrib.credits = 4000
 

greyelf

Well-Known Member
Nov 16, 2016
1,075
787
note: You didn't include a link to the game so I'm assuming you mean

Hi, I'm trying to edit multiple variables at once...

I want to be able to edit them like
charasmatic: true
good_in_school: true
quick: true
rich: true
smart: true
strong: false
Based on the image you includes the startChoices Story Variable references a JavaScript .

There are a number of ways to update multiple properties of an Object at the same time, which you use depends on whether you're updating ALL the properties or only SOME of the them.

If you are updating ALL of the properties then you could just assign a new Object instance to the startChoices Story Variable.
Code:
SugarCube.State.variables.startChoices = {charasmatic: true, good_in_school: true, quick: true, rich: true, smart: true, strong: false}
If you only want to update SOME of the properties then you can do that using the function.
Code:
SugarCube.State.variables.startChoices = Object.assign(SugarCube.State.variables.startChoices, {charasmatic: true, good_in_school: true})
 

pzqmthm

Newbie
Jun 27, 2017
19
3
note: You didn't include a link to the game so I'm assuming you mean


Based on the image you includes the startChoices Story Variable references a JavaScript .

There are a number of ways to update multiple properties of an Object at the same time, which you use depends on whether you're updating ALL the properties or only SOME of the them.

If you are updating ALL of the properties then you could just assign a new Object instance to the startChoices Story Variable.
Code:
SugarCube.State.variables.startChoices = {charasmatic: true, good_in_school: true, quick: true, rich: true, smart: true, strong: false}
If you only want to update SOME of the properties then you can do that using the function.
Code:
SugarCube.State.variables.startChoices = Object.assign(SugarCube.State.variables.startChoices, {charasmatic: true, good_in_school: true})
yes this is it, thank you
 

Lure of Chaos

Member
Apr 21, 2020
113
99
instead of writing each variable i want to change line by line like this
SugarCube.State.variables.player.charismatic=true
SugarCube.State.variables.player. good_in_school =true
SugarCube.State.variables.player. quick =true
SugarCube.State.variables.player. smart =true
i was wondering if its possible to copy the text the way it shows on the gyazo and edit it like that?
of course you can use autocomplete btw
 

wapitawg

Member
Aug 16, 2017
246
129
How to alter the variable which checks if a given scene / passage was visited?
For example when script checks for something like that: <<<if visited("intro15")>>
 
Last edited: