Ren'Py How to replace the ENTIRE string instead of a specific word?

May 29, 2022
231
432
I feel as if this may be more Python related than Ren'Py related, but I kinda need help right now.

So, I'm making a Sandbox game and I have a time of day system. The player can do certain tasks and such (If it's the morning, the player can make and eat breakfast. But only when it's morning. In the night, the player can get a midnight snack. As long as it's night time.) But I created a screen where it displays the time of day and the day of the week so people know what time it is (Morning time? More like Morbin' time :cool:) but I'm forced to define the variable so it can work (I just defined it as "null" as I assumed that it would work anyway.)

Here's an small, basic excerpt of my code:

Python:
label timeofday:
    while True:
        if timeofday == 1:
            $ timeofday_string = "Morning"
        elif timeofday == 2:
            $ timeofday_string = "Evening"
        elif timeofday == 3:
            $ timeofday_string = "Night"
    return
And the screen to accommodate it.

Python:
screen timeofday_display():
        text _("[timeofday_string]")
Except, when the time changes, it works, but the string doesn't even change! The string still stays as "null". So I want to replace the string instead. I never actually tried to replace strings before, but this is what I think they might do. I'm unsure how it looks in Ren'Py, but I know what it may look like in Python.

Python:
whattimeisit = "It's morning time"

x = whattimeisit.replace("morning", "morbin")
Which means hopefully, "It's morning time" will turn into "It's morbin time". I'm trying it in Python and it doesn't seem to work so I'm wondering how I can get it to work.

But, I want to change the ENTIRE instance, instead of a specific word. Like what if I wanted to change "hehehe 69420" to "Touch grass"? How would I do that?

I'm kind of confused on how I can go on doing this. And no, I do not mean CTRL + F, I mean replacing the string itself using str.replace. Sorry if this is a bit long but, HOW?!?!?!
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,369
15,285
Except, when the time changes, it works, but the string doesn't even change! The string still stays as "null".
Hmm. Since "null" is its original value, I would say that either the variable, or the screen, is not updated.

Open the console, and type watch timeofday_string. Then play your code normally.
If the value shown at the top right of the screen and the one shown by your screen do not match, then your screen have a problem, try to prefix the variable (text _( "[store.timeofday_string]" ) ).
Else, then the variable is effectively not updated...


Code:
label timeofday:
    while True:
        if timeofday == 1:
            $ timeofday_string = "Morning"
        elif timeofday == 2:
            $ timeofday_string = "Evening"
        elif timeofday == 3:
            $ timeofday_string = "Night"
    return
Is this your actual code, or an example you wrote for your post ?
And if it's your code, how do you expect it to works ? And how do you use it ?

If you reach the code wrote on your post, you end stuck in an endless loop. So, if it's effectively the code in your script, it's never played ; what mean that the value is never updated.


So I want to replace the string instead.
It wouldn't change the issue you're facing. Whatever if it's the screen itself, or the variable, something isn't updated when you directly assign a new value. It wouldn't works better when you replace a part of the value ; what anyway is an assignation in Python.


Which means hopefully, "It's morning time" will turn into "It's morbin time". I'm trying it in Python and it doesn't seem to work so I'm wondering how I can get it to work.
It works perfectly fine. What just reinforce my feeling that the error is somewhere else ; once again, either the variable or the screen, that isn't updated.


But, I want to change the ENTIRE instance, instead of a specific word. Like what if I wanted to change "hehehe 69420" to "Touch grass"? How would I do that?
You're thinking too much:
Code:
variableName = "hehehe don't doxe yourself"
variableName = "Touch grass"
 
May 29, 2022
231
432
Is this your actual code, or an example you wrote for your post ?
And if it's your code, how do you expect it to works ? And how do you use it ?
Example. Actual code would probably make the post too long.

You're thinking too much:
Code:
variableName = "hehehe don't doxe yourself"
variableName = "Touch grass"
I tried using that method but it didn't seem to work as I wanted it to. The string still didn't update.

It wouldn't change the issue you're facing. Whatever if it's the screen itself, or the variable, something isn't updated when you directly assign a new value. It wouldn't works better when you replace a part of the value ; what anyway is an assignation in Python.
How do I fix the issue then? I'm unsure what the root cause of it may be, maybe I'm using while True statements, or other things like that.

Also, a more detailed example if it helps:

Time of day
Python:
label timeofday:

    while True:
        if timeofday == 1:
            $ timeofday_string = "Morning"
        elif timeofday == 2:
            $ timeofday_string = "Noon"
        elif timeofday == 3:
            $ timeofday_string = "Evening"
        elif timeofday == 4:
            $ timeofday_string = "Night"
        elif timeofday == 5:
            $ timeofday_string = "Midnight"
        else:
            $ timeofday = "{color=#ff001e}Error{/color}"
    return
Screen that displays the time and day of the week
Python:
screen uilobby():
    add "gui/custom/calendaricon.png" xpos 25 ypos 160 xzoom 0.25 yzoom 0.25
    text _("[dayofweek_string] ║ [timeofday_string]") xpos 148 ypos 180
    text _("[monthofyear_string] [year]") xpos 148 ypos 220
Bedroom code (this will update the time of day, but not the text for some reason)
Python:
label bedroom_choices:
    if timeofday == 1 or timeofday == 2:
        scene bedroom_day with fade
    if timeofday == 3:
        scene bedroom_evening with fade
    if timeofday == 4 or timeofday == 5:
        scene bedroom_night with fade

    while True:
        menu:
            "Go To Bed...":
                menu:
                    "Take a Nap":
                        $ timeofday += 1
                        $ energy += 7
                        $ xp += 6
                        n "You had a nice nap."
                        n "Now you're feeling a bit more energetic!"
                        jump bedroom_choices
                    "Rest my eyes for a bit":
                        $ xp += 3
                        $ energy += 4
                        n "You rested your eyes for a few minutes."
                        n "You gained some Energy!"
                        jump bedroom_choices
                    "Fall Asleep":
                        $ xp += 14
                        $ timeofday += 2
                        $ energy += 13
                        n "You went and fell asleep."
                        jump bedroom_choices
This code is kind of unnecessary for what I'm talking about but I do kind of need help.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,369
15,285
How do I fix the issue then?
First you find what is the issue, then you fix it. And so far you don't know the issue, just it's consequence: Either the variable or the screen is not updated.


Python:
label timeofday:

    while True:
        if timeofday == 1:
            $ timeofday_string = "Morning"
        elif timeofday == 2:
            $ timeofday_string = "Noon"
        elif timeofday == 3:
            $ timeofday_string = "Evening"
        elif timeofday == 4:
            $ timeofday_string = "Night"
        elif timeofday == 5:
            $ timeofday_string = "Midnight"
        else:
            $ timeofday = "{color=#ff001e}Error{/color}"
    return
Once again the same code that can not works... Once, it's a lack of attention when writing the example, twice it start to be the mark of a wrong code.
So I ask again: What do you expect from this code ?
What is it supposed to do, and how you reach it ?


Bedroom code (this will update the time of day, but not the text for some reason)
"some reason" that is: At no time the value of the variable timeofday_string is updated, because at no time you reach the "timeofday" label.


This code is kind of unnecessary for what I'm talking about but I do kind of need help.
It's the opposite, "this code" explained everything...

So, replace "timeofday" by this:
Python:
label timeofday( step ):
    $ timeofday += step
    if timeofday > 5:
        $ timeofday -= 5

    if timeofday == 1:
        $ timeofday_string = "Morning"
    elif timeofday == 2:
        $ timeofday_string = "Noon"
    elif timeofday == 3:
        $ timeofday_string = "Evening"
    elif timeofday == 4:
        $ timeofday_string = "Night"
    elif timeofday == 5:
        $ timeofday_string = "Midnight"
    else:
        $ timeofday_string = "{color=#ff001e}Error{/color}" # It's not "timeofday" here !!!
return
And update "bedroom_choices" like this:
Python:
label bedroom_choices:
    if timeofday == 1 or timeofday == 2:
        scene bedroom_day with fade
    if timeofday == 3:
        scene bedroom_evening with fade
    if timeofday == 4 or timeofday == 5:
        scene bedroom_night with fade

    while True:
        menu:
            "Go To Bed...":
                menu:
                    "Take a Nap":
                        #$ timeofday += 1
                        call timeofday( 1 )
                        $ energy += 7
                        $ xp += 6
                        n "You had a nice nap."
                        n "Now you're feeling a bit more energetic!"
                        jump bedroom_choices
                    "Rest my eyes for a bit":
                        $ xp += 3
                        $ energy += 4
                        n "You rested your eyes for a few minutes."
                        n "You gained some Energy!"
                        jump bedroom_choices
                    "Fall Asleep":
                        $ xp += 14
                        #$ timeofday += 2
                        call timeofday( 2 )
                        $ energy += 13
                        n "You went and fell asleep."
                        jump bedroom_choices
And like now "timeofday_string" is finally updated, everything should works.
 
May 29, 2022
231
432
First you find what is the issue, then you fix it. And so far you don't know the issue, just it's consequence: Either the variable or the screen is not updated.




Once again the same code that can not works... Once, it's a lack of attention when writing the example, twice it start to be the mark of a wrong code.
So I ask again: What do you expect from this code ?
What is it supposed to do, and how you reach it ?




"some reason" that is: At no time the value of the variable timeofday_string is updated, because at no time you reach the "timeofday" label.




It's the opposite, "this code" explained everything...

So, replace "timeofday" by this:
Python:
label timeofday( step ):
    $ timeofday += step
    if timeofday > 5:
        $ timeofday -= 5

    if timeofday == 1:
        $ timeofday_string = "Morning"
    elif timeofday == 2:
        $ timeofday_string = "Noon"
    elif timeofday == 3:
        $ timeofday_string = "Evening"
    elif timeofday == 4:
        $ timeofday_string = "Night"
    elif timeofday == 5:
        $ timeofday_string = "Midnight"
    else:
        $ timeofday_string = "{color=#ff001e}Error{/color}" # It's not "timeofday" here !!!
return
And update "bedroom_choices" like this:
Python:
label bedroom_choices:
    if timeofday == 1 or timeofday == 2:
        scene bedroom_day with fade
    if timeofday == 3:
        scene bedroom_evening with fade
    if timeofday == 4 or timeofday == 5:
        scene bedroom_night with fade

    while True:
        menu:
            "Go To Bed...":
                menu:
                    "Take a Nap":
                        #$ timeofday += 1
                        call timeofday( 1 )
                        $ energy += 7
                        $ xp += 6
                        n "You had a nice nap."
                        n "Now you're feeling a bit more energetic!"
                        jump bedroom_choices
                    "Rest my eyes for a bit":
                        $ xp += 3
                        $ energy += 4
                        n "You rested your eyes for a few minutes."
                        n "You gained some Energy!"
                        jump bedroom_choices
                    "Fall Asleep":
                        $ xp += 14
                        #$ timeofday += 2
                        call timeofday( 2 )
                        $ energy += 13
                        n "You went and fell asleep."
                        jump bedroom_choices
And like now "timeofday_string" is finally updated, everything should works.
Yay! It finally worked! And I apologize if I did seem a bit rude in the previous comments, I was a bit upset and frustrated since I had no idea what I did wrong in my code for it to not work. But, thank you so much for your help!