I'd like to dynamically add tags to passages in Sugarcube, like L's <<addtag>> macro:
http://www.glorioustrainwrecks.com/node/5021But the macro throws an error for me: "Error: cannot execute macro <<addtag>>: Cannot read property 'tags' of undefined"
Is there a way to do what I want in Sugarcube?
Thanks for reading.
Comments
Here's what I want to achieve: I have a set of background images for various locations in my story. Some passages can be triggered in different locations, so in these passages I'd like to run a script that checks my $location variable and applies the appropriate CSS tag to display the desired image. I'm sure there's a better way to do it, though.
I don't think I need the tags to be saved between sessions, as each of these variable-location passages will only be accessed once in the story.
EDIT: Sorry, forgot to add that for this to work, you need this useful bit in your code:
You're spot on about using classes though.
@mostly useless: If I'm understanding you correctly, you have a set of passages which you can't simply tag with a location tag (e.g. forest), since they're generic passages and could/will be used in multiple locations.
If that's the case, and assuming that $location contains the name of the location (similar to the passage tags you've used for the same purpose, e.g. forest), then simply do something like this at the very top of each of these location-agnostic passages: If $location doesn't simply contain the name of the location, then let me know what $location actually contains and I'll give you an updated example.
@mostly useless: It sounds as though TME has probably figured out what you're needing, but just in case, here's more of my code for context; basically, I'm using the code to manage CSS transitions on both incoming and outgoing passages, so that different transitions can be used to switch to different types of content. [Edited a couple of times for clarity...]
Based on your shown code, passage is a passage object, which means that the classes property is a JavaScript array, which has no add() method. Unless you or some library you've loaded has modified the Array prototype, adding an add() method, those bits cannot be working. You may not be seeing an error for various reasons (I assume that code's in PassageReady or something like that), but it has to be throwing an error (likely a TypeError).
As a test, with your compiled project open in your browser, open the dev console and try something like this (you should see an error): Additionally, in that code, at least, you never do anything with the "flip" you're tying to add to the passage object anyway.
Honestly, I would have thought my second reply would have made it clear that I didn't know that you'd edited your post. I wouldn't have said "Unless you or some library you've loaded has modified the Array prototype, adding an add() method", nor would I have suggested that you test for the error. What would the point of either of those have been (or my second reply at all), if I'd known about your edit.
I checked the Sugarcube documentation and tried things like: But I can't seem to get the syntax right and only the variable class is being added.
Thanks for reading!
For example: That would add the class(es) specified by $location to both the body element and all elements containing the class "pie".