Howdy, Stranger!

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

Using numerical values as direct passage links [Sugarcube]

In my game, I'm setting up a system by which the content of each random encounter is contained within its own unique passage titled by a specific number. Then, when a random encounter happens an initialization passage appears. To the player, the job of this passage is to announce the encounter, but what it also does is roll a random number, which is then used to link to the encounter passage associated with that number.

However, the only way I can think of to make that link is to set up a gigantic web of <<if>> conditionals that convert the random number to a string. Like this:
<<if $rand is 1>><<set $encountertitle to "Enc001">><<if>>
With this done, I can just stick the string variable into the standard passage link and it works fine.
[[Ready to fight!|$encountertitle]]
However, I would really rather avoid building a huge list of <<if>> statements like that, because I'm going to have many, many encounters possible and such a list would be very cumbersome to build, maintain, add to, etc, even if there's only one list that activates every time a passage is refreshed, such as in PassageReady.

I'd much rather find a way to just use the $rand variable as a direct link somehow. It would save so much time and effort and reduce the amount of code in an already bloated game. I can't do the same thing with numerical variables because the Twine passage link format seems to use numbers differently than text.

I've discovered that it does do something, and what it does is consistent. If I do this:
[[Ready to fight!|001]]
Then it doesn't go to the 001 passage but it does go to the same passage every time. Even more encouraging, if I go <<$rand = random(1,6)>> and $rand ends up being 1, I can just plug in $rand the same way I would plug in a string variable and it goes to the same place.

It seems like each passage in a story is assigned some kind of hidden identity number, and that's what it links to. But I have no idea how to determine or control which passages are assigned to which numbers. If I could with a certain level of confidence then I could do that.

Alternatively, perhaps there's a much easier and elegant way to accomplish what I need, in which case I'm all ears, even if it takes an overhaul of my code.

On a side note, how is 2.0? Is there a change log somewhere where I can see what's different? Does Sugarcube still work? It would mean a prohibitive amount of work to roll my code back to pre-Sugarcube. But hey, maybe it would be worth it.

Comments

  • I would suggest against using solely numerals for the title of any passage.  That's a bad idea for a number of reasons.

    And yes, each passage has an ID, in addition to its title.  Depending on how your numeral-based passage title filters down through the system it might get used as a string or an actual number, which will result in very different things happening (strings are compared to titles, numbers are compared to IDs).

    I would suggest using titles something like "Enc001", as in your first example, so there's no ambiguity.  You can easily use a little code to return a random encounter title.  For example, assuming $rand is an integer number (from random() or something similar), this function will return a string formatted similar to "Enc001": (this goes in a script tagged passage)

    window.encounter = function (id) {
    return "Enc" + ("000" + id).slice(-3);
    };
    Then you'd simply use it like so:

    [[Ready to fight!|encounter($rand)]]

    Charisma wrote:

    On a side note, how is 2.0? Is there a change log somewhere where I can see what's different? Does Sugarcube still work? It would mean a prohibitive amount of work to roll my code back to pre-Sugarcube. But hey, maybe it would be worth it.


    There is a build of SugarCube for Twine 2, however, there are a few issues in the current release of Twine 2 which break it, so a waiting we will go.  There's also, currently, no easy way to import Twine 1 projects into Twine 2 (largely, I'd wager, because there's been no need), so you'd be unable to import your project anyway.

    AFAIK, SugarCube might also be the only pre-Twine 2 format being made available on Twine 2, so rolling back to pre-SugarCube probably won't help you much.  The other two existing Twine 2 formats (Harlowe and Snowman) are vastly different from any Twine 1 format, so you'd be starting from scratch with either of them.
  • Bwahaha it works perfectly. I've been pining for some code to convert a number to a string.

    Thank you for all your help, Exile. You really are a superhero around here.

    Edit: While I'm at it, I do have another obstacle I've been wrestling with.

    There's a common thing that text-based games do that I can't figure out how to replicate in Twine: announce progress/regression and other types of events. For this I would need some way to have a conditional that triggers only when a variable transitions from one state to another.

    For example: many games might print the player a message about their strength achieving the next "level." Like going from the 10-20 category to the 20-30 category. "You have grown stronger, and are now built." Conversely, it can also print a similar message when regressing from 20-30 down to 10-20: "You have grown weaker, and are now frail." Obviously, this message should only print once, when the transition is made, but should print every time the transition is made and should be capable of printing almost anywhere (as strength gains can happen anywhere).

    I can't even conceive of a way to do this in Twine, but seeing how it's such a common element of these types of games, I'm hoping it's possible. Maybe even easy.
  • Hey, Sharpe! I'm just getting all the big names here.

    Back when I started this project, both those posts were of huge help to me, especially the objects one. I use so many objects it's sickening. The RPG also taught me a lot.

    Neither of them will help with my problem with a transitional trigger, though. You run a level check at the end of a fight, which is the only place the pc can gain exp - not to mention it's a linear level progression. Very simple. I'm talking about a much bigger system that announces changes not only to level but also stats and character status elements and things, and these changes can happen any time because of an automatic degrading mechanic I can't talk myself into getting rid of.

    After some careful thought, I can do it with my current knowledge. It just involves some bulky variable setups and if statements and I was hoping there was some kind of existing, elegant way to do it.
  • Glad you found them useful. I'd have to re-read those post to even know what's in them. It's been so long since I worked with Twine that I couldn't write the first line of code, unfortunately.
Sign In or Register to comment.