Ren'Py [SOLVED]screen if condition doesn't work

Hetaneko

Newbie
Apr 26, 2018
32
31
Guys, i just wanted to make day/time system but its doesn't work because of the screen. I realized that if condition doesn't work in screen. here is the code:


default time_hour = 0
screen myGui():
if time_hour >= 12:
$ time_hour -= 12


when that happens its just shows that "time_hour" is "0" but when i look at from console, the "time_hour" is still "12" its doesnt work.
please help!
 

riktor

Active Member
Nov 26, 2018
906
1,161
could be an issue with variable scope, try

globals()['time_hour']

inside the screen definition
 

Hetaneko

Newbie
Apr 26, 2018
32
31
dude, you got what i mean but its doesn't works.

first i did like that
317865

it didn't work. then i did this:

317866

and it didn't work too. :(
 

riktor

Active Member
Nov 26, 2018
906
1,161
you don't seem to get what I'm saying.

use globals()['time_hour'] instead of time_hour any time you call that var in the screen definition. so your if should be

if globals()['time_hour'] >= 12:

etc.. every time you would call time_hour use globals()['time_hour'] instead
 
Last edited:
  • Like
Reactions: Hetaneko

Hetaneko

Newbie
Apr 26, 2018
32
31
dude its kinda worked but idk why, the hour is always decreasing...
im gonna share the full code:
Code:
default time_hour = 0
default time_day = 1
default time_day_name = "Mon"
default pm_am_name = "AM"
default time_minute = 0
default time_pm_am = 1
screen myGui():
    zorder 10

    if time_day == 1:
        $ time_day_name = "Mon"
    elif time_day == 2:
        $ time_day_name = "Tue"
    elif time_day == 3:
        $ time_day_name = "Wed"
    elif time_day == 4:
        $ time_day_name = "Thu"
    elif time_day == 5:
        $ time_day_name = "Fri"
    elif time_day == 6:
        $ time_day_name = "Sat"
    elif time_day == 7:
        $ time_day_name = "Sun"
    if time_day >= 7 and time_hour >= 12 and time_pm_am == 1:
        $ time_day -= 6
    if time_pm_am == 1:
        $ pm_am_name = "PM"
    elif time_pm_am == 2:
        $ pm_am_name = "AM"


    if globals()['time_pm_am'] == 2 and globals()['time_hour'] == 0:
        $ globals()['pm_am_name'] = "NOON"
    if globals()['time_pm_am'] == 1 and globals()['time_hour'] == 0:
        $ globals()['pm_am_name'] = "MOON"
    if globals()['time_hour'] >= 12 and globals()['time_pm_am'] == 2:
        $ globals()['time_hour'] -= 12
        $ globals()['time_pm_am'] -= 1
    if globals()['time_hour'] >= 12 and globals()['time_pm_am'] == 1:
        $ globals()['time_hour'] -= 12
        $ globals()['time_pm_am'] += 1
    frame:
        style "frame_default"
        xalign 0.5 yalign 0.0
        xminimum 0.45
        yminimum 0.050
        hbox:
            xalign 0.5 yalign 0.0
            textbutton "Wait" text_style "buttonz" action SetVariable("time_hour", time_hour + 1)
            text "[time_hour]:[time_minute] [pm_am_name]"
            textbutton "Pass Day" text_style "buttonz" action SetVariable("time_day", time_day + 1)
 

riktor

Active Member
Nov 26, 2018
906
1,161
screens loop, any var changes may be repeated while the screen is displayed unless they are blocked in specific ways. I use tracking vars and if statements. or if you want to get more complex you could put your variable calculations into a custom function and call that function in an on show statement in the screen definition.

also you should be using the globals()['VARIABLE'] setup for any global vars (other than class attributes and such) inside a screen def so for all your vars basically. time_day should be globals()['time_day'] time_day_name / globals()['time_day_name'] etc. all of them. if statements, textbuttons, even the text calls so:

Python:
text "[globals()['time_hour']]:[globals()['time_minute']] [globals()['pm_am_name']]"
or my preferred method would be
Python:
text '{0}:{1} {2}'.format(globals()['time_hour'], globals()['time_minute'], globals()['pm_am_name'])
 
Last edited:
  • Like
Reactions: Hetaneko

Hetaneko

Newbie
Apr 26, 2018
32
31
god damn it, is it too hard to make a fuckin day/time system or is this just renpy. all i wanted to make this work:
317911

thanks for your help m8.
 

riktor

Active Member
Nov 26, 2018
906
1,161
no trouble. it's not that difficult if you keep at it. I'm happy to keep trying to help. give me an over view of how you want the whole system to work (rather than just the screen portion) and I can try to bang something out for you as an example.
 
Last edited:
  • Like
Reactions: Hetaneko

Hetaneko

Newbie
Apr 26, 2018
32
31
dude i appreciate you so much. all i need is when the time went 12, the var is must reset. and the "AM" will be change to "PM". then when the hour is 12 and the time is "PM" its going to be "AM" and add 1 point to "time_day" (there is no minute system, it will be just "00")
it should look like this:
317920

i'm trying to make this for 2 days, when the hour is 12, resetting to 0, but then it's not increasing because when i look at from console its still 12.
Thanks.
 

riktor

Active Member
Nov 26, 2018
906
1,161
no trouble, I have the time today. give me a bit to work on this and I'll get back to you.
 
  • Like
Reactions: Hetaneko

riktor

Active Member
Nov 26, 2018
906
1,161
ok, heres what is working for me, and should perhaps not interfere with the way your were planning things.

Python:
init python:
    class Time:
        hour = 0
        day = 1
        days = [0, "Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"]
        day_name = Time.days[Time.day]
        half = [1, "PM", "AM"]
    def adv_time(h=0):
        while h > 0:
            if h > 0:
                Time.hour += 1
            if Time.hour >= 12:
                Time.hour -= 12
                if Time.half[0] == 2:
                    Time.half[0] = 1
                else:
                    Time.half[0] = 2
                Time.day += 1
                if Time.day == 7: Time.day -= 6
                Time.day_name = Time.days[Time.day]
            h -= 1
label test:
    call screen myGui()
    
screen myGui():
    zorder 10
    frame:
        style "frame_default"
        xalign 0.5 yalign 0.0
        xminimum 0.45
        yminimum 0.050
        hbox:
            xalign 0.5 yalign 0.0
            textbutton "Wait" action Function(adv_time, h=1)
            text '{0}:00 {1}'.format(Time.hour, Time.half[Time.half[0]])
            textbutton "Pass Day" action Function(adv_time, h=24)
Variable names are a bit different though, I used a class to contain them so as not to run into a scope problem like you were having.

Time.day
Time.days -- to track the day names
Time.day_name
Time.hour
Time.half -- replacing the am/pm stuff

I left minutes out since you said they weren't tracked anyway

instead of addressing the vars in the screen I use a function that the screen calls
tested using the console command
renpy.call('test')
to call label test and open the screen.
If you have any questions about how it works ask away.
 

riktor

Active Member
Nov 26, 2018
906
1,161
please attach what ever file you saved that code into so I can look at that
 

anne O'nymous

I'm not grumpy, I'm just coded that way.
Modder
Donor
Respected User
Jun 10, 2017
10,979
16,236
but when i look at from console, the "time_hour" is still "12" its doesnt work.
Due to the way Ren'py optimize its own code, the console is always an interaction late. If you used it with the code you gave later as test, it's natural that you still see 12 in the console.

Anyway, here's what you want :
Code:
default time_hour = 0
default is_am = True

screen myGui():
    frame:
        hbox:
            textbutton "wait" action If( time_hour < 12,
                    # Increment the hour is lower than 12.
                    SetVariable( "time_hour", time_hour +1 ),
                    # else turn back the hour to 0, and toggle the /is_am/ flag.
                    [ SetVariable( "time_hour", 0 ), ToggleVariable( "is_am" ) ]  )
            text "Time=[time_hour]"
            if is_am is True:
                text "AM"
            else:
                text "PM"

could be an issue with variable scope, try
There's only two scopes with Ren'py : global variables and screen variables.
There's no need to import variables from the global space unless you're in a Python function and want to assign a value.
 

riktor

Active Member
Nov 26, 2018
906
1,161
There's only two scopes with Ren'py : global variables and screen variables.
There's no need to import variables from the global space unless you're in a Python function and want to assign a value.
addressing his vars in a global context seemed the only way to make his initial if statements / var setting function the way he wanted without re-writing the entire thing.
 

Hetaneko

Newbie
Apr 26, 2018
32
31
Due to the way Ren'py optimize its own code, the console is always an interaction late. If you used it with the code you gave later as test, it's natural that you still see 12 in the console.

Anyway, here's what you want :
Code:
default time_hour = 0
default is_am = True

screen myGui():
    frame:
        hbox:
            textbutton "wait" action If( time_hour < 12,
                    # Increment the hour is lower than 12.
                    SetVariable( "time_hour", time_hour +1 ),
                    # else turn back the hour to 0, and toggle the /is_am/ flag.
                    [ SetVariable( "time_hour", 0 ), ToggleVariable( "is_am" ) ]  )
            text "Time=[time_hour]"
            if is_am is True:
                text "AM"
            else:
                text "PM"



There's only two scopes with Ren'py : global variables and screen variables.
There's no need to import variables from the global space unless you're in a Python function and want to assign a value.
hi, first i did this already and i realized thats just work when you click to wait.
im going to add +1 hour with label too and its not gonna work because its resets just when you click wait...