Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Is it possible to read a passage's TAGs from code? Possibly parent passage's TAGs, too?

One of my Twine (with Sugarcube) projects has a series of passages representing locations. I would like to easily see which are considered DARK or ONFIRE with a quick glance at my passages overview screen.

Also, if reading TAGs is doable, is it doable to set TAGs via code? (I'm OK without this one, but it would be nice, of course.)

Now, if the reader is viewing "InBakery" (which has TAGs "Indoors" and "Safe") and I use <<display "HasCake">> (with TAG "InHand") can code in either passage see TAGs for the other?

(So when I display info about the cake, it can see if it's indoors and make a comment about 'maybe you should blow out the candles now that you're indoors". Or if not in a Safe zone/passage, 'lit candles will make you easier to see'.)

Hopefully, this made sense. (Writing at 2am.)

For the record, I searched the forums here and Google...

-- Cam

Comments

  • Which version of SugarCube? Or are you confusing it with Sugarcane?

    Moving on under the assumption that SugarCube is actually being used.

    Reading the fine manual might be informative. Documentation for SugarCube 2.x and the legacy 1.x.

    Specifically, you probably want the entry on the tags() story function (docs: 2.x or 1.x), which returns a new array consisting of all of the tags of the given passages—if omitted, it will default to the current passage (within the history).

    Also likely useful in this endeavor are the contains*() family of array methods (docs: 2.x or 1.x).

    For example, to check if the "InBakery" passage has a particular tag:
    /* If the tag "Indoors" exists on the "InBakery" passage, returns true, else false. */
    tags("InBakery").contains("Indoors")
    
    /* Used in some TwineScript. */
    <<if tags("InBakery").contains("Indoors")>>
    …bakery is an indoor location…
    <<else>>
    …bakery is not an indoor location…
    <</if>>
    
    However, based on the candles example you gave in the OP, you do not actually want to give tags() a passage, which will cause it to poll the current passage (current as in navigated to, within the history; using the <<display>> macro does not count). For example, in the "HasCake" passage:
    <<if tags().contains("Indoors")>>
    maybe you should blow out the candles now that you're indoors
    <</if>>
    <<if not tags().contains("Safe")>>
    lit candles will make you easier to see
    <</if>>
    
    Though, of course, those only make sense if you know the candles are lit, which you'll probably need to track via a $variable (SugarCube treats the properties of passages, which include tags, as immutable, and thus provides no easy way to modify them). For example, that could look something like the following (assuming $cakeCandlesLit will either be true or false):
    <<if $cakeCandlesLit and tags().contains("Indoors")>>
    maybe you should blow out the candles now that you're indoors
    <</if>>
    <<if $cakeCandlesLit and not tags().contains("Safe")>>
    lit candles will make you easier to see
    <</if>>
    
    /*  OR  */
    
    <<if $cakeCandlesLit>>
    <<if tags().contains("Indoors")>>
    maybe you should blow out the candles now that you're indoors
    <</if>>
    <<if not tags().contains("Safe")>>
    lit candles will make you easier to see
    <</if>>
    <</if>>
    

    PS #1: In the examples given above, I put most things on separate lines for clarity. In actual practice, you'll probably want to use some form whitespace elision, of which there are several methods available.

    PS #2: As a tangential addendum. Passage tags are transformed into classes whenever a passage is navigated to normally (in case you wanted to do some styling based on tags; classes are slugified though, which is described under the General Notes section of the CSS doc).
  • (awesome RTFM joke)
  • Which version of SugarCube? Or are you confusing it with Sugarcane?
    {SNIP!}
    Reading the fine manual might be informative. Documentation for SugarCube 2.x and the legacy 1.x.
    {SNIP!}
    PS #2: As a tangential addendum. Passage tags are transformed into classes whenever a passage is navigated to normally (in case you wanted to do some styling based on tags; classes are slugified though, which is described under the General Notes section of the CSS doc).

    The version of SugarCube I'm using is: v1.0.22

    ...and I will say, in defense of my honor, that I used Google to search and I went through several webpages (using ^F-find), including the documentation. I came up empty after several attempts on different days before finally resorting to posting here. (And I reasoned that someone else might benefit from my ignorance being resolved.)

    Also... WOW. Thank you for all the detail. (No sarcasm.) I would have been happy with just a link to the page. The added info and code snippets are very much appreciated.

    And... I'm still learning modern webpage coding so CSS is beyond me, right now. (I am learning, though!)

    Finally, thank you so much for Sugarcube! It is a wonderful tool!

    -- Cam
  • Sage wrote: »
    (awesome RTFM joke)

    Sage... ;-P *guilty razz-berries* to you...

    I caught the reference... though one instructor from ages ago used to have a sign behind his desk, and would sometimes answer questions by pointing to the sign... as he explained:
    In class, it means Read That F-f-f-f-f-fabulous Manual... because I'm not allowed to use *that* word anymore.
    He was a grumpy man with a wicked sense of humor... thank you for the trip down memory lane.
  • Believe it or not @CamD , being ex-military I have literally NEVER heard any other word used for the F in RTFM.

    Like, literally EVER. I was laughing because I thought it was hilarious that someone actually thought to change it, because it never occurred to me. (I guess maybe I don't really self-censor myself enough. Ha!)

    I promise I wasn't saying you need to read the manual... still... I'm glad to see you had a sense of humor about it either way!

    And I second the "Thanks" to TheMadExile.

    SugarCube is awesome.
  • CamD wrote: »
    The version of SugarCube I'm using is: v1.0.22
    In that case, you may wish to upgrade to the latest 1.x release (1.0.25), as it fixes a few issues present in prior versions (release notes).

    CamD wrote: »
    ...and I will say, in defense of my honor, that I used Google to search and I went through several webpages (using ^F-find), including the documentation. I came up empty after several attempts on different days before finally resorting to posting here. (And I reasoned that someone else might benefit from my ignorance being resolved.)
    I meant it as a light-hearted poke, so don't take it too seriously. 8)

    Some people don't bother looking, some look but just can't find, others still look and find but the docs just don't click for them. So, unless I knew for absolute certain that someone simply hadn't made a good faith effort to help themselves, I would never seriously just tell them to RTFM (gads, that would be jackassery).

    Speaking of which, if you find something in the docs which makes no sense to you, please let me know (I'm under no illusions that the docs are anything resembling perfect, or complete for that matter).

    As an aside, most webpages about Twine are, frankly, terrible, so I'm always disheartened when people seem to prefer searching though the dross of the internet rather than simply reading the docs I've written. That's not the case here (as you checked the docs too), but it does happen a lot.

    Hmm. That probably came out a bit moodier than intended. Don't take it too seriously, I'm generally happy to answer questions about SugarCube, specifically, and/or Twine coding, in general.

    CamD wrote: »
    Also... WOW. Thank you for all the detail. (No sarcasm.) I would have been happy with just a link to the page. The added info and code snippets are very much appreciated.
    Well, occasionally, all the examples (from the docs) in the world can't make something click for someone, so I like to give more targeted examples and extra info when I can.
Sign In or Register to comment.