Renpy keypressed event [Solved]

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
Raalkenzo
Newbie
Posts: 3
Joined: Fri May 10, 2019 8:59 am
Contact:

Renpy keypressed event [Solved]

#1 Post by Raalkenzo »

Hello,

I am a beginner with Renpy and I have a question about the keypressed event.
What I want to do is (with Screen language), return 1 when the key pressed is the correct one and return 0 otherwise.

I managed to complete the first condition with "key trigger_key action Return(1)" but I don't know how to return 0 when the player do not hit the right key.

Can you help me ?
Many thanks
Last edited by Raalkenzo on Sun May 12, 2019 10:20 am, edited 1 time in total.

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Renpy keypressed event

#2 Post by Per K Grok »

Raalkenzo wrote: Fri May 10, 2019 9:04 am Hello,

I am a beginner with Renpy and I have a question about the keypressed event.
What I want to do is (with Screen language), return 1 when the key pressed is the correct one and return 0 otherwise.

I managed to complete the first condition with "key trigger_key action Return(1)" but I don't know how to return 0 when the player do not hit the right key.

Can you help me ?
Many thanks
I am not sure if this is helpful to you.
The code below will get you a screen that if you press key 'x' will set the variable testV to 1
and if you press key 'c' or 'z' will set the variable to 0. You can of course add more keys.
You should note that a lot of keys already have default functions in Ren'Py.

Code: Select all

default testV=0

screen keyTest():
    text str(testV)
    key "x" action SetVariable("testV",1)
    key "c" action SetVariable("testV",0)   
    key "z" action SetVariable("testV",0)   
I do wonder what 'trigger_key' in your code is. Don't seem to be a key word. A variable? If so where does the value for that variable come from?
What are Return supposed to do? Return will send you back to main page in Ren'py, or if you have called a label, back to the point the call was made from.
Unless you use it in a function.

Raalkenzo
Newbie
Posts: 3
Joined: Fri May 10, 2019 8:59 am
Contact:

Re: Renpy keypressed event

#3 Post by Raalkenzo »

Hello,

Thanks for you answer.
So there is no other way to "select" all the other keys except the one which is pressed ?
I guess I'll only do it with some keys then.
I do wonder what 'trigger_key' in your code is. Don't seem to be a key word. A variable? If so where does the value for that variable come from?
What are Return supposed to do? Return will send you back to main page in Ren'py, or if you have called a label, back to the point the call was made from.
Unless you use it in a function.
Yes, I use trigger_key as a variable. The variable comes from another piece of code in the script which generates a random key in a pre-defined list (ex : choose a random letter between [a,b,c])

I use Return as a function in order to know if the correct key has been pressed. In fact, I followed this "tutorial" :

viewtopic.php?t=50766

I use the "screen qte_keyboard" thing.

Also, sorry if my explainations are bad, english is not my native language but I try to be as clear as possible.

User avatar
Per K Grok
Miko-Class Veteran
Posts: 882
Joined: Fri May 18, 2018 1:02 am
Completed: the Ghost Pilot, Sea of Lost Ships, Bubbles and the Pterodactyls, Defenders of Adacan Part 1-3, the Phantom Flyer
itch: per-k-grok
Location: Sverige
Contact:

Re: Renpy keypressed event

#4 Post by Per K Grok »

Raalkenzo wrote: Sat May 11, 2019 1:55 pm Hello,

Thanks for you answer.
So there is no other way to "select" all the other keys except the one which is pressed ?
I guess I'll only do it with some keys then.

-----

As far as I know the key events from the keyboard are that limited in Renpy.

One possibility to get more options is to import the pygame library and use the key events functions in that library.

But perhaps that is not necessary. It might depend on what problem you are looking to solve.

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

Re: Renpy keypressed event

#5 Post by Alex »

Raalkenzo wrote: Fri May 10, 2019 9:04 am ...What I want to do is (with Screen language), return 1 when the key pressed is the correct one and return 0 otherwise.

I managed to complete the first condition with "key trigger_key action Return(1)" but I don't know how to return 0 when the player do not hit the right key. ...
As Per K Grok said it might depend on what problem you are looking to solve. Also, you could provide the code you have so far...

Anyway, this is a possible solution of an issue (as I get it)

Code: Select all

screen my_qte(keys):
    text right_key size 55 align(0.5, 0.05)
    for k in keys:
        key k action If(k==right_key, Return(1), Return(0))

label start:
    "..."
    $ right_key = None
    $ my_seq = "asdfg"
    show screen my_qte(my_seq)
    
    label qte_loop:
        $ right_key = renpy.random.choice(my_seq)
        $ res = ui.interact()
        "[res]"
        jump qte_loop

Raalkenzo
Newbie
Posts: 3
Joined: Fri May 10, 2019 8:59 am
Contact:

Re: Renpy keypressed event

#6 Post by Raalkenzo »

What I want to do is a QTE which stops under 3 conditions :
- 5 success (5 keys hit correctly)
- on the first wrong key pressed
- timer runs out

For now, I selected the keys individually. Here is my code :

Code: Select all


screen qte_keyboard:
    #key input qte

    timer interval repeat True action If(time_start > 0.0, true=SetVariable('time_start', time_start - interval), false=[Return(0), Hide('qte_keyboard')])
    # timer, using variables from label qte_setup
    # false is the condition if the timer runs out - and this will be reached if the user doesn't get hit the key on time

    key trigger_key action Return(1)
    key "r" action Return (0)
    key "q" action Return (0)
    key "s" action Return (0)
    key "d" action Return (0)
    key "f" action Return (0)
    
    if(trigger_key != "a") :
        key "a" action Return (0)
    elif(trigger_key != "z") :
        key "z" action Return (0)
    elif(trigger_key != "e") :
        key "e" action Return(0)
    
    frame :
        background "#000"
        xalign x_align
        yalign y_align
        vbox:
            
            spacing 25
            # vbox arrangement

            text trigger_key:
                xalign 0.5
                color "#fff"
                size 36
                #outlines [ (2,"#000000",0,0) ]
                # text showing the key to press

            bar:
                value time_start
                range time_max
                xalign 0.5
                xmaximum 300
                if time_start < (time_max * 0.25):
                    left_bar "#f00"
                    # this is the part that changes the colour to red if the time reaches less than 25%

label qte_setup(time_start, time_max, interval, trigger_key, x_align, y_align):

    $ time_start = time_start
    $ time_max = time_max
    $ interval = interval
    $ trigger_key = trigger_key
    $ x_align = x_align
    $ y_align = y_align

    call screen qte_keyboard
    # can change to call screen qte_button to switch to button mode

    $ cont = _return
    # 1 if key was hit in time, 0 if key not

    return

label quickgame1 :
    
    $ cont = 1 #continue variable
    $ arr_keys = ["a","z","e"] 
    $ i = 0
    while cont == 1 and i < 5:
        call qte_setup(0.5, 0.5, 0.01, renpy.random.choice(arr_keys), renpy.random.randint(1, 9) * 0.1, renpy.random.randint(1, 9) * 0.1)
        # to repeat the qte events until it is missed
        $ i=i+1
        "{b}%(i)s{/b}"
    if cont == 0 :
        jump route1
    else :
        jump route2
    return

Since it's not the main part of my game, I think it's ok to leave it this way but I just wanted to know if there were other solutions.

I looked at the pygame's documentation and it seems to have a function to manage this issue.
I will test your code later too Alex.

Thanks for your time. I will set the topic as "solved" since there I found a solution.

Post Reply

Who is online

Users browsing this forum: Bing [Bot]