Howdy, Stranger!

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

Global Variables in Harlowe

Hi all,

I'm trying to create a global variable that can goes up by 1 every second and can be accessed by any passage. I'm wondering if there's anyway for a passage to check if this global variable has reached a certain number every second, so like while I may have arrived at the passage when the variable is 10, I need it to move on to the next one if the variable's now 50, and I need the passage I move to to be able to access that number as well.

Thanks!

Comments

  • edited June 2015
    I can't answer your question for Harlowe, but it's pretty easy to do in SugarCube with the <<replace>> macro set. Maybe someone else will know how to translate this to Harlowe.
    In StoryInit passage:
    
    <<set $stophamster to 0>>
    <<set $hamsterlaps to 0>>
    
    In widgets passage:
    
    <<widget "hamsterwheel">>
    <<if $stophamster is 0>><<timedcontinue 1s>><<set $hamsterlaps to $hamsterlaps + 1>><<hamsterwheel>><</if>>
    <</widget>>
    

    Then you start the <<hamsterwheel>> widget in a passage somewhere, and then it just keeps looping back on itself. To stop it you would <<set $stophamster to 1>>. To start it back up you'd write <<set $stophamster to 0>><<hamsterwheel>>.

    Whenever you want to check it, you do an if statement in a passage like <<if $hamsterlaps is gt 50>><<goto "Passage 50">><</if>>.

    Of course if you wanted to reduce the number of times it updates you could change the timedcontinue value to 10s, and the variable updating to +10, or even make it on a minute timer. Same results in the long run, but you wouldn't be calling a function every second so might be more efficient.
  • I can't answer the full question because I do not know how to anything real time in harlowe, but here is how you could add a global variable: Create a new passage, put the adding variable in there with something like this:

    (set: $variable to it + 1)

    And then, in each passage where you want the variable to "grow"

    (display: "passage with variable")

    This way, you only have to make your changes in one passage. Keep in mind that any space/ linebreak will show up in the other passages.
  • So there's no function in harlowe that checks for clock ticks or anything like that? Basically, I need something to happen at a specific time (about 1:30 after the first passage launches) regardless of which passage I'm at, AND if a specific condition is met.
  • I could only find this

    Times: meant only for use with the time keyword (see below), these are CSS time durations like 3.2s or 1550ms. They actually are converted to milliseconds at runtime, so you can in fact
    (set: $variablename to 1500ms + 5)
    

    time: This keyword represents the amount of time (in milliseconds) that has passed since the current passage was rendered.

    (live:): This macro renders attached hook after the given time passes - write (live: 2s) to make the hook appear after 2 seconds, for instance - but then continues to re-render it every 2 seconds after that, until or unless a (stop:) macro appears inside the hook. This macro can be used in tandem with a number of other macros to create "events" - for instance, you could write {(live:100ms)[ (if: $var is true) [Hey! (stop:)]]} to make the text "Hey!" appear only after the variable $variablename becomes true (which could be performed by another sensor macro such as (click:)).

    here

    You can do this in a separate passage, and then add (display: "passage") at the start of each passage where you want it to be used.
  • I think the problem with this @Rafe is that Harlowe resets the clock each time you move to another passage.

    I have been trying to create a similar effect (e.g. a countdown clock that continues ticking on every passage). While I am able to create the countdown clock and include it by using the (display:) macro, the problem is that all passages are refreshed each time you navigate to another passage, and so navigating to another passage affects the clock, instead of the clock running independently.

    So, I think @cabelhigh is asking if there is a way of running a clock independently of passage navigation, and I think the answer is no, at least not in Harlowe, since there is no option of creating a passage that is not affected by the "refreshing".

    I am probably not explaining it properly. Perhaps @L or @greyelf can explain it better.
  • Maybe a way around this is setting the links between passages to save the "time" variable in a new variable, and have it add to the time in the display passage.
  • Rafe wrote: »
    Maybe a way around this is setting the links between passages to save the "time" variable in a new variable, and have it add to the time in the display passage.

    very clever. This could work.

  • The timers created by using the (live:) macro within a passage are destroyed when the Reader leaves that passage.

    You can create your own timer by using Javascript's setTimeout() or setInterval() function, so that would be one way to get something to happen after a set period of time.

    The problem is that there is currently no documented Harlowe Javascript API, which means no documented way of changing which passage is being shown using Javascript.
  • edited June 2015
    So, I think @cabelhigh is asking if there is a way of running a clock independently of passage navigation, and I think the answer is no, at least not in Harlowe, since there is no option of creating a passage that is not affected by the "refreshing".

    The code I gave for SugarCube is not affected by new passages, so it's a Harlowe only issue. In SugarCube, all timeout macros exist independently to the passage and will continue until stopped.

    I think Harlowe has some deliberately coded brakes that prevent (live:) timeouts being applied past their passage.
  • edited February 2016
    So this is probably too late to help the original poster, but for future people I figured out a way to make a global countdown clock in Harlow with no css or javascript.

    first I created a passage called vars and put this code in it:
    {(live: 0.01s)[
        (set: $elapse to time/1000)
    	(set: $remain to 30 - $elapse + $clockCounter)
    	(set: $clock to $remain.toFixed(2))$clock
    	(if: $remain <= 0)[(go-to: "dead")]
    ]}
    
    (set: $clockCounter to $elapse + $clockCounter)
    

    then I put
    (display: "vars")
    
    on all pages

    It counts down and preserves the timer from page to page.

    the elapse variable counts up
    the remain counts down from 30 seconds in this case
    the clock makes remain look pretty
    and the clockCounter is what keeps the clock going when switching between passages
    dead is a game over passage for when the clock runs out.

    I should note you will need to reset the variables on the death screens, and the back button in the sidebar just messes it up so you should just turn that off, and include your own.

    Hopefully this helps someone.
Sign In or Register to comment.