Simultaneous timers

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
akemicchi
Veteran
Posts: 465
Joined: Mon Dec 31, 2007 11:22 pm
Projects: Magicians of Delphine, Panaderia: Ensaimada, SweetTooth!, XOXO Droplets
Deviantart: littlebabyshoes
Contact:

Simultaneous timers

#1 Post by akemicchi »

I'm using a screen which uses a timer that decreases one unit's HP every x seconds. If the HP of the target drops to 0, the player jumps to a new label.

It works. One unit's HP gets chipped off before jumping to a new screen when the HP reaches 0. What I want to do is have two timers run at the same time: one for the current unit and one for the target. I want both units to have their HP decrease at different rates until one of their HP drops to 0.

Code: Select all

screen timers:
    timer current_unit_turn.SpeedCalculation() action [ChipAtHP(current_unit_turn, target), If(WinningConditions("Killed Enemy"), Jump("won_battle"))] repeat True
    timer target.SpeedCalculation() action [ChipAtHP(target, current_unit_turn), If(LosingConditions("Killed"), true=Jump("lost_battle"))] repeat True
I noticed that between the two timers, the faster one is always used. The other timer doesn't seem to start at all, because the HP doesn't change. Is there any way I can get both timers run at the same time? If it's not possible, what are some alternatives? I'm guessing if I can't do this, I could just calculate who'll win and use AnimatedValue to stimulate a bar steadily decreasing... Hmm.

Any help is appreciated. Thanks for your time! :)

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Simultaneous timers

#2 Post by xela »

There are too many possible approaches here, I suppose the most obvious one without changing code structure is creating a function that chips away at both hps at the same time on conditions to handle the rate.
Like what we're doing? Support us at:
Image

Asceai
Eileen-Class Veteran
Posts: 1258
Joined: Fri Sep 21, 2007 7:13 am
Projects: a battle engine
Contact:

Re: Simultaneous timers

#3 Post by Asceai »

Simultaneous timers work fine.

I don't know what the problem is because I lack all the information (specifically, the implementations of ChipAtHP, WinningConditions and LosingConditions) but I have a feeling things aren't happening in exactly the way you think they are happening.

It may be helpful to know that the If condition is run when the screen is evaluated. So you can't do something like this and have it work as expected:
[SetVariable("hp", hp-1), If(hp == 0, Jump("Dead"))]
This is because the hp == 0 check is made before hp is decremented. Now, it will work the time after hp reaches 0 - SetVariable does a renpy.restart_interaction() each time it is run, which causes the screen to be re-evaluated - but not the time hp actually reaches 0. I don't know that ChipAtHP does the same restart_interaction or other factors in its implementation, but I don't think the problem is simultaneous timers.

User avatar
akemicchi
Veteran
Posts: 465
Joined: Mon Dec 31, 2007 11:22 pm
Projects: Magicians of Delphine, Panaderia: Ensaimada, SweetTooth!, XOXO Droplets
Deviantart: littlebabyshoes
Contact:

Re: Simultaneous timers

#4 Post by akemicchi »

So I shut off my computer, took a break, and launched the game when I came back, and the timers work properly now! I'm not sure what happened-- all the changes I tried and made didn't solve anything as far as I could tell. I resorted to using one timer and made ChipAtHP reduce both unit's HP at the same time, so I could play around with the amount that ChipAtHP reduces. I thought I'd try to solve the mystery of the timers again, so I un-commented the other timer. They both worked!

... If I had to make a guess why, though, I think the timer didn't work because a unit's HP would drop to 0 before the other timer could actually run their action. It'd make sense why messing with the amount to chip away to lower numbers (and therefore prolonging the screen) worked. Haha... oops. I could have sworn the screen lasted long enough to get the both timers to run, though!

That's helpful to know, Asceai! I was wondering why sometimes HP would be able to reach 0 (and even negative numbers) before the game jumped anywhere. I thought everything in the list ran in order?

Still, thank you very much for the alternative, xela, (my head was stuck on the timers and refused to think of any alternatives, haha) and thank you both your time and help! :)

Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot]