0 votes
by (160 points)
Hello all,

I´m in need of recording the number of steps taken for the player to finish the story, including the times people hit the back button.

Right now everytime a person hits Back the number of steps also goes to the ones they had in the previous room.

My goal is to also record the number of times people hit Back as a move, to discourage them to go back to a previous choice.

Or at least for the variable to be fixed even after hitting Back.

How can it be possible?

Thanks!

1 Answer

0 votes
by (159k points)

One way to implement the functionality you want is to combine a Config.navigation.override handler with an Integer variable on the special setup object to track each time the Reader selects either one of your links or one of the navigation links.

Add the following to your story's Story Javascript area.

/* Track the number of steps (links) that are selected during the story's life-cycle. */
setup.steps = 0;

Config.navigation.override = function (destinationPassage) {
	/* Increment the counter. */
	setup.steps++;
	return falue;
};

... You can test the above by adding the following to the StoryCaption special passage.

Steps: <<= setup.steps>>

NOTE: Your question left out an important detail about if your story allows the Reader to use the built-in Save/Load functionality, and if so how to handle the steps counter during a Load so that it is 'reset' back to the value it was at the time the Save was made.

The following code handles the saving and loading of the setup.steps variable, it also needs to be placed within your story's Story Javascript area.

Config.saves.onSave = function (save) {
	var metadata = {steps: setup.steps};
	save.metadata = JSON.stringify(metadata);
};

Config.saves.onLoad = function (save) {
	var metadata = JSON.parse(save.metadata);
	/* The Load process calls the navigation.override handler, which will result it the steps counter being one higher than it should be and thus the need to decrement the value stored within the save by one. */
	setup.steps = (metadata.steps - 1);
};

 

by (160 points)
Wow!

works perfectly! Many thanks!
by (68.6k points)
edited by

CORRECTIONS

There's absolutely no reason to abuse Config.navigation.override in this way (you also misspelled false as falue).  The same effect may be achieved with a prehistory task function or, if using SugarCube v2.20.0 or greater, a :passageinit event handler.

The shown Config.saves.onSave and Config.saves.onLoad functions are needlessly complex.  Manually serializing the contents of the metadata property is pointless as saves get serialized as part of the process.

For example:

/* SugarCube v2 (all versions) */
prehistory['increment-steps'] = function () {
	setup.steps++;
};
Config.saves.onSave = function (save) {
	save.metadata = { steps : setup.steps };
};
Config.saves.onLoad = function (save) {
	setup.steps = save.metadata.steps - 1;
};
/* SugarCube v2.20.0+ */
$(document).on(':passageinit', function () {
	setup.steps++;
});
Config.saves.onSave = function (save) {
	save.metadata = { steps : setup.steps };
};
Config.saves.onLoad = function (save) {
	setup.steps = save.metadata.steps - 1;
};

 

...