ChapelR Cycle Macro for custom calendar year

If you are requesting technical assistance with Twine, please specify:
Twine Version: Twine web and latest app just installed
Story Format: Sugarcube 2.x.x

Hello, first question here, i have read a couple of posts both here and on the reddit and more whilst searching on google but i am not sure how to ask, it’s probably something easy.

I plan to create a custom calendar/day night system, i use ChapelR Cycles custom macro, and at the end there’s a code for cycle-reset that i can’t quite seem to make work.

I have this on the javascript section of the story:

$(document).on(':cycle-reset', function (ev) {

if (ev.cycle.name === 'daynight') {

// increment day night

State.variables.totaldays + 1;

State.variables.weekday + 1;

}

});

Right below the // end cycles.min.js notation.

To display the date and cycle i use the storyCaption portion, it does display but it does not update, the variable does not change once the cycle resets.
And this is in the storyInit passage, it is properly set and the variables are shown as they were set.

<<set $totaldays to 1>>
<<set $monthday to 21>>
<<set $weekday to  1>>
<<set $numbermonth to 1>>
<<set $dayweek to "Sunday">>

<<if $numbermonth == 1>><<set $month to "June">>
<<elseif $numbermonth == 2>><<set $month to "July">>
<<elseif $numbermonth == 3>><<set $month to "August">>
<<elseif $numbermonth == 4>><<set $month to "September">>
<<elseif $numbermonth == 5>><<set $month to "October">>
<<elseif $numbermonth == 6>><<set $month to "November">>
<<elseif $numbermonth == 7>><<set $month to "December">>
<<elseif $numbermonth == 8>><<set $month to "January">>
<<elseif $numbermonth == 9>><<set $month to "February">>
<<elseif $numbermonth == 10>><<set $month to "March">>
<<elseif $numbermonth == 11>><<set $month to "April">>
<<elseif $numbermonth == 12>><<set $month to "May">>
<</if>>

<if $numbermonth gte 1 or $numbermonth lte 3>><<set $season to "Summer">>
<<elseif $numbermonth gte 4 or $numbermonth lte 6>><<set $season to "Fall">>
<<elseif $numbermonth gte 7 or $numbermonth lte 9>><<set $season to "Winter">>
<<else>><<set $season to "Spring">><</if>

<<newcycle 'daynight' 1>>
    <<phase 'morning' 'midday' 'evening' 'night'>>
<</newcycle>>


<<if $weekday gte 8>>
<<set $weekday to 1>>
<</if>
<<if $weekday == 1>>
<<set $dayweek to "Sunday>>
<<elseif $weekday == 2>>
<<set $dayweek to "Monday">>
<<elseif $weekday == 3>>
<<set $dayweek to "Tuesday">>
<<elseif $weekday == 4>>
<<set $dayweek to "Wednesday">>
<<elseif $weekday == 5>>
<<set $dayweek to "Thursday">>
<<elseif $weekday == 6>>
<<set $dayweek to "Friday">>
<<elseif $weekday == 7>>
<<set $dayweek to "Saturday">>
<</if>

<<if $totaldays == 11>>
<<set $numbermonth == 2>>
<<set $monthday == 1>>
<<elseif $totaldays == 42>>
<<set $numbermonth == 3>>
<<set $monthday == 1>>
<<elseif $totaldays == 73>>
<<set $numbermonth == 4>>
<<set $monthday == 1>>
<<elseif $totaldays == 103>>
<<set $numbermonth == 5>>
<<set $monthday == 1>>
<<elseif $totaldays == 134>>
<<set $numbermonth == 6>>
<<set $monthday == 1>>
<<elseif $totaldays == 164>>
<<set $numbermonth == 7>>
<<set $monthday == 1>>
<<elseif $totaldays == 195>>
<<set $numbermonth == 8>>
<<set $monthday == 1>>
<<elseif $totaldays == 226>>
<<set $numbermonth == 9>>
<<set $monthday == 1>>
<<elseif $totaldays == 254>>
<<set $numbermonth == 10>>
<<set $monthday == 1>>
<<elseif $totaldays == 285>>
<<set $numbermonth == 11>>
<<set $monthday == 1>>
<<elseif $totaldays == 315>>
<<set $numbermonth == 12>>
<<set $monthday == 1>>
<</if>

I have seen other methods of using date and time, but my story sets place in a fictional world with a different year duration month names and the like, so it cant reflect current or real date/time.

I created the system to emulate our world calendar to be able to better spot issues during testing but once i check it works i would be changing them.

Thanks in advance.

Edit: there where some extra ’ when i pasted. Removed just in case.

There are a couple of issues and misconceptions with your existing usage of Chapel’s Cycles system:

1: Adding 1 to the current value of a variable (eg. State.variables.weekday + 1) does not update the value stored in that variable, to do that requires the inclusion of an assignment operator.
eg. State.variables.weekday += 1

2: The :cycle-reset Cycle Event only occurs if you manually use either the <<editcycle>> macro or the <cycle>.reset() method to reset a cycle. That event does not occur when the “current” phase changes from being the last in the list to the being the first.

If you need to do something when the above situation occurs then you would monitor the :cycle-change event, and check if the current stack value of the cycle instance is greater than 1 and if the “current” phase if the first one you defined.

$(document).on(':cycle-change', function (ev) {
	if (ev.cycle.name === 'daynight'
			&& ev.cycle.stack > 1
			&& ev.cycle.check('morning')) {
		// increment day night
		State.variables.totaldays += 1;
		State.variables.weekday += 1;
	}
});

3: The contents of a Passage is not automatically reprocessed whenever that value of a variable it reference is updated/changed, this includes special Passages like StoryInit and StoryCaption.

This means that currently the block of <<if>> and <<elseif>> macro calls within your StoryInit special Passage is only being executed when the project starts up, which is why later changing of the values of the $weekday and/or $totaldays and/or $numbermonth variables is having no effect on the associated variables like $month and/or $season and/or $dayweek

4: The visual output of the current page does not automatically refresh if the value of a variable, whose value is being show on the page, is dynamically changed.

eg. if you are displaying the current value of $month on the page, and the current value of that variable is programmatically changed then the value being displayed will not automatically refresh, unless a Passage Transition occurs.

If you want displayed content to change dynamically without doing a Passage Transition then you need to: first apply an identifier to the area of the page you want change; then use something like the <<replace>> macro to update that area.

The following is a simple example of using the above technique to dynamically refresh an identified area of the current page. It uses Custom Styling to apply an ID to an area of the page, a <<link>> macro to simulate “dynamically” executed code, and the <<replace>> macro to update the identified area.

<<set $counter to 0>>

Turns: @@#turns;$counter@@

<<link "Dynamically Update Turns">>
	<<set $counter += 1>>
	<<replace "#turns">>$counter<</replace>>
<</link>>

Obviously your implementation would need to be more complex but the basic principle is the same.

5: When you have a block of code that you want to execute multiple times, like the block of <<if>> and <<elseif>> macro calls that are currently within your StoryInit special Passage, it is often a good idea to place that code within a custom <<widget>> and to call that widget instead as needed.

The following is an example of placing all that block of <<if>> and <<elseif>> macro calls within a single widget, however you may need to breakup that block into separate widgets.
warning: the following code has not been tested, it may contain syntax or logic errors.

<<widget "updatecal">>

	<<if $totaldays == 11>>
		<<set $numbermonth == 2>>
		<<set $monthday == 1>>
	<<elseif $totaldays == 42>>
		<<set $numbermonth == 3>>
		<<set $monthday == 1>>
	<<elseif $totaldays == 73>>
		<<set $numbermonth == 4>>
		<<set $monthday == 1>>
	<<elseif $totaldays == 103>>
		<<set $numbermonth == 5>>
		<<set $monthday == 1>>
	<<elseif $totaldays == 134>>
		<<set $numbermonth == 6>>
		<<set $monthday == 1>>
	<<elseif $totaldays == 164>>
		<<set $numbermonth == 7>>
		<<set $monthday == 1>>
	<<elseif $totaldays == 195>>
		<<set $numbermonth == 8>>
		<<set $monthday == 1>>
	<<elseif $totaldays == 226>>
		<<set $numbermonth == 9>>
		<<set $monthday == 1>>
	<<elseif $totaldays == 254>>
		<<set $numbermonth == 10>>
		<<set $monthday == 1>>
	<<elseif $totaldays == 285>>
		<<set $numbermonth == 11>>
		<<set $monthday == 1>>
	<<elseif $totaldays == 315>>
		<<set $numbermonth == 12>>
		<<set $monthday == 1>>
	<</if>

	<<set _months to ["June", "July", "August", "September", "October", "November", "December", "January", "February", "March", "April", "May"]>>
	<<set $month to _months[$numbermonth - 1]>>

	<if $numbermonth gte 1 or $numbermonth lte 3>>
		<<set $season to "Summer">>
	<<elseif $numbermonth gte 4 or $numbermonth lte 6>>
		<<set $season to "Fall">>
	<<elseif $numbermonth gte 7 or $numbermonth lte 9>>
		<<set $season to "Winter">>
	<<else>>
		<<set $season to "Spring">>
	<</if>

	<<if $weekday gte 8>>
		<<set $weekday to 1>>
	<</if>

	<<set _daynames to ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]>>
	<<set $dayweek to _daynames[$weekday - 1]>>
<</widget>>

To execute the above new widget simply call it like you would a macro…

<<updatecal>>