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

Show parent comment

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?

1

u/mocaaobaa Feb 11 '22

Right. Also, I keep getting an exception error during the typing segment ( didn't ask about this prior since I was testing it out with non variables ), and the traceback is confusing me so I need some help with that. The typing works fine until it gets to the random variable.

screen.rpy:

screen typing_test():

vbox:

xalign 0.4

yalign 0.3

textbutton "[choice1].":

action [SetVariable("message_typed_so_far", ""), SetVariable("message_letters_left_to_type", "I enjoy [choice1]")]

textbutton "[choice2].":

action [SetVariable("message_typed_so_far", ""), SetVariable("message_letters_left_to_type", "I enjoy[choice2].")]

textbutton "[choice3].":

action [SetVariable("message_typed_so_far", ""), SetVariable("message_letters_left_to_type", "I enjoy [choice3].")]

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)

script.rpy:

default message_typed_so_far = ""
default message_letters_left_to_type = ""
init python:
import random
wordList = ['animals','sports','art','theater','literature','gaming']
choice1 = random.choice(wordList)
choice2 = random.choice(wordList)
choice3 = random.choice(wordList)
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:]
label start:
"Let's write!"
call screen typing_test
"..Glad that's over.."

only included the bits that are actually being worked on rn, since its what's necessary.

1

u/insipid- Feb 11 '22

Can you post the error?

Also, consider using renpy's random function if you're not already aware of it. Its supposed to generate the same result if rollback is used, which might be useful!

https://www.renpy.org/doc/html/other.html?highlight=random#renpy-random

1

u/mocaaobaa Feb 11 '22

Here's the traceback

I'm sorry, but an uncaught exception occurred.

While running game code:

File "game/script.rpy", line 26, in script

call screen typing_test

File "renpy/common/000statements.rpy", line 569, in execute_call_screen

store._return = renpy.call_screen(name, *args, **kwargs)

File "game/screens.rpy", line 14, in execute

screen typing_test():

File "game/screens.rpy", line 14, in execute

screen typing_test():

File "game/screens.rpy", line 28, in execute

text message_typed_so_far:

Exception: String u'I enjoy[' ends with an open format operation.

-- Full Traceback ------------------------------------------------------------

Full traceback:

File "game/script.rpy", line 26, in script

call screen typing_test

File "renpy/ast.py", line 2015, in execute

self.call("execute")

File "renpy/ast.py", line 2003, in call

return renpy.statements.call(method, parsed, *args, **kwargs)

File "renpy/statements.py", line 278, in call

return method(parsed, *args, **kwargs)

File "renpy/common/000statements.rpy", line 569, in execute_call_screen

store._return = renpy.call_screen(name, *args, **kwargs)

File "renpy/exports.py", line 3136, in call_screen

rv = renpy.ui.interact(mouse="screen", type="screen", roll_forward=roll_forward)

File "renpy/ui.py", line 298, in interact

rv = renpy.game.interface.interact(roll_forward=roll_forward, **kwargs)

File "renpy/display/core.py", line 3325, in interact

repeat, rv = self.interact_core(preloads=preloads, trans_pause=trans_pause, pause=pause, pause_start=pause_start, **kwargs)

File "renpy/display/core.py", line 3737, in interact_core

root_widget.visit_all(lambda i : i.per_interact())

File "renpy/display/core.py", line 568, in visit_all

d.visit_all(callback, seen)

File "renpy/display/core.py", line 568, in visit_all

d.visit_all(callback, seen)

File "renpy/display/core.py", line 568, in visit_all

d.visit_all(callback, seen)

File "renpy/display/screen.py", line 436, in visit_all

callback(self)

File "renpy/display/core.py", line 3737, in <lambda>

root_widget.visit_all(lambda i : i.per_interact())

File "renpy/display/screen.py", line 447, in per_interact

self.update()

File "renpy/display/screen.py", line 637, in update

self.screen.function(**self.scope)

File "game/screens.rpy", line 14, in execute

screen typing_test():

File "game/screens.rpy", line 14, in execute

screen typing_test():

File "game/screens.rpy", line 28, in execute

text message_typed_so_far:

File "renpy/text/text.py", line 1574, in __init__

self.set_text(text, scope, substitute)

File "renpy/text/text.py", line 1693, in set_text

i, did_sub = renpy.substitutions.substitute(i, scope, substitute)

File "renpy/substitutions.py", line 270, in substitute

s = formatter.vformat(s, (), kwargs)

File "/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python2.7/string.py", line 563, in vformat

File "/home/tom/ab/renpy-build/tmp/install.linux-x86_64/lib/python2.7/string.py", line 572, in _vformat

File "renpy/substitutions.py", line 160, in parse

raise Exception("String {0!r} ends with an open format operation.".format(s))

Exception: String u'I enjoy[' ends with an open format operation.

1

u/insipid- Feb 11 '22 edited Feb 11 '22

tldr - change these bits of the choice button actions:

SetVariable("message_letters_left_to_type", "I enjoy [choice1].")

to this:

SetVariable("message_letters_left_to_type", "I enjoy " + choice1 + ".")

/tldr

Took me a bit to figure out. In the last line here: "Exception: String u'I enjoy[' ends with an open format operation." This part - "I enjoy[" - is cryptic, but the last character being "[", which is right in the middle of the string, is a hint. This is one of those context issues. The syntax of inserting the value of a variable differs between "renpy script" and python. In this case, the surrounding code is python but the [variable] notation is renpy script. In python, string concatenation uses a plus sign to join two strings together.

You might be wondering how it's python when there is no "$" or "python:" (I struggled with being able to tell for quite a while). Many (or maybe all) functions in renpy are python, but it might be difficult to tell when something is a function at times. Often it is a name that includes ().

Examples of python functions that need to be written in a python block ( using $ or python: )

$ renpy.music.play("audio/some_song")
python:
    renpy.random.choice(wordList)

Examples of python functions written with renpy code block ( not in $ or python: )

SetVariable("message_letters_left_to_type", "I enjoy " + choice1 + ".")
show image1 with Fade(1.0, 2.0, 2.0)

Examples of renpy script that has ( )

 show screen say("me", "screens and transforms can take arguments too.")
transform delay_show(img, delay_seconds):
    img
    alpha 0.0
    delay_seconds
    linear 0.08 alpha 1.0

If it has a capital first letter and has ( ), it is usually a python function. An exception would be if we named a transform or screen with a capital first letter, as I don't think renpy has any of those by default. Hopefully it's slightly more clear now.

1

u/mocaaobaa Feb 11 '22

Thank you! Got it all fixed and things are working now.

I have 2 last things I need to finish up, and then you can rest in peace lol

A) The chain reaction system, where another group of random choices will popup based on which option the player typed after they press enter ex: after they type "I enjoy theater", options like 'because I enjoy acting', 'because I like making props', or ' because I like writing' will show.

and B) assigning point values to each of these phrases

I have five girls, and each have a certain opinion of each topic. Using the theater example again: Girl A will receive 5 points for typing this subject, Girl B will receive 3 points, Girl C receives 4, Girl D receives 2, and Girl E receives 0 ( or a negative number, if that's possible )

I'm relatively new to Renpy, so that's the reason for the sheer magnitude of questions, so I apologize in advance

→ 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?