How to make player lose health if they view a screen?

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
AliceGames
Newbie
Posts: 13
Joined: Fri Jul 23, 2021 10:06 am
Contact:

How to make player lose health if they view a screen?

#1 Post by AliceGames »

Hi! I'm not sure if this is something I can do, but I thought I'd try.
In my game, the player loses blood over actions and fights, and the rule is that they cannot know their blood levels unless they check them, by clicking on "Blood Levels". The catch is, they lose blood in the process, how can I make this happen?
Here's the code I have right now, but it's probably so wrong, lol

Code: Select all

screen bloodlevels:
    text "Blood Left: [currenthp]/[maxhp]" xalign 0.0 yalign 0.05
    bar value currenthp range maxhp xalign 0.0 yalign 0.1 xmaximum 200
    $currenthp -= 40
    if currenthp <= 0:
        action Jump ("death")
And at the start of my script, I define how much the player has, and how much the player can have, maximum. AGAIN this probably looks totally dumb, please be patient I am sooo new at this and I'm just trying to learn ;u;

Also don't mind the screen look right now, I'm going to customize it once I figure out how to get the result I want.

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3784
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: How to make player lose health if they view a screen?

#2 Post by Imperf3kt »

You should take the python out of the screen and instead add it as an action when the screen is opened. One button can run multiple actions by separating them with a comma.

Also would be worth adding () to the end of your screen as this is needed for optimisation.
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

AliceGames
Newbie
Posts: 13
Joined: Fri Jul 23, 2021 10:06 am
Contact:

Re: How to make player lose health if they view a screen?

#3 Post by AliceGames »

Imperf3kt wrote: Fri Jul 23, 2021 6:08 pm You should take the python out of the screen and instead add it as an action when the screen is opened. One button can run multiple actions by separating them with a comma.

Also would be worth adding () to the end of your screen as this is needed for optimisation.
Ok! Totally cool if you can't answer, but what would that look like? Thank you again for the reply

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: How to make player lose health if they view a screen?

#4 Post by hell_oh_world »

Code: Select all

default hp = 100
default hp_max = 100
default hp_min = 0

init python:
  def _setHealth(value, action=[]):
    global hp, hp_min, hp_max
    hp = min(max(value, hp_min), hp_max) # clamp the values
    if hp == 0: renpy.run(action)

  def SetHealth(value, action=[]):
    return Function(_setHealth, value, action)

screen bloodlevels():
  on "show" action SetHealth(hp - 40, Jump("death"))
https://www.renpy.org/doc/html/screens.html#on
EDITED: corrected a minimal mistake. my bad.
Last edited by hell_oh_world on Sat Jul 24, 2021 10:25 am, edited 1 time in total.

rayminator
Miko-Class Veteran
Posts: 793
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Re: How to make player lose health if they view a screen?

#5 Post by rayminator »

I think he/she want to automatically drop while the player sit in that screen like every five to ten seconds they on that screen so should use a timer for that like

something like this

Code: Select all

timer 0.1 repeat True action SetVariable('currenthp', currenthp - 5)

AliceGames
Newbie
Posts: 13
Joined: Fri Jul 23, 2021 10:06 am
Contact:

Re: How to make player lose health if they view a screen?

#6 Post by AliceGames »

hell_oh_world wrote: Fri Jul 23, 2021 8:05 pm

Code: Select all

default hp = 100
default hp_max = 100
default hp_min = 0

init python:
  def _setHealth(value, action=[]):
    global hp, hp_min, hp_max
    hp = min(max(hp, hp_min), hp_max) # clamp the values
    if hp == 0: renpy.run(action)

  def SetHealth(value, action=[]):
    return Function(_setHealth, value, action)

screen bloodlevels():
  on "show" action SetHealth(hp - 40, Jump("death"))
https://www.renpy.org/doc/html/screens.html#on
I tried implementing this code, it shows the screen but no health is taken off

AliceGames
Newbie
Posts: 13
Joined: Fri Jul 23, 2021 10:06 am
Contact:

Re: How to make player lose health if they view a screen?

#7 Post by AliceGames »

rayminator wrote: Fri Jul 23, 2021 9:09 pm I think he/she want to automatically drop while the player sit in that screen like every five to ten seconds they on that screen so should use a timer for that like

