[solved] trigger vpunch and hpunch from python?

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.
Message
Author
User avatar
Kia
Eileen-Class Veteran
Posts: 1039
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

[solved] trigger vpunch and hpunch from python?

#1 Post by Kia »

Is it possible to trigger vpunch and hpunch from a python function?
something akin to:

Code: Select all

def hit():
    health -= 10
    renpy.vpunch()
Last edited by Kia on Thu Aug 25, 2022 3:20 pm, edited 1 time in total.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: trigger vpunch and hpunch from python?

#2 Post by Ocelot »

Those are transitions, so, like every other transition:

Code: Select all

renpy.with_statement(vpunch)
< < insert Rick Cook quote here > >

philat
Eileen-Class Veteran
Posts: 1900
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: trigger vpunch and hpunch from python?

#3 Post by philat »

It's a transition, so with. https://www.renpy.org/doc/html/statemen ... _statement

ETA: Ninja'ed, damn. ;)

User avatar
Kia
Eileen-Class Veteran
Posts: 1039
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: trigger vpunch and hpunch from python?

#4 Post by Kia »

It does work , but I I'm getting:

Code: Select all

Exception: Cannot start an interaction in the middle of an interaction, without creating a new context.
I assume it's because I have a screen called and present.
tried:

Code: Select all

renpy.invoke_in_new_context(renpy.with_statement(vpunch))
tried:

Code: Select all

renpy.call_in_new_context(shake)
label shake:
	with vpunch
	return
it hides the screen while shaking.

before resorting to writing a transition and shake the screen with some variables and conditions, do you know of any easy way out?

User avatar
Kia
Eileen-Class Veteran
Posts: 1039
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: trigger vpunch and hpunch from python?

#5 Post by Kia »

here's the narrowed down problem for testing

Code: Select all

init python:
    def do_a_punch():
        renpy.with_statement(vpunch)
screen test_vpunch:
    modal True
    key "w" action Function(do_a_punch)

label start:
    show screen test_vpunch
    pause
does the same with `call screen`

User avatar
m_from_space
Miko-Class Veteran
Posts: 939
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: trigger vpunch and hpunch from python?

#6 Post by m_from_space »

The following does work when calling it as part of a screen with Call('do_a_punch'), and also if you call it with renpy.call("do_a_punch") as part of a function.

Code: Select all

label do_a_punch:
    with vpunch
    return

User avatar
Andredron
Miko-Class Veteran
Posts: 700
Joined: Thu Dec 28, 2017 2:37 pm
Location: Russia
Contact:

Re: trigger vpunch and hpunch from python?

#7 Post by Andredron »

$ renpy.vibrate(1.0)


init python:
...def vibrate():
.......renpy.vibrate(1.0)

textbutton 'xuy' action [Function(vibrate), Jump("metka")]

https://www.renpy.org/doc/html/other.html#renpy.vibrate

User avatar
m_from_space
Miko-Class Veteran
Posts: 939
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: trigger vpunch and hpunch from python?

#8 Post by m_from_space »

Andredron wrote: Fri Jul 15, 2022 11:31 pm $ renpy.vibrate(1.0)
This is something completely different. renpy.vibrate() is about making your Android phone vibrate, if supported. The question was about making the screen visually shake using vpunch. And as I wrote, calling a label (either via a screen or a python function) works without problems.

User avatar
Kia
Eileen-Class Veteran
Posts: 1039
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: trigger vpunch and hpunch from python?

#9 Post by Kia »

I've finally got time to try this issue again.
Thank you m_from_space, it does work, but the issue of hiding the screen for the duration of the punch remains. I think I'll suggest this one as an additional feature that can be added.

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: trigger vpunch and hpunch from python?

#10 Post by Ocelot »

Ah... it is within screen. That slightly changes everything.
1) You cannot start an interaction (which with statement does, as pointed in documentation) inside of another interaction.
2) You can queue transition to start on next interaction, if you cannot initiate one right now.
3) You cannot start an interaction from within interaction, but you can restart it.

Combine 2 and 3 and you get:

Code: Select all

def do_a_punch():
    renpy.transition(vpunch)
    renpy.restart_interaction()
https://www.renpy.org/doc/html/other.ht ... transition
Or throw function away and use standard action for that: key "w" action With(vpunch). As it turns out, its code is identical to the function I provided.
< < insert Rick Cook quote here > >

User avatar
m_from_space
Miko-Class Veteran
Posts: 939
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: trigger vpunch and hpunch from python?

#11 Post by m_from_space »

Kia wrote: Thu Aug 25, 2022 3:48 am I've finally got time to try this issue again.
Thank you m_from_space, it does work, but the issue of hiding the screen for the duration of the punch remains. I think I'll suggest this one as an additional feature that can be added.
What screen is hiding during the vpunch? Nothing is hiding on my end calling that label.

User avatar
Kia
Eileen-Class Veteran
Posts: 1039
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: trigger vpunch and hpunch from python?

#12 Post by Kia »

the screen that calls the function.

Code: Select all

init python:
    def do_a_vpunch():
        renpy.call("do_a_punch")

screen vpunch_test:
    modal True
    button:
        text "Punch"
        action Function(do_a_vpunch)

label do_a_punch:
    with vpunch

label start:
    call screen vpunch_test

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: trigger vpunch and hpunch from python?

#13 Post by Ocelot »

Kia wrote: Thu Aug 25, 2022 6:28 am the screen that calls the function.
Calling a label stops current interaction. Called screens are transient: they are automatically hidden at the end of interaction. Move transition (and vpunch as a subset of it) use only new_widget, so if a screen is hidden between interactions, it is not visible buring transition.
You either need to keep screen shown between interactions, or to restart interaction, not end it, as I shown before.

Example:
https://i.imgur.com/8UhtY52.gif
< < insert Rick Cook quote here > >

User avatar
Alex
Lemma-Class Veteran
Posts: 3090
Joined: Fri Dec 11, 2009 5:25 pm
Contact:

Re: trigger vpunch and hpunch from python?

#14 Post by Alex »

Kia wrote: Thu Aug 25, 2022 6:28 am ...
Screens can be shown using transition. Since 'vpunch' affects the whole screen, you can show some empty screen using it. Like

Code: Select all

screen test_scr():
    textbutton "ClickMe!" action Show("shake_scr", vpunch) align(0.5, 0.5)
    
screen shake_scr():
    timer 0.5 action Hide("shake_scr")
        
# The game starts here.

label start:
    "..."
    show screen test_scr
    "... ..."
    hide screen test_scr
    "___"
    call screen test_scr
    "?!"
Also, it's better to put game logic outside the screen (in a game loop). Otherwise try to create and use some transforms.

https://www.renpy.org/doc/html/screen_actions.html#Show

User avatar
m_from_space
Miko-Class Veteran
Posts: 939
Joined: Sun Feb 21, 2021 3:36 am
Contact:

Re: trigger vpunch and hpunch from python?

#15 Post by m_from_space »

Kia wrote: Thu Aug 25, 2022 6:28 am the screen that calls the function.
Don't call the screen, but show it. You can hide it whenever you want anyway. Everything works fine this way.

Code: Select all

label start:
    show screen vpunch_test

Post Reply

Who is online

Users browsing this forum: Bing [Bot]