Howdy, Stranger!

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

OnClick events making paths visible

Hey, I am using the default mode for Twine and would  like to make bombs, but I dont know how two make the following thing: Someone has to click on my custom button, then a variable is counted down and you can enter a new field OR, if the variable is already 0,it should say something like"you will have to buy a bomb!"Can someone help me with an idea? Iam not that experienced with JavaScript jet sadly.

Comments

  • I'm assuming since you're in the "Help! for 2.0" forum, you mean default for Twine 2.0, which is Harlowe? In which case, you don't need any JavaScript at all. Something like this should work:

    Do you want to make a bomb?

    [Yes]<count|

    (click: ?count)[{
    (if: $makebomb > 0)[
    (set: $makebomb to it - 1)
    [[Go make the bomb]]
    ]
    (else:)[
    Sorry! You'll have to buy a bomb!
    ]
    }]
    In other words, you need to make your custom button what's called a hook. Then you attach a tag to identify it.
    [This is a hook]<tag|
    Then you use the "click" macro - referencing the name on the tag - to check the variable and decrease it if necessary. The code above assumes you've set the variable in a previous passage and can reference it; if you haven't, it'll throw an error.

    I highly recommend checking out the Harlowe documentation if you haven't already done so!

    Also: if you're using Twine 1, you still shouldn't need to use any JavaScript to do this. I'm not familiar with Twine 1 syntax, however.
  • No, I mean something like the attatchment. Can someone please fill out the "missing line"?
  • Masterfox, I took a quick look at your file, and I'm a little confused.

    I think what you want is the following:
    [list type=decimal]
    A passage that contains a "yes" and a "no" link.
    When clicking "no", the user goes to the "no" passage. Normal Twine stuff, right?
    When clicking "yes", Twine checks to see if the counter variable is 0.
    If the counter variable is 0, Twine sends them to a passage or displays text that says something like "Sorry, you don't have any bombs"
    If the counter variable is greater than 0, Twine sends them to a passage that says something like "you used a bomb" and decrements the counter variable by 1.
    If that's correct, InspectorCaracal's example code actually does that, you just have to change "[[Go make the bomb]]" and "Sorry! You'll have to buy a bomb!" to whatever text or links you want.
  • @Masterfox

    The following is a modified version of @InspectorCaracal's solution changed to be based on the example in your HTML file.
    note: the (set: $bombs to 0) line is only need to test the code, change it to (set: $bombs to 1) to see what happens when you have a bomb.

    (set: $bombs to 0)

    Use a bomb here? |bombcheck>[Yes] [[No|Example]]
    (click: ?bombcheck)[(if: $bombs is 0)[you will have to buy a bomb!] (else:) [(set: $bombs to it - 1)(goto: "New Field")]]
    Here is copy of the above (click:) macro formatted so it is easier to read, and to see that is happening:

    (click: ?bombcheck)[
    (if: $bombs is 0)[
    you will have to buy a bomb!
    ]
    (else:) [
    (set: $bombs to it - 1)
    (goto: "New Field")
    ]
    ]
    One thing you may of noticed is that if you click the Yes link when you have no bombs the No link is still clickable, the following code contains a fix for that:

    (set: $bombs to 0)

    Use a bomb here? [|bombcheck>[Yes] or [[No|Example]]]<options|
    (click: ?bombcheck)[(if: $bombs is 0)[(replace: ?options)[Yes]you will have to buy a bomb!] (else:) [(set: $bombs to it - 1)(goto: "New Field")]]
    Again here is copy of the above (click:) macro formatted so it is easier to read, and to see that is happening:

    (click: ?bombcheck)[
    (if: $bombs is 0)[
    (replace: ?options)[Yes]
    you will have to buy a bomb!
    ]
    (else:) [
    (set: $bombs to it - 1)
    (goto: "New Field")
    ]
    ]
    The difference is the (replace:) macro, which being used to remove the unwanted No link.
Sign In or Register to comment.