Setting passage tags through Harlowe Macros

Twine Version: 2.8.1
Using Harlowe 3.3.8

I’m creating a project where I’m trying to be as efficient as possible with passage usage; essentially, there is a phase of non-linear exploration where I want the passage background to change depending on the time of day.

However, I’m not entirely sure as to the nature of how passage tags are stored. I’ve tried using either an array or a datamap to add tags using a (set:) macro but that doesn’t seem to have worked and throws up errors.

Would anyone who’s a little more versed in the inner workings of twine’s engine be able to assist me on this?

You can access all passages and the current passage with the (passages:) and (passage:) macros and the associated tags are a part of the datamap that is returned.

You should be able to read and overwrite the tags value. However, this only takes effect while the story is being played. It will not alter the Twine file used to author your story.

https://twine2.neocities.org/#macro_passages

I’m not sure that using tags are necessary for controlling the background style of your story though. Especially if it’s a dynamic thing… but hopefully this helps you.

I’m using the story’s stylesheet to denote changes to the background based upon the tag, like so:

tw-story[tags~="noon"]
{
  background-image: linear-gradient(#a34400, #662b00);
}

I did read that segment of the documentation and that’s what let me to believe that the tags are an array. However, if I use code like this:

(if:$time is "day")[(set:(passage:)'s tags to it + (a:"noon"))]

It throws an error. If there is an alternative method that doesn’t require me to fiddle with it like this that’d help a lot.

Try something like this…

tw-story.noon
{
  background-image: linear-gradient(#a34400, #662b00);
}

And in a specially tagged “footer” or “header” passage, or the specific passages being affected themselves something like…

(if:$time is "day")[<script>$('tw-story').addClass('noon');</script>]

Each time a new passage is drawn, a new <tw-story> is drawn so the class you add should be reset with each link to a new passage.

Haven’t tested this out, but the logic is sound.

I found this solution from a post by @Greyelf many noons ago. I slay me. :wink:
https://twinery.org/questions/765/changing-the-tw-passage-color-according-to-variable-harlowe

This seems to function just the once and then doesn’t appear to loop. I’m utilising it like so:

(if:$time is "day")[<script>$('tw-story').addClass('noon');</script>]
(elseif:$time is "night")[<script>$('tw-story').addClass('night');</script>]

with the relevent tw-story.noon and tw-story.night you posted above. It is able to cycle once without issue before it seems to get stuck with the background being the ‘night’ colour.

Describe how you want the day night cycle to work from an end-user’s perspective. Include navigating to and from a passage.

Ah, I see an issue. The addClass doesn’t replace the class. Try…

$('tw-story').className('noon');

I got

"TypeError: $(...).className is not a function".

I don’t know a whole lot of html, I’m afraid, I’m sorry if I’m being difficult!

Apparently, I don’t know much either! :laughing:

We’re dealing with multiple layers to achieve this. I’m just not at a computer where I can test this. jQuery suggests that we try this…

$('tw-story').attr('class', 'noon');

Sorry about leading you around like that. I still might be. Let’s suffer through this together! :wink:

1 Like

Thank you for assisting me, I appreciate your commitment. :blush:

This seems to have worked from my initial test! I clicked through my test passages back and forth and it cycles between both options perfectly fine.

I’ll make this as a solution and if I come across another problem I’ll come back or make a new post. Thank you so much!!

1 Like

This can be a record of both a solution… and my early-onset senility. :wink:

2 Likes