Save available only on a specific day of the week

Twine Version: 2.3.14
Story Format: Sugarcube

Hello,
I have this great clock/time system running in my project.

/*
	Date & Time Widget Setup
*/
<<set
	window.GameDays to [
		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
	];
	window.GameMonths to [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
	];

	/*
		Below we have to use the multi-parameter version of the Date
		constructor, rather than the date string version, because the
		date string version treats a missing timezone offset as UTC.
		While there are ways to determine players' timezone offsets,
		so they could be added to a date string, it's more convenient
		simply to use the multi-parameter constructor.

		The point of this is so that you can simply initialize the game
		world clock to whatever date and time you wish without having to
		worry about the players' timezone offsets, while still ensuring
		that they all see the same game world dates and times.
	*/
	/* params: year , month(0-based) , day , hour(24H) , minute [, second ] */
	$gameDate to new Date(2020, 3, 17, 15, 30); /* e.g. Mar 17, 2015 03:24 */
>>


/*
	Date & Time Advancement Widget Definitions
*/
/* Adds the specified number of minutes. */
<<widget "addmins">><<nobr>>
<<set _dateOld to $gameDate.toDateString()>>
	
<<set $gameDate to new Date($gameDate.getTime() + $args[0] * 60000)>>

<<set _date to $gameDate.toDateString()>>
<<if _dateOld != _date>>	
	<<include "Reset Variables">>
<</if>>
<</nobr>><</widget>>

/* Adds the specified number of hours. */
<<widget "addhours">><<nobr>>
<<set _dateOld to $gameDate.toDateString()>>
	
<<set $gameDate to new Date($gameDate.getTime() + $args[0] * 3600000)>>

<<set _date to $gameDate.toDateString()>>
<<if _dateOld != _date>>	
	<<include "Reset Variables">>
<</if>>
<</nobr>><</widget>>

/* Adds the specified number of days. */
<<widget "adddays">><<nobr>>
<<set _dateOld to $gameDate.toDateString()>>
	
<<set $gameDate to new Date($gameDate.getTime() + $args[0] * 86400000)>>

<<set _date to $gameDate.toDateString()>>
<<if _dateOld != _date>>	
	<<include "Reset Variables">>
<</if>>
<</nobr>><</widget>>


/*
	Date & Time Printing Widget Definitions
*/
/* Prints the current date ("{weekday} {month} {day}, {year}"). */
<<widget "date">>\
<<print String.format("{0} {1} {2}, {3}",
	GameDays[$gameDate.getDay()],
	GameMonths[$gameDate.getMonth()],
	$gameDate.getDate(),
	$gameDate.getFullYear()
)>>\
<</widget>>

/* Prints the current time (12H). */
<<widget "time12hr">>\
<<if $gameDate.getHours() eq 0>>\
12\
<<elseif $gameDate.getHours() gt 12>>\
<<print $gameDate.getHours() - 12>>\
<<else>>\
<<print $gameDate.getHours()>>\
<</if>>:\
<<if $gameDate.getMinutes() lt 10>>0<</if>><<print $gameDate.getMinutes()>> \
<<if $gameDate.getHours() gte 12>>PM<<else>>AM<</if>>\
<</widget>>

/* Prints the current time (24H). */
<<widget "time24hr">>\
<<if $gameDate.getHours() lt 10>>0<</if>><<print $gameDate.getHours()>>:\
<<if $gameDate.getMinutes() lt 10>>0<</if>><<print $gameDate.getMinutes()>>\
<</widget>>

/* Prints the current date and time (12H). */
<<widget "datetime">><<date>> <<time12hr>> (<<time24hr>>)<</widget>>

Them, i also have this nice code block below that allow players to save only in a specific tag passage, in that case, “save-enabled”.

/* Save point on specific passage tag save-enable */

$(document).on(':dialogopening', function (ev) {
	if (!$("body").hasClass("save-enable")) {
		if ($(ev.target).find(".save").length) {
			$(ev.target).find(".save").ariaDisabled(true);
		}
		if ($(ev.target).find(".delete").length) {
			$(ev.target).find(".delete").ariaDisabled(true);
		}
		$('#saves-export').ariaDisabled(true)
		$('#saves-clear').ariaDisabled(true)	
	}
});

So, what i want to achive is a new code block where players can only save in a specific day of the week.
The reason behind it is because i want to create diferent dificculty levels, and how often is possible to save is something that will influence. In a easy mode for example, he can save everytime he is inside his house independent of the day of the week. Regular mode, only inside the player’s house and in specific day of the week. In a Hard mode, only inside the player’s house and only in the first day of the month.

I tried the following knowing that would not work and i will post here to help clarify better what i want.

/* Save point on specific days of the week */

$(document).on(':dialogopening', function (ev) {
	if GameDays[$gameDate.getDay()] == "Fri" {
	    if ($(ev.target).find(".save").length) {
			$(ev.target).find(".save").ariaDisabled(true);
		}
	    if ($(ev.target).find(".delete").length) {
			$(ev.target).find(".delete").ariaDisabled(true);
		}
			$('#saves-export').ariaDisabled(true)
			$('#saves-clear').ariaDisabled(true)	
	}
}); 

Obviously, the if GameDays[$gameDate.getDay()] == “Fri” is wrong and i don’t know how exactly i will target that. Also, wanna wrap everything on a var like, easy mode, regular mode and hard mode.
So that will regulate how often the player can save his game.

Thank you.

2 Likes

I 'm sorry, but I cannot assist you whatsoever as I’m not well-versed in Twine. I’m responding anyway though, because I believe your concept for a variable save availability as a component of a game’s difficulty level is very clever. I just wanted you to know that.

I hope someone comes along shortly to help you out.

P.S. Welcome to Intfiction!

1 Like

If you look in the Saves Settings section of the documentation you will find the Config.saves.isAllowed setting, which allows you to assign a JavaScript function that return either true or false depending on some logic.

Often this setting is used to allow/disallow Saving based on what Passage Tags have/haven’t been assigned to the Passage current being visited.
eg. If you don’t want Saving to be allowed when viewing a “menu” tagged Passage…

Config.saves.isAllowed = function () {
	return !tags().includes("menu");
};

You could use this same setting to control when Saving is allowed in your project, using code something like the following…

Config.saves.isAllowed = function () {
	return GameDays[State.variables.gameDate.getDay()] !== "Fri";
};

Your JavaScript code is trying to reference a Story Variable using the $variable syntax, which only works within TwineScript based code. You need to use the State API, specifically the State.variables object (or the function equivalent) to reference those variables from JavaScript code.
I include an example of such usage in the 2nd of my above Config.saves.isAllowed related code.

2 Likes

I have been learning a lot thanks to Twine 2 and this amazing community. But like probably a lot of users here, we are rushing to create a game and by doing so, we massively dive in tons of contents of programming. In my case, i truly love to learn and it’s amazes me when i create a simple code that works exactly like i was planning. But now and then, i got stuck with the basics because autodidacticism commonly are not a linear process.

Although my user is new, I’m a passive observer for a long time i would say.
You, TheMadExile, HiEv, Chapel and others are really legends.

Thank you very much for your time and help.

1 Like