Howdy, Stranger!

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

Adding to a Variable (Harlowe)

I’m trying to add to variable inside a passage by using a click macro. (Part of a character creation system I’m working on.) This is what I have so far;

(set: $Health to 8)
Health = $Health [Health +1]<Health1|

(click: ?Health1)[
(set: $Health to $Health +1)
]

So when Health + 1 is clicked the following function happens and is supposed to add + 1 to $Health. However when Health +1 is clicked the value of $Health does not change. Does anyone know what I might be missing?

Thanks!

Comments

  • When you write code like Health = $Health what you are actually doing is appending the current value contained within the variable to the HTML output/page being generated for the passage's content, and by default the passage's output is only generated once per viewing of the passage.

    This means that any changes to the variable's value will not be seen on the current page unless you cause either the page or parts of it to be updated, and you can use macro's like the (replace:) macro to do this.
    (set: $Health to 8)
    Health = [$Health]<health| [Health +1]<Health1|
    
    (click: ?Health1)[
    (set: $Health to $Health +1)
    (replace: ?health)[$Health]
    ]
    
  • Fantastic, thanks a bunch!
  • I have a follow up question to do with the replace macro Greyelf you have the time :)

    I'm new to twine so I'm sure the answer is once again very simple, I’m missing the correct syntax probably, but I’m currently trying to replace Health +1 when it is clicked with the same clickable piece of text. Essentially allowing the player to allocate points into health and later on other statistics. So far, the code will replace Health +1 with text but it will not be clickable or in this case it simply disappears.

    I'm thinking the code needs a line of logic that will make the clickable button appear only if there if there is a certain amount of attribute points but I'm not sure if twine will recognize that's what I'm trying to do?
    (set: $Health to 8)
    (set: $Attripoints to 8)
    
    Attribute Points = [$Attripoints]<attripoints|
    
    Health = [$Health]<health| [Health +1]<Health1|
    
    (click: ?Health1)[
    (if: $Attripoints is 8)[
    (set: $Health to $Health +1)
    (set: $Attripoints to $Attripoints -1)
    (replace: ?health)[$Health]
    (replace: ?attripoints)[$Attripoints]
    (replace: ?Health1)[?Health1]]
    ]
    

    Any suggestions?
  • As explained in this comment in the twine2/Harlowe how to 'reanimate' the click:ed hook thread the (click:) related macros and most of the (link:) macros are single use only, that comment also included information about how to solve the issue using those macro.

    Since the above meantioned solution was written a [url="http://"](link-repeat: ) macro[/url] has been added to Harlowe.
    (set: $Health to 8)
    (set: $Attripoints to 8)
    
    Attribute Points = [$Attripoints]<attripoints|
    
    Health = [$Health]<health| {(link-repeat: "Health +1")[
    (if: $Attripoints is 8)[
    (set: $Health to $Health +1)
    (set: $Attripoints to $Attripoints -1)
    (replace: ?health)[$Health]
    (replace: ?attripoints)[$Attripoints]
    ]]}
    
    note: I needed to add Collapsing whitespace markup to the above to stop the page becoming a little longer each time the link was clicked, the increase in page length was due to the line-breaks on the end of each statement within the (click:)/(link-repeat:) macro's associated hook.
  • edited July 2016
    Works great! Thanks so much for the responses! In fact I now have the *buttons* working exactly as intended, you can now click on a plus and minus button only if you have the correct amount of attribute points. However.. the page does grow longer and longer each time as it creates code in response. >.>

    (set: $Health to 8)
    (set: $Attripoints to 8)
    
    (set: $Apointsplus to true)
    
    Attribute Points = [$Attripoints]<attripoints|
    
    Health = [$Health]<health| {(link-repeat: "Health +1")[
    (if: $Attripoints >= 1)[(set: $Apointsplus to true)]
    (if: $Attripoints <= 8)and(if: $Apointsplus is true)[
    (set: $Health to $Health +1)
    (set: $Attripoints to $Attripoints -1)
    (replace: ?health)[$Health]
    (replace: ?attripoints)[$Attripoints]
    (if: $Attripoints is 0)[(set: $Apointsplus to false)]]]
    (link-repeat: "Health -1")[
    (if: $Attripoints < 8)[
    (set: $Health to $Health -1)
    (set: $Attripoints to $Attripoints +1)
    (replace: ?health)[$Health]
    (replace: ?attripoints)[$Attripoints]
    ]]}
    

    I looked at the collapsing whitespace markup and that does seem to work for a the code I have already but doesn't include code that is created afterwards =/



Sign In or Register to comment.