something like this

Code: Select all

timer 0.1 repeat True action SetVariable('currenthp', currenthp - 5)
Hi! So this is perfect, but how do I make it to where if the player hits zero they can die from blood loss in that screen?

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: How to make player lose health if they view a screen?

#8 Post by hell_oh_world »

crap, uhhh sorry, there's a little mistake.

Code: Select all

hp = min(max(hp, hp_min), hp_max)
should be...

Code: Select all

hp = min(max(value, hp_min), hp_max)
but ig, rayminator's code is what you're looking for, just want to correct it nonetheless.

rayminator
Miko-Class Veteran
Posts: 793
Joined: Fri Feb 09, 2018 12:05 am
Location: Canada
Contact:

Re: How to make player lose health if they view a screen?

#9 Post by rayminator »

AliceGames wrote: Sat Jul 24, 2021 8:47 am
rayminator wrote: Fri Jul 23, 2021 9:09 pm I think he/she want to automatically drop while the player sit in that screen like every five to ten seconds they on that screen so should use a timer for that like

something like this

Code: Select all

timer 0.1 repeat True action SetVariable('currenthp', currenthp - 5)
Hi! So this is perfect, but how do I make it to where if the player hits zero they can die from blood loss in that screen?
you can try something like this

Code: Select all

screen penistimer( step=0.1, goal=10 ):
    # Increase the counter when a key is pressed
    key "K_space" action SetVariable('penpoint', penpoint + 1)

    # Always decrease the counter, this force the player to press the key many times.
    # And return if the counter goes below the decreasing step.
    timer 0.1 repeat True action If(penpoint > step, SetVariable('penpoint', penpoint - step), Return( False ) )

    # Wait for a winner, returning if you got one, doing nothing else.
    timer 0.1 repeat True action If( penpoint >= goal, Return( True ), NullAction() )

    # Here display the counter the way you want

label whatever:
    $ penpoint = 4
    call screen penistimer( step=0.2, goal=20 )
    if _return:
        "Congratulation, you won."
    else:
        "We found a looser."

AliceGames
Newbie
Posts: 13
Joined: Fri Jul 23, 2021 10:06 am
Contact:

Re: How to make player lose health if they view a screen?

#10 Post by AliceGames »

hell_oh_world wrote: Sat Jul 24, 2021 10:25 am crap, uhhh sorry, there's a little mistake.

Code: Select all

hp = min(max(hp, hp_min), hp_max)
should be...

Code: Select all

hp = min(max(value, hp_min), hp_max)
but ig, rayminator's code is what you're looking for, just want to correct it nonetheless.
I can try this! Honestly any kind of method works, I just want health decreased as a punishment for opening the screen lol, thank you again, I’ll let you know how it goes

AliceGames
Newbie
Posts: 13
Joined: Fri Jul 23, 2021 10:06 am
Contact:

Re: How to make player lose health if they view a screen?

#11 Post by AliceGames »

hell_oh_world wrote: Fri Jul 23, 2021 8:05 pm

Code: Select all

default hp = 100
default hp_max = 100
default hp_min = 0

init python:
  def _setHealth(value, action=[]):
    global hp, hp_min, hp_max
    hp = min(max(value, hp_min), hp_max) # clamp the values
    if hp == 0: renpy.run(action)

  def SetHealth(value, action=[]):
    return Function(_setHealth, value, action)

screen bloodlevels():
  on "show" action SetHealth(hp - 40, Jump("death"))
https://www.renpy.org/doc/html/screens.html#on
EDITED: corrected a minimal mistake. my bad.
Hi! I tried the edited version, and the screen is blank, no errors though.

User avatar
hell_oh_world
Miko-Class Veteran
Posts: 777
Joined: Fri Jul 12, 2019 5:21 am
Contact:

Re: How to make player lose health if they view a screen?

#12 Post by hell_oh_world »

the code was meant to be an example, you're seeing nothing because I just want to show you how to do what you want.
if you want to show it visually, you gonna have to add some displayables to your screen, like the text and bar that you have in your original code.
Those kinds of things depend on your discretion so you gonna have to modify the screen, to make it look how you want it to.

Post Reply

Who is online

Users browsing this forum: Google [Bot]