A persistent choice - best way to implement?


I’m hoping one of you choicescript/javascript enthusiasts have already figured this out because it seems like my dilemma would be a common one (though I’ve looked through the forum and haven’t found it).

I’d like to include a persistent option but am not sure of the best way to do it.

For example, if a game has combat there should be an option to DRINK A HEALTH POTION at every *choice. Putting that option in front of the user at every *choice would become obnoxious rather quickly, so I’d like to put it somewhere else in the interface where it would always be available. I think it would work if I put it on an additional stats screen, but I already have two stat screens and would like to avoid adding another one.

Adding it to one of the existing stat screens would be an option, but I haven’t had consistent results with it. In every case, adding a *choice to the screen splits the screen and the user has to press the NEXT button twice. Sometimes the variable will increase and other times it won’t.

Another option would be to include a button on the screen that doesn’t open another stat screen but simply executes the javascript that would increment a variable – but I don’t know how to do that.

Anyone been where I’m at and have an idea that might work? Anyone not been where I’m at but still have a thought?


Unfortunately, no changes that you add to a stat_screen have any permanent effect on the game. As soon as the stat_screen is closed, all changes are reset to before you opened the screen. As such, adding the potion there isn’t an option. The only working way would be to have the option at every choice.

A workaround would be to create your own stat_screen, either in a new page or as a *gosub. You would still be stuck with adding it to every choice, but could add more information to the screen as you see fit (change weapon / armor, etc.).


No current way for ChoiceScript to handle it, and it seems like it would be a large thing to code in with JS. Remember CS is built mostly for narrative, with ‘gammy’ concepts paid little to no heed. Probably, if you’re working on something like that, it would be best to tie nearly everything into a single choice with a lot of *if commands disabling the choices when you don’t want them used.


You could effectively make a button to do this… But it’d involve jumping scenes which would lose your current scene’s place, so that’d need to be noted and stored somehow then referred back to once the potion has been drunk.

In other words, there is no “good” way to do it, just copy and paste it’ll be significantly easier.


There is precedent for what CJW has suggested. You can use a set of variables to make a save point, and effectively *gosub between pages. I’m sure Reaperoa can direct you to the thread for this as he described it originally, I believe.


I wasn’t expecting so many responses so quickly. I’ll look into the *gosub thing if I find what I’ve just done doesn’t work right. So far it appears to work so I’ll post it in the event that someone else might want to do something similar (or point out where it fails).

My goal was to do it with the minimum of changes, and then only the files within the game folder (no messing with persist, ui, util, etc). It’s only a few lines on Index.HTML, but it took me awhile to get it right.

Within the body tags put something like:
Increase Health

I think you can put this anywhere, but for this example I just put it right after one of the other blocks:

      function IncreaseHealth() {
           this.stats['health'] = this.stats['health'] + 5;
           this.stats['potion'] = this.stats['potion'] - 1;

I’ve only done some surface testing, but the stats screen get updated, the current scene is updated, and when I jump to another scene it sticks.


Oops, sorry about that, the button tag actually posted a button (I guess that’s what ‘Preview’ is for). Looked great as I was typing it though.

So, it’s just a button tag with onClick=“IncreaseHealth()” in it. And, of course, the function has script tags around it.



Ah, yes the code tag does make it look much better:
Increase Health



It is possible to shorten the time you have entering long choices, for instance, you can create a varible for battle scenes, i.e. *create a, then, *set a “Use a potion”, then in the choice, you can use #${a}, and use a potion will become considerably faster to type, I don’t know if this helps, but it makes the process less tedious and allows you to make the battle scenes faster.


That will work yes, but you’ll need some conditional parameters else people can just keep clicking it (never run out of potions!).


CJW, you are correct, I still need to wrap the thing in a check for ‘potion’ > 0. I’m just assuming that it will work but I suppose I should make sure before going much further.


The variable is stored in javascript, so in theory, there will be a way to do it, you might have to specify which file (mygame.js) it’s in, but yeah it’s all very doable.

It’s a good solution as well but most people would rather not touch the j/s so that’s why no one suggested it.

Nah, as long as they’re global variables (which they are) it should work fine, just gotta work out conditions :stuck_out_tongue:

Something like this?



I think I’m done tweaking stuff for now. I’ve gone a bit farther than I originally intended so I’ll explain what I’ve done. I’m sure this is sissy stuff to Dan Fabulich but for those who don’t code for a living/hobby it might help knowing that this would be fairly simple to add to their own game:

First, here’s the javascript:`


The eatRation function checks to be sure that there are potions (in this case “meals”) in the inventory and then proceeds to increment one of the stats. If it goes beyond the maximum for the stat it sets it to the maximum. The button is also removed from the screen so that it can’t be pressed again when the maximum has been reached (if that line is used, you will need a way to add it back when needed). The potion stat is decremented by 1.

The last line, refreshScreen() is there to call the next function because I wanted to see those 3 stats on the screen at all times. I have some span tags near the top of the screen that are updated by this function. The function also includes a line to add the Eat Ration button back to the screen when it is needed. I’m sure there is a more efficient place call this function but I just added onclick=“refreshScreen()” to the body tag.

Anyway, it does everything I wanted and seems stable.


Glad to hear it, and thanks for sharing! :slight_smile: