just setting variable to True only affects - see code

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
CSK
Newbie
Posts: 16
Joined: Sat Sep 10, 2022 2:42 pm
Contact:

just setting variable to True only affects - see code

#1 Post by CSK »

Apologies, didn't know what the correct terms are to describe it correctly in the subject.

After "tfperson.var1b = True", when opening the "the_openscreen" screen "[tfperson.Output]" is showing what intended right away, but advancing the dialogue doesn't seem to have any effect on the "if tfperson.var1b == True" for "[tfperson.var1a]" in the textbutton, that only shows when using the button and going back or after having opened and closed the "the_openscreen" and opening it again. It is intended to show straight away. So for some reason [tfperson.Output] has the information right away, but "[tfperson.var1a]" does not. How do I provide "[tfperson.var1a]" with the information so it shows straight away too, or whatever is the mistake I have made that doesn't make it show as intended?

Code: Select all

init python:
    class TestFun:
        def __init__(self, var1a, var1b):
            self.var1a = var1a
            self.var1b = var1b

        @property
        def Output(self):
            var1b = self.var1b
            var1a = self.var1a
            self.var1a = var1a

            if var1b == False:
                var1a = "unknown"
            if var1b == True:
                var1a = (self.var1a)

            return "var1a: " + str(var1a)

define tfperson = TestFun("VarOneAttA", False)
define j = Character("Jane")

label test4:

    call variables

    scene white

    window show

    show screen open_screen

    j "After this line var1b to True"
    $ tfperson.var1b = True
    j "First line after"
    j "2nd line after"
    j "3rd line after"
    j "4th line after"
    j "5th line after"
    j "6th line after"

## this is in it's own .rpy in the original
screen open_screen():
    key 'keyup_K_c' action [Show('the_openscreen'), Hide('open_screen')]
    vbox:
        xalign 0.0 yalign 0.0
        textbutton "Open Screen" action Show('the_openscreen'), Hide('open_screen')

screen the_openscreen():
    tag tab_menu
    modal True
    frame:
        xalign 0.25
        yalign 0.5
        xsize 1000
        ysize 950
        has vbox

        hbox:
            if tfperson_01 >= 1:
                vbox:
                    ## this part doesn't seem to change with going down the dialogue
                    ## only after opening and closing the screen or using the button and going back
                    if tfperson.var1b == False:
                        textbutton _("Unknown") action Show('tfperson_01_screen') xalign 0.5
                    if tfperson.var1b == True:
                        textbutton _("[tfperson.var1a]") action Show('tfperson_01_screen') xalign 0.5
            else:
                null

        hbox:
            ## this part is supposed to be only in the tfperson_01_screen
            ## put it there during testing and shows right away
            text _p("""
                [tfperson.Output]
                """)
            textbutton _("Back") action Show('the_openscreen')

        textbutton _("Close") action Hide('the_openscreen'), Show('open_screen')
        key 'keyup_K_c' action [Hide('the_openscreen'), Show('open_screen')]

screen tfperson_01_screen():
    tag tab_menu
    modal True
    frame:
        xalign 0.25
        yalign 0.5
        xsize 1000
        ysize 950
        has vbox

        hbox:
            text _p("""
                [tfperson.Output]
                """)
            textbutton _("Back") action Show('the_openscreen')

## in the original this is in variables.rpy
label variables:

    $ tfperson_01 = 0
    $ tfperson_01 += 1

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: just setting variable to True only affects - see code

#2 Post by Imperf3kt »

Don't set variables via a label, it's always recommended to use default
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

CSK
Newbie
Posts: 16
Joined: Sat Sep 10, 2022 2:42 pm
Contact:

Re: just setting variable to True only affects - see code

#3 Post by CSK »

