HTML Java Modifying a variable with javascript

Souvenirs

Newbie
Game Developer
May 23, 2023
19
85
Hi,

I'm doing a little game on Twine chapbook, the problem is that chapbook only allows to modify a variable in the var-section at the top of the passage. Wich means I can't modify a variable in its own passage (imagine you start with money ==1 but on the same page you end up money ==2, that is not possible, i need to increment it on next passage).

So here comes my issue:
I have a cycling choice, let's say "blue" or "red" then we go to next passage and depending on the choice the text differs. But I would want also to differ a variable (red+=1 if red was chosen etc) wich is not possible in the var-section, i can set red += 1 but not based on the choice i made so even if blue was chosen red would also get +=1 and so the choice would have no impact.

So is there a way to use JS to do that ? Or any other way ?
I know I could just split the passage in two but it's less fun and it's annoying to do 2 passages for the same text with only a few words that changes.

Thanks
 

Saki_Sliz

Well-Known Member
May 3, 2018
1,403
1,011
I only dabbled with twine for a day or two, but if I recall, what you can do is have choice red open up one passage, and choice blue open up a different passage.

the passage however, can show nothing, it can just run code (such as red += 1), and the passage can automatically close and jump back to the script you want to cycle over and over again. I forget if you need to do anything special for the passage to act 'invisible' (ie run code, and automatically return to the previous passage)

While not elegant (makes your story map look like crap), it sounds like its a limitation with twine, but its not terrible, its just how most programming languages work, triggering some other bit of code to do some dirty work, while the top level code looks clean and pretty.
 
  • Like
Reactions: Souvenirs

Alcahest

Engaged Member
Donor
Game Developer
Jul 28, 2017
3,483
4,325
There are ways to do it using HTML and JavaScript, like using a button with onclick, updating the variable in JS and then updating the HTML element with the new value. Too busy to show code though, sorry.

And the other problem is that you're using chapbook and I don't know anything about it, and I don't know anyone else here who does. Most HTML games on this site use SugarCube and SugarCube provides additional ways to do what you want. If you want help from this site, you should consider switching to SugarCube. Or I suggest you seek out some forum for chapbook users when you want help.
 
  • Like
Reactions: Souvenirs