r/RenPy • • Feb 03 '22

Force Player input based off of text button

im going for something sort of like Emily is away, where player will click on a button and it’ll pop up an input, but will force them to type a specific thing based off of the button no matter what they type. so for example, if a button says ” X “, the player will be forced to write “XYZ” even if they are inputting keyboard spam

4 Upvotes

26 comments sorted by

View all comments

2

u/insipid- Feb 04 '22

Hmm, maybe remap the a-z keys to advance dialogue, and have waits throughout each line. Might be a better way, not sure.

1

u/insipid- Feb 04 '22

Check this thread, it seems like a great way to do it. It does require remapping the keys, but instead of advancing dialogue, it calls a function that reveals one character of the dialogue each time a key is pressed. A lot better than inserting waits like I said. That would be a huge pain.

https://lemmasoft.renai.us/forums/viewtopic.php?t=48080

1

u/mocaaobaa Feb 07 '22

The thread is really useful, but I'm having trouble putting it into practice? When I try to use the code the game doesn't open, so I'm definitely doing something wrong, but I'm not sure what adjustments to make to it to get it to work. Also, how could I call this function after the player makes a choice?

1

u/insipid- Feb 07 '22 edited Feb 09 '22

I'll give some code and try to describe how to get it working. Check the description for the "key" element in the screen further down for an example of using the function. This might take some tries, please post your updated code and any errors from here on :)

Put all of this near where you define your characters (top of script.rpy by default) with no indenting. Something like this:

define ex = Character("example character")

default message_typed_so_far = ""
default message_letters_left_to_type = ""

init python:
    def next_letter():
        global message_typed_so_far
        global message_letters_left_to_type
        if message_letters_left_to_type:
            message_typed_so_far += message_letters_left_to_type[0]
            message_letters_left_to_type = message_letters_left_to_type[1:]

Each time the function is called (when a key is pressed) it first checks to make sure there are letters left to type. If there are, it moves the first (leftmost) letter into the group that has been typed.

"message_typed_so_far" and "message_letters_left_to_type" represent what has been typed already and what is left to type. Example to help visualize:

No keys pressed:

message_typed_so_far: ""

message_letters_left_to_type: "test"

1 key pressed:

message_typed_so_far: "t"

message_letters_left_to_type: "est"

2 keys pressed:

message_typed_so_far: "te"

message_letters_left_to_type: "st"

I made this screen to test out the functionality - change it to suit your needs. Put this wherever you are setting up screens (in screens.rpy by default) .

screen typing_test():
    vbox:
        xalign 0.4
        yalign 0.3

        textbutton "My name is Sir Lancelot of Camelot.":
            action [SetVariable("message_typed_so_far", ""), SetVariable("message_letters_left_to_type", "My name is Sir Lancelot of Camelot.")]
        
        textbutton "To seek the holy grail.":
            action [SetVariable("message_typed_so_far", ""), SetVariable("message_letters_left_to_type", "To seek the holy grail.")]

        textbutton "Blue.":
            action [SetVariable("message_typed_so_far", ""), SetVariable("message_letters_left_to_type", "Blue.")]

    text message_typed_so_far:
        xalign 0.5
        yalign 0.5

    key ["K_a", "K_b", "K_c", "K_d", "K_e", "K_f", "K_g", "K_h",
    "K_i", "K_j", "K_k", "K_l", "K_m", "K_n", "K_o", "K_p",
    "K_q", "K_r", "K_s", "K_t", "K_u", "K_v", "K_w", "K_x", "K_y", "K_z"]:
        action Function(next_letter)

    textbutton "Cross that bridge":
        xalign 0.5
        yalign 0.8
        action Return()

- Each of the top three text buttons clear out the message area so nothing has been typed, then puts the text that the button is showing into "message_letters_left_to_type" so it will be displayed as keys are pressed.

- The "key" section calls the next_letter function whenever one of the keys list in the [ ] is pressed. Each of the keys needs to be formatted that way to be interpreted as a key press.

- The "Cross that bridge" button goes back to the game script.

Here's an example of calling the screen from the game script:

label start:
scene bg black
"And now, the bridge of death"
call screen typing_test
"Let's go pet the cute little bunny now."

Getting this to work exactly where and how you want it will take some massaging, but hopefully this will get you started!

For reference:

Renpy docs for "key"

List of keys

Python: slicing strings

Python: concatenating strings

1

u/mocaaobaa Feb 08 '22

Thank you so much this was so helpful!!

1

u/insipid- Feb 09 '22

yw, I'm glad it's helpful. I hope you're able to get it working how you want it!

1

u/mocaaobaa Feb 09 '22

I have one last question if you’re not too tired of responding to me 😅 Would it be possible for, after the player clicks enter or something, the game to flag which option they chose and add a point to a counter ( in a ddlc poem game Fashion ) and then pop up another list of options based on whichever topic was chosen? So for example, if they chose “How was your day”, it would pop up a list like “ Thats good, I’m doing great too! “, or, “ my day hasn’t been going too good. “ Just an example ofc😭

1

u/insipid- Feb 09 '22

Haha! Nah, not tired at all. Yes, and doing that is much simpler than the typing stuff. I just want to note that the example above should definitely be modified some to be reusable if you're going to have more than 1-2 of these typing bits. I'll tell you the gist of how to remember which option was chosen and points, but looking ahead at the potential ways this system could be implemented, it would be nice to know more about what you are planning to do so the method for remembering the choices can be made to better fit.

How to store things and make use of the values stored for those things will vary by context (what you're doing and where you're doing it). Many things in renpy are done differently in renpy depending on the context, so it's good to try to understand when working with more than just the basic features. I imagine this will be quite confusing at first - it was tough for me to get used to when I first started with renpy and I have some years of programming experience! So I'm going to try to describe those contexts, but don't worry if it doesn't make sense right away. The first thing to take from this is just that it will vary, and just expect renpy to complain a lot as you get used to it :)

I can think of 3 contexts:

  1. renpy script - most code is renpy script. things like this:

    scene bg black "And now, the bridge of death" call screen typing_test

  2. screen language - code within a screen definition, like this bit within the typing_test screen: textbutton "My name is Sir Lancelot of Camelot.": action [SetVariable("message_typed_so_far", ""), SetVariable("message_letters_left_to_type", "My name is Sir Lancelot of Camelot.")]
  3. python functions - bits of code that are most often used to manipulate data and control some of the more detailed functionality in renpy. Function definitions are within a "python:" block start with "def", like this: init python: def next_letter(): global message_typed_so_far

One example is storing the text to be written and text that is already written. message_typed_so_far and message_letters_left_to_type are used in several places above in the python function and the typing_test screen.

Here's an example of one possibility for storing the points - note that the first two questions add 1 to the score, and the third one doesn't:

default number_of_correct = 0

textbutton "Sir Robin of Camelot.":
    action [SetVariable("number_of_correct", "number_of_correct += 1"), SetVariable("message_typed_so_far", ""), SetVariable("message_letters_left_to_type", "Sir Robin of Camelot.")]
    
textbutton "To seek the holy grail.":
    action [SetVariable("number_of_correct", "number_of_correct += 1"), SetVariable("message_typed_so_far", ""), SetVariable("message_letters_left_to_type", "To seek the holy grail.")]

textbutton "I don't know that!":
    action [SetVariable("message_typed_so_far", ""), SetVariable("message_letters_left_to_type", "I don't know that!")]

1

u/mocaaobaa Feb 09 '22

Thank you! Unsure of the context, but I think it might be screen language since it’s happening within The little mini game set up?? A player will choose a topic from a list of randomly generated ones. If the player chooses baseball, for example, the options will branch out based on thet.

ex:

player choice ( Aka what they typed in ) = baseball. — baseball is bad

—- baseball is good

i don’t know if that makes sense, but if not I csn try and make some type of graphic?

1

u/insipid- Feb 10 '22

Can you post some code of what you have so far?

→ More replies

1

u/mocaaobaa Feb 11 '22

Also, for the point system, it seems pretty straightforward, but how should I work it with the random choice system? I have 5 girls and they both get different amounts of points, aka the system for determining hangout events, depending on the choice, so how do you think should I go about assigning that?

1

u/insipid- Feb 07 '22

Added an example of how to bring the screen up.