Imperf3kt wrote: Mon Sep 26, 2022 8:57 pm Don't set variables via a label, it's always recommended to use default
Thanks. The label thing is very good to know, as I had taken that from a guide I followed in my very first Renpy steps. The default I would guess means instead of "define" for variables using "default" ?

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3785
Joined: Mon Dec 14, 2015 5:05 am
itch: Imperf3kt
Location: Your monitor
Contact:

Re: just setting variable to True only affects - see code

#4 Post by Imperf3kt »

Almost.
Default is slightly different and explained here with define just above it.
https://www.renpy.org/doc/html/python.h ... -statement

The basic premise is define - things that don't change, default - things that will change
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor

Twitter

CSK
Newbie
Posts: 16
Joined: Sat Sep 10, 2022 2:42 pm
Contact:

Re: just setting variable to True only affects - see code

#5 Post by CSK »

Imperf3kt wrote: Tue Sep 27, 2022 3:03 am The basic premise is define - things that don't change, default - things that will change
Thanks. I had read that already, but early on when I was just starting to learn, so I wasn't too sure what changing exactly meant within all the new information, and while still not fully sure, it's getting better.

Generally.

In regards to the OP, I did more testing around and I needed to rewrite it anyway, so the issue does no longer apply, but it would still be interesting to know why one of them is being shown right away and the other not.

I have changed the tfperson.var1a in the following part to tfperson.Output and the issue was still the same, only closing and opening again or using the button and going back shows the "== True". Even though I no longer use it, knowing the logic why and / or the mistake I made could turn out to be useful, if anyone can explain it, but since it wasn't that clear in the OP yet for this I consider it overall solved.

Code: Select all

                    if tfperson.var1b == False:
                        textbutton _("Unknown") action Show('tfperson_01_screen') xalign 0.5
                    if tfperson.var1b == True:
                        textbutton _("[tfperson.Output]") action Show('tfperson_01_screen') xalign 0.5
                        

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: just setting variable to True only affects - see code

#6 Post by Ocelot »

In short, it is becaue you used define if you change it to defaukt there, issue should disappear.

A longer explanation: when you create varaibles using define RenPy considers them unchangeable and does not check if they are changed if they are still in cache somewhere. So changes are not always visible. Another funny thing: if you take the code you posted, run it to the end until you exit to the main menu and launch it again, tfperson.var1b will already be True, since tfperson shouldn't change anyway, so RenPy does not bother recreating it again.
< < insert Rick Cook quote here > >

CSK
Newbie
Posts: 16
Joined: Sat Sep 10, 2022 2:42 pm
Contact:

Re: just setting variable to True only affects - see code

#7 Post by CSK »

Ocelot wrote: Tue Sep 27, 2022 6:46 am In short, it is becaue you used define if you change it to defaukt there, issue should disappear.

A longer explanation: when you create varaibles using define RenPy considers them unchangeable and does not check if they are changed if they are still in cache somewhere. So changes are not always visible. Another funny thing: if you take the code you posted, run it to the end until you exit to the main menu and launch it again, tfperson.var1b will already be True, since tfperson shouldn't change anyway, so RenPy does not bother recreating it again.
Thanks. That "funny" thing I had already noticed before I attempted the "true/false" version of it and was part of the reason that even lead to it - and also lead to several added test runs before I posted any questions here to be sure what I was asking was what was happening - so good to know that this was the reason behind that.

As already stated, the issue is no longer an issue that I need to have resolved for my project, as I went a different way, but in regards to understanding what's happening. Well, when I read the explanation I went back to test this again to be sure, as I thought I had already changed it to "default" and it still acted that way, and the testing confirmed it:

Code: Select all

init python:
    class TestFun:
        def __init__(self, var1a, var1b):
            self.var1a = var1a
            self.var1b = var1b

        @property
        def Output(self):
            var1b = self.var1b
            var1a = self.var1a
            self.var1a = var1a

            if var1b == False:
                var1a = "unknown"
            if var1b == True:
                var1a = (self.var1a)

            return str(var1a)

label test4:

    default tfperson = TestFun("VarOneAttA", False)
    default j = Character("Jane")
    scene white

    window show

    show screen open_screen

    j "After this line var1b to True"
    $ tfperson.var1b = True
    j "First line after"

## this is in it's own .rpy in the original
screen open_screen():
    key 'keyup_K_c' action [Show('the_openscreen'), Hide('open_screen')]
    vbox:
        xalign 0.0 yalign 0.0
        textbutton "Open Screen" action Show('the_openscreen'), Hide('open_screen')

screen the_openscreen():
    tag tab_menu
    modal True
    frame:
        xalign 0.25
        yalign 0.5
        xsize 1000
        ysize 950
        has vbox

        hbox:
            vbox:
                ## with definition via default, still doesn't show right away like the below
                if tfperson.var1b == False:
                        textbutton _("Unknown") action Show('tfperson_01_screen') xalign 0.5
                if tfperson.var1b == True:
                        textbutton _("[tfperson.Output]") action Show('tfperson_01_screen') xalign 0.5

        hbox:
            ## this part with the same [tfperson.Output] as the above but shows
            text _p("""
                [tfperson.Output]
                """)
            textbutton _("Back") action Show('the_openscreen')

        textbutton _("Close") action Hide('the_openscreen'), Show('open_screen')
        key 'keyup_K_c' action [Hide('the_openscreen'), Show('open_screen')]

screen tfperson_01_screen():
    tag tab_menu
    modal True
    frame:
        xalign 0.25
        yalign 0.5
        xsize 1000
        ysize 950
        has vbox

        hbox:
            text _p("""
                [tfperson.Output]
                """)
            textbutton _("Back") action Show('the_openscreen')
I had even just deleted all the ".rpyc" files due to a bug and I didn't use "shift+r" for the reload, but closed the whole thing and started fresh with "Launch Project". So, unless I am missing something, there should be another reason why one of them shows and one doesn't. Again, nothing I have a practical need for, so I still consider this solved and just for my theoretical understanding, but it does seem like something that could be worth understanding, especially if I had made yet another mistake in there (which hopefully isn't in my perception and I just seen it wrong).

User avatar
Ocelot
Lemma-Class Veteran
Posts: 2384
Joined: Tue Aug 23, 2016 10:35 am
Github: MiiNiPaa
Discord: MiiNiPaa#4384
Contact:

Re: just setting variable to True only affects - see code

#8 Post by Ocelot »

IDK, everything is working fine for me:
https://i.imgur.com/FkcP2qA.gif
< < insert Rick Cook quote here > >

CSK
Newbie
Posts: 16
Joined: Sat Sep 10, 2022 2:42 pm
Contact:

Re: just setting variable to True only affects - see code

#9 Post by CSK »

Ocelot wrote: Tue Sep 27, 2022 10:36 am IDK, everything is working fine for me:
https://i.imgur.com/FkcP2qA.gif
This lead to trying it again and then creating a new project to test it, as I had just used a test.rpy (hence the "label test4:") file to jump to and commenting everything in the functions file and whatever else may have brought up errors. In the new project, as intended, in my own, still not. So I kept commenting whole files and trying, while hoping that it's not something I written or changed in the screens or gui or whatnot, until I finally found it in another test file, in which I had done the shortened pre "true / false" version, also with TestFun and tfperson, still with defined, but without this leading to an error message for some reason, and while changing it to default lead to an error, switching it to tfperson1 did not, showing that this was the line causing the issue.

Thank you SO much for putting in the effort to run this. Not just that I now know my mistake and all the potential issues this may have caused when working with Renpy, but especially because of potential future questions - annoying people and wasting their time over several pages in a thread in which I cry for help for a problem because I was too stupid to use a new project to test it in - glad I had this now that I am still a beginner and in a relatively short thread, future questions will have gone through testing in a new project before posting them. So, big thanks. Issue completely solved.

Post Reply

Who is online

Users browsing this forum: Google [Bot]