Howdy, Stranger!

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

Using a random code in a variable (Harlowe)

edited March 2016 in Help! with 2.0
Say I have 10 lockpicks, I want it to be random with how many you break.

(if: (random: 1, 10) would give you the random number of lockpicks,
so then I would have to do (set: $lockpicks) and link it somehow with the random code to make it show.
If you break all 10 lockpicks, you head back to the previous passage, which means the next passage wouldn't show.

I have followed a tutorial on variables, but using them alongside a random code would be a little harder.

Comments

  • You can assign the value returned by the (random:) macro to a variable like so:
    (set: $broken to (random: 1, 10))
    

    You could decrease the original $lockpicks variable and check if there are any left, and if they are all broken then you could show a link.

    note: The following assumes you have done a (set: $lockpicks to 10) in a prior passage.
    (if: $lockpicks > 0)[
    (set: $broken to (random: 1, $lockpicks))
    (set: $lockpicks to $lockpicks - $broken)
    ]
    
    [[Previous Passage->Name of Previous Passage]]
    (if: $lockpicks <= 0)[ [[Next Passage->Name of Next Passage]]]
    
  • edited March 2016
    Somehow I have 2 passages showing when they're should be one.

    I have done exactly as you asked, so not sure where I am going wrong.
    I have added (set: $lockpicks to 10) in the passage before the Lockbox part, everything works apart from the passages as it shows 2 instead of 1.

    Say: Trainer->Trainer as your previous passage and The Robbery->The Robbery as your next passage. Both of them show up regardless.
  • Here are both screenshots of the same passage, one in Twine and one in Preview.
  • note: The example I gave previously contained two variables:

    1. $lockpicks: The number of unbroken lock-picks the character currently has available to them.
    2. $broken: The number of lock-picks the character broke trying to unlock something.

    There are a number of issues in your example:

    a. Printing the current value of a variable on a page:
    You eventually you find the easiest box to open after $lockpicks broken lockpickes.
    When you print the current value of a variable on a page, the value shown on the page will not change if you later change the variable's value. So you need to change the variable's value before you print it on the page.

    b. Printing the number of lock-picks available instead of the number that were broken:
    You eventually you find the easiest box to open after $lockpicks broken lockpickes.
    You should use the $broken variable if you want to tell the Reader how many they broke while trying to open the boxes.

    c. Both markup links are showing:
    Somehow I have 2 passages showing when they're should be one.
    .....
    Training with Sylvia->Training with Sylvia
    (if: $lockpicks <= 10)[The Grand Hignway Robbery->The Grand Highway Robbery]
    I am assuming that when you stated "I have 2 passages showing" you really meant "I have 2 maarkup links showning".

    There are two reasons for this:

    1. Your (if:) macro is stating that if the current value of $lockpicks is equal to or less than ten then you want the second markup link to appear. The value of $lockpicks starts as 10 so the (if:) macro's condition is true.

    The equivalent (if:) macro in my previous example compared to zero instead of ten, this is because you stated you only wanted the link to appear if they broke all the lock-picks.

    You can fix the first issue by changing the (if:) macro to something like the following:
    (if: $lockpicks <= 0)[[[The Grand Hignway Robbery->The Grand Highway Robbery]]]
    

    2. The second issue is very similar to that of point A, the (if:) macro only checks the current value of a variable at the time that the (if:) macro is processed so it does not take into account any later changes to the variable's value. So again you need to change the variable's value before you check it within the condition of of a (if:) macro.

    So delete the (set: $broken to (random: 1, 10)) line from your example and move the following code (maybe to start of the passage) so that it appears earlier than any of the related variables are printed or used to conditionally show markup links:
    (if: $lockpicks > 0)[
    (set: $broken to (random: 1, $lockpicks))
    (set: $lockpicks to $lockpicks - $broken)
    ]
    

    d. Centring the text of all the passage's within a Story:
    There is a simpler way to centre all the passage text of your Story instead of using (align: "=><==") again and again. Place the following CSS within your Story Stylesheet area:
    tw-passage {
    	text-align: center;
    }
    

    e. Displaying a horizontal line in a Passage:
    One simpler way to this is to use a HTML hr element, as shown in the following example.
    Text within a passage that appears above a horizontal line.
    <hr>
    Text within a passage that appears below a horizontal line.
    
  • edited March 2016
    I am testing this following code out to see if the $broken and $lockpicks have any effect on it, so if the lockpicks broken say between 1 or 9, the first option shows, but if it says 10 the 2nd option shows.

    (if: (random: 1, 10) <= 9)[
    The Grand Highway Robbery](else:)[ Training with Sylvia ]

    ^ I got that random code to work, but isn't affected by the number of lockpicks, I have been testing it out, so somehow that code is affected by the lockpicks.
  • I have shortened my Story down by making a new Story and copying the passages over, so you can check the code, as I don't know where I am going wrong.
  • Never mind... it randomly worked somehow with this code:

    (align: "=><==")[(if: $lockpicks >= 1)[ The Grand Highway Robbery](else: $lockpicks <= 0)[ Training with Sylvia] ]
Sign In or Register to comment.