0 votes
by (310 points)
edited by

It doesn't show in the footer at all, so I know I must be doing something wrong (and it's probably something really simple, knowing my luck).

I've got variables called $elapsedtime and $showpotion set to 0 and 1 respectively in the "init" passage, as well as a $healpot variable to track inventory (that part I have working).

in the footer, I've got the following code:

<!-- where to show the potion
|usehealpot>[]

<!-- when to display the health potion
(live: 1s)[(set: $elapsedtime to it + 1)]
(if: $elapsedtime is $showpotion)
	(replace: ?usehealpot)[
		(link: Drink a health potion.)[
			(if: $healpot < 1)[Your vials are empty!"]
			(else-if:)[
				(set: $hitpopints to it + 10)
				(set: $healpots to it - 1)
				(set: $showpotion to $elapsedtime + 10)]]]

 But when I test, nothing shows.  I figure I'm probably using (live:) wrong, but I can't come up with how.

EDIT:  I did get this to show up once, by moving actual potion section to a passage other than the footer.  However, it only worked one time, and I can't figure out what was different about that one time.

2 Answers

0 votes
by (2.7k points)

Hi sirjohnthetall,

could you try if this works for you:

<!-- where to show the potion -->
|usehealpot>[]

<!-- when to display the health potion -->
(live: 5s)[(set: $elapsedtime to it + 1)]
(if: $elapsedtime is $showpotion)[
	(replace: ?usehealpot)[
		(link: "Drink a health potion.")[
			(if: $healpot < 1)[Your vials are empty!"]
			(else:)[
				(set: $hitpopints to it + 10)
				(set: $healpots to it - 1)
				(set: $showpotion to $elapsedtime + 10)]]]]

What has changed:

- added javascript comment endings
- added missing quotes in  (link: "Drink .... potion.")
- wrapped whole 'if-elapsedtime-is-showoption' body with [ resp. ]
- changed (else-if:) to (else:)

By the way, Harlowe wants the init passage to be tagged with 'startup' - the name StoryInit does not have any effect in Harlowe (I first did this wrong, therefore mentioned here).

Hope, all that helps a bit.

Regards, Jherek

 

 

by (310 points)
Thanks Jherek.  I did know about the "startup" tag for the "init" passage, so no worries on that.  :)

Made the changes you suggested, a couple of them being my own mistake.  Not putting brackets on the whole initial "if" was a big whoops.

Still not showing in the footer though.  My text does blink every few second, now, so I know it's doing something.  Just not what I'm trying to get it to do.  lol
by (2.7k points)

Thank you for the reply, I'm sorry, that this did not work for you.

I forgot to mention which versions I've tested that with: online Twine2 2.2.1 in Firefox with Harlowe 2.1.0

For reproducing the code, I had 3 passages:

Start

Start Root
(print: $elapsedtime)
(print: $showoption)

StoryInit (tagged 'startup')

(set: $showoption to 1)
(set: $elapsedtime to 0)

F2 (tagged 'footer')

<!-- where to show the potion -->
|usehealpot>[]

<!-- when to display the health potion -->
(live: 1s)[(set: $elapsedtime to it + 1)]
(if: $elapsedtime is $showpotion)[
	(replace: ?usehealpot)[
		(link: "Drink i bit a health potion.")[
			(if: $healpot < 1)[Your vials are empty!"]
			(else:)[
				(set: $hitpopints to it + 10)
				(set: $healpots to it - 1)
				(set: $showpotion to $elapsedtime + 10)]]]]

 

So when I create this in actual twinery.org/2 

then 'play' works so far, as it shows

Start Root
0
1


Drink i bit a health potion.

without any delay. And after clicking on the link it turns to

Start Root
0
1



Your vials are empty!"

 

So I wonder whether you could reproduce this as it would prove that your code was useable with a few typos corrected only.

If it does not work, it might be of the different story versions 2-0-1 vs 2.1.0 ?

 

 

 

by (63.1k points)
See my comment on the other answer for why I think this isn't working. Note that you're testing with 0 potions, so you aren't actually testing it for the cooldown itself, which is the main point of the question from what I can tell.

Also you misspelled $showpotion.
+1 vote
by (63.1k points)

Here's a working implementation.

:: init [startup]
(set: $healpot to 3, $hitpoints to 5)

:: potion
{
(if: $healpot > 0)[
	(link-repeat: 'Use a potion!')[
	    (replace: ?healthpot)[]
		(live: 5s)[
			(stop:)
			(replace: ?healthpot)[(display: 'potion')]
		]
		(set: $hitpoints to it + 10,
		    $healpot to it - 1)
	]
](else:)[
	Your vials are empty!
]
}

:: use potion [header]
|healthpot>[(display: 'potion')]

Note that this code is in Twee format: the :: represents a passage, and the [] on the passage name line represents the tags.

by (310 points)
If I'm being honest, when I read the code, I didn't think it would work, but it does seem to be working.  It's certainly a different approach than what I used, and there is nothing at all wrong with that.  All told, I really expected something like that to have to be more complicated.  Thank you very much, Chapel.
by (63.1k points)

I have a bit more time to explain. 

There was probably a way to make it work the way you were originally going about it, but I think this way is actually a little simpler / safer (meaning less bug and error prone). I'm using a programming concept called recursion, which is when a chunk of code (a function, subroutine, or in this case, a passage) calls itself. 

In your original code and in jherek's code, there are a few issues: 

  • Performance. Your live macros are constantly running. Running it only as needed ensures it takes up only the processing power and memory it needs to do its job. 
  • Non-dynamic. The (if:) statements exist outside of your live macros, so they're only reassessed when passage transition occurs, which is usually not suitable for a cooldown. I imagine this is unintentional in both cases. 
  • Imprecise. Since your live macros only add seconds to the timer, and the timer is only updated on each second, your code will always be fairly imprecise. If the player clicks on a half-second, they'll need to wait an extra half-second for the code to run. Having the live macro start up in response to the player means it's going to be exactly X seconds each time. 
  • Infinite counters. You should be resetting $elapsedtime to 0, not adding 10 to the target time. Having two variables counting up forever is usually a bad idea when you can avoid it. 
  • Jherek's version specifically has a very long (50-ish seconds) cooldown since his live macro runs in 5s intervals, counts up by 1, and raises the target by 10. I imagine this is also unintentional. 
This site has been placed in read-only mode. Please use the Interactive Fiction Community Forum instead.
...