Ren'Py Need help with renpy/python

Inmortaliity

Member
Feb 27, 2019
181
225
I'm putting up Try, Exept, Finally in renpy within a init python.
But how do I manage it to jump to a renpy-label?
Here is the code.

init python:
def Day_counter():
try:
## Day declarations ##
if store.DayTime == 1:
store.DayName = "Morning"
if store.DayTime == 2:
store.DayName = "Late Morning"
if store.DayTime == 3:
store.DayName = "Noon"
if store.DayTime == 4:
store.DayName = "Afternoon"
if store.DayTime == 5:
store.DayName = "Evening"
if store.DayTime == 6:
store.DayName = "Late Evening"
if store.DayTime == 7:
store.DayName = "Night"
if store.DayTime == 8:
store.DayName = "Midnight"
if store.DayTime > 8 or store.DayTime < 1:
store.renpy.jump(Go_Back_Point)
except:
pass
config.python_callbacks.append(Day_counter)


But Renpy never jumps over to the label Go_Back_Point....
Niether "call" or "jump" works. It just ignores it.

Am I missing something?
Or do someone have a better solution to make Renpy jump to a label, when DayTime is increased over 8?
 

scrumbles

Well-Known Member
Jan 12, 2019
1,069
1,096
When inspecting a variable, combine the IFs or the conditional expressions are evaluated again and again even if clearly false (if you already know that DayTime is equal to one, there is no need to check if it's equal to another integer).
Also, sorry if my question is retarded, but if DayTime and DayName are regular variables, why do you prepend "store." in front of them?
Finally not sure if you can change the value of DayName if it isn't declared as global (but again, I don't know how "store" works).
Code:
global DayName
if DayTime == 1:
    DayName = "Morning"
elif DayTime == 2:
    DayName = "Late Morning"
elif DayTime == 3:
    DayName = "Noon"
...
Also, I don't know much about Python, but I find dictionaries more elegant:
Code:
define DayNames = {1:"Morning", 2:"Late Morning", 3:"Noon", 4:"Afternoon", 5:"Evening", 6:"Late Evening", 7:"Night", 8:"Midnight"}
init python:
    def Day_counter():
        global DayName
        try:
            if DayTime in DayNames:
                DayName = DayNames[DayTime]
            else:
                renpy.jump(Go_Back_Point)
        except:
            pass
    config.python_callbacks.append(Day_counter)
Edit: I haven't tested my code, it's most likely garbage.
 
Last edited:

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,971
16,226
Here is the code.
There's so many things to say about this code :

Firstly, the variables are automatically made locale to Python code. You need to prefix them only when you perform a direct assignation. So you can just write :
Code:
    if DayTime == 1:
        store.DayName = "Morning"

Secondly, you absolutely don't need a try block here. Either the variable don't exist, and it's bug that you should let Ren'py handle, or the variable exist and you'll never reach the except part.


Thirdly, the if/elif structure would be better, like said above by scrumbles .


Fourthly, a dict or list structure would be better, like also said by scrumbles .
Code:
default DayNameDict = { "1": "Morning", "2": "Late Morning" }
default DayNameList = [ "Morning",  "Late Morning" ]

screen UI:
    vbox:
        text ( "Period: {}".format( DayNameDict[DayTime] ) )
        text ( "Period: {}".format( DayNameList[DayTime] ) )

Fifthly, why doing this in Python ?
Code:
label whatever:
    [...]
    call advanceTime
    [...]

label advanceTime:
    $ DayTime += 1
    if 1 <= DayTime <= 8:
         $ DayName = DayNameDict[DayTime]
         return
    else:
         $ renpy.pop_call()
         jump Go_Back_Point

Sixthly... Using config.python_callbacks is a really strange and weird way to enforce boundaries for a variable. It works, but using the Ren'py code I gave above is way better.


But Renpy never jumps over to the label Go_Back_Point....
Niether "call" or "jump" works. It just ignores it.
I suspect that it can be a question of context, due to the fact that it happen in a callback, but I can be wrong here.
 
  • Like
Reactions: Inmortaliity

Inmortaliity

Member
Feb 27, 2019
181
225
There's so many things to say about this code :

Firstly, the variables are automatically made locale to Python code. You need to prefix them only when you perform a direct assignation. So you can just write :
Code:
    if DayTime == 1:
        store.DayName = "Morning"

Secondly, you absolutely don't need a try block here. Either the variable don't exist, and it's bug that you should let Ren'py handle, or the variable exist and you'll never reach the except part.


Thirdly, the if/elif structure would be better, like said above by scrumbles .


Fourthly, a dict or list structure would be better, like also said by scrumbles .
Code:
default DayNameDict = { "1": "Morning", "2": "Late Morning" }
default DayNameList = [ "Morning",  "Late Morning" ]

screen UI:
    vbox:
        text ( "Period: {}".format( DayNameDict[DayTime] ) )
        text ( "Period: {}".format( DayNameList[DayTime] ) )

Fifthly, why doing this in Python ?
Code:
label whatever:
    [...]
    call advanceTime
    [...]

label advanceTime:
    $ DayTime += 1
    if 1 <= DayTime <= 8:
         $ DayName = DayNameDict[DayTime]
         return
    else:
         $ renpy.pop_call()
         jump Go_Back_Point

Sixthly... Using config.python_callbacks is a really strange and weird way to enforce boundaries for a variable. It works, but using the Ren'py code I gave above is way better.




I suspect that it can be a question of context, due to the fact that it happen in a callback, but I can be wrong here.

Hmm okay, I'm going to try all your suggestion.
Why I had Try, Exept, Finally is because I don't won't to have every action that adds a DayTime to check if DayTime is over "8".

But looks like your example advanceTime is a perfect solution for my probs, if "return" now continues the current label if DayTime not goes over 8.

Big thanks!
 
Last edited:

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,971
16,226
Why I had Try, Exept, Finally is because I don't won't to have every action that adds a DayTime to check if DayTime is over "8".
It's not what try do. A try will catch the exceptions (error that aren't expected to happen) thrown by Python itself, and that's all. Therefore, with the code you gave, the block will do absolutely nothing, unless the "DayTime" or "DayName" variables do not exist. But it will surely never know the value of DayTime, nor that you don't want it to be over 8.

If you don't want to check every time if "DayTime" is over 8, just do this :
Code:
    if DayTime == 1:
        [do something]
    elif DayTime == 2:
        [do something]
    [...]
    elif DayTime == 7:
        [do something]
    elif DayTime == 8:
        [do something]
    else:
       [Oop's, it's either below 1 or over 8]
 

Inmortaliity

Member
Feb 27, 2019
181
225
It's not what try do. A try will catch the exceptions (error that aren't expected to happen) thrown by Python itself, and that's all. Therefore, with the code you gave, the block will do absolutely nothing, unless the "DayTime" or "DayName" variables do not exist. But it will surely never know the value of DayTime, nor that you don't want it to be over 8.

If you don't want to check every time if "DayTime" is over 8, just do this :
Code:
    if DayTime == 1:
        [do something]
    elif DayTime == 2:
        [do something]
    [...]
    elif DayTime == 7:
        [do something]
    elif DayTime == 8:
        [do something]
    else:
       [Oop's, it's either below 1 or over 8]
Hm Could that be an idea to put all these in some sort of a function to just call, instead to add all these if, elif cases in start(or in end) of every label.
But I fear that I need a some if-cases to check DayTime, everytime something adds to DayTime?
My hope was to find a solution that always listens to it which store.function kinda does.
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,971
16,226
But I fear that I need a some if-cases to check DayTime, everytime something adds to DayTime?
You mean each time the period change ? You just need to call the label like in my example. It will automatically increase DayTime, change DayName according to the new period, ensure that the value is between 1 and 8, and jump to the expected label if the value is over 8.
It's the exact copy of your Python function, but in more pure Ren'py.
 

Inmortaliity

Member
Feb 27, 2019
181
225
You mean each time the period change ? You just need to call the label like in my example. It will automatically increase DayTime, change DayName according to the new period, ensure that the value is between 1 and 8, and jump to the expected label if the value is over 8.
It's the exact copy of your Python function, but in more pure Ren'py.
my return just made renpy to go back to main menu.
Do or maybe i declare return somewhere?
 

RustyV

Conversation Conqueror
Game Developer
Dec 30, 2017
6,949
33,421
When ever I create an option for a player to change a set character name (that I have changed the color of) I can not get the new name to use anything but the default color for the name and not the altered color. How do I change a player picked name's color?

define d = Character("Dee", color="#006400")
define y = Character("[ ]", color="#006400")
$ y = renpy.input ("What is my name?")
y "My new name is [y]"
 

the66

beware, the germans are cumming
Modder
Donor
Respected User
Jan 27, 2017
7,808
24,389
When ever I create an option for a player to change a set character name (that I have changed the color of) I can not get the new name to use anything but the default color for the name and not the altered color. How do I change a player picked name's color?

define d = Character("Dee", color="#006400")
define y = Character("[ ]", color="#006400")
$ y = renpy.input ("What is my name?")
y "My new name is [y]"
because your with define y = Character("[ ]", color="#006400") declared ADVCharacter object gets overwritten with $ y = renpy.input ("What is my name?") and is now a string.

Python:
define y = DynamicCharacter("y_name", color="#006400")
.
.
.
$ y_name = renpy.input ("What is my name?").strip() or "Default Name"
y "My new name is [y]"
 
  • Like
Reactions: RustyV

RustyV

Conversation Conqueror
Game Developer
Dec 30, 2017
6,949
33,421
Thanks for the assistance.
Is there a way to get rid of the textbox so just the text appears on the screen?
 

the66

beware, the germans are cumming
Modder
Donor
Respected User
Jan 27, 2017
7,808
24,389
Thanks for the assistance.
Is there a way to get rid of the textbox so just the text appears on the screen?
set the background property to None in the style definition of the window in the say screen.
 

RustyV

Conversation Conqueror
Game Developer
Dec 30, 2017
6,949
33,421
Okay one last question:
How do you create text that has more than one color?
Some games have text that is white with a black shadow.
Is that coding or choice of a font?
 

79flavors

Well-Known Member
Respected User
Jun 14, 2018
1,608
2,256
Okay one last question:
How do you create text that has more than one color?
Some games have text that is white with a black shadow.
Is that coding or choice of a font?
For the most part, it's a style definition that covers all (or most) of the text that's displayed. (Probably in the screens.rpy file).
For the details, you can find more here: https://f95zone.to/threads/renpy-shadow-or-outline-text.10744/

However, it's probably also worth pointing out that it's sometimes worth tearing a couple of games apart and looking for the answers directly. You specifically mention that "some games" have shadowing. Maybe pick one of those games and look at the source code and compare it with a game that doesn't show text that way. It'll take you longer to get to the answer... and you may need to search the to confirm it... but each wrong turn you take will likely also teach you something that you wouldn't have even thought to ask. Most games will likely be compressed into a RenPy archive (.rpa files). You can decompress them using the UnRen tool (grab the 0.9 dev version right now, since 0.8 no longer works with some games due to some semi-recent RenPy changes). If the author only included the .rpyc files, UnRen can convert those back into .rpy files too.