Issue with comparing variables

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
Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Issue with comparing variables

#1 Post by Errilhl »

So, I've got a weird issue.
I have the following code, modifying some stats based on randomness, for the most part.

Code: Select all

    label w_mc():
        if not backpack.has_item(toolbox_item):
            if toolbox_added:
                $ backpack.add_item(toolbox_item)
                $ toolbox_added = False

        if current_hour[:2] == '06':
            $ maxcount = 1
        elif current_hour[:2] > '14' and current_hour[:2] < '22':
            $ maxcount = 2

        label repeat_event:
        if current_hour[:2] == '06' and day_week <= 4:
            $ choice_1 = "Hm... I have at least an hour. Maybe I can get a bit done before going to school"
            $ choice_2 = "Or, I could just leave early, give myself a bit of extra time"
            $ event = 1
        elif current_hour[:2] == '06':
            $ choice_1 = "Hm... I have at least an hour before anyone else is up. I can probably get a bit done before breakfast"
            $ choice_2 = "Or... I could probably just go get breakfast first, then come back here later"
            $ event = 2
        elif '14' < current_hour[:2] < '22':
            $ choice_1 = "I can probably do at least a couple hours of bike repair today"
            $ choice_2 = "Or I could go back into the house, relax on the couch for a bit..."
            $ event = 3
        else:
            $ choice_1 = "Hm. I have the entire day off. Maybe I should spend some time on the bike, see if I can get some traction on the rebuild"
            $ choice_2 = "Or, I could just slack off today, and work on the bike another day..."
            $ event = 4
        menu:
            "[choice_1]":
                $ goto = 1
            "[choice_2]":
                $ goto = 2

        # if goto == 1:
            # if event == 1:
            #     call addtime(1, False)
            #     $ count = 3
            # elif event == 2:
            #     $ count = 1
        # elif goto == 2:
        if goto == 2:
            if event == 2:
                jump breakfast_interaction
            else:
                jump front_door

        if count <= maxcount:
            $ count += 1
            call addtime(1,False) from _call_addtime_27
            if count == 1:
                $ sc = str(count) + " hour"
            else:
                $ sc = str(count) + " hours"
            "I've already worked on this bike for [sc] today"
            if count >= maxcount:
                if debug:
                    "count vs maxcount"
                if int(current_hour[:2]) == int('07') and day_week <= 4:
                    fP "I should get ready for school"
                    jump after_fS_mad_morning
                elif int(current_hour[:2]) == int('07'):
                    fP "I should go get some breakfast"
                    jump breakfast_interaction
                else:
                    fP "It's late, and probably time to call it a day second"
                    label end_bike_repair_daily:
                        $ end_bike_repair = True
                        jump front_door
            else:
                jump update_stat
        else:
            fP "It's late, and probably time to call it a day third"
            jump front_door

        label update_stat:
            $ c = 0
            $ mcI = False
            if mc_b >= 40 and mc_b < 100:
                $ c = .35
            elif mc_b >= 100 and mc_b <= 150:
                $ c = .25
            else:
                $ c = .5
            if renpy.random.random() > c:
                if mc_b <= 40:
                    $ mcI = 1
                    if backpack.has_item(toolbox_item):
                        $ mcI = 2
                elif mc_b <= 75:                        
                    $ mcI = .5
                    if backpack.has_item(toolbox_item):
                        $ mcI = 1
                else:
                    $ mcI = .25
                    if backpack.has_item(toolbox_item):
                        $ mcI = .5
            if mcI:
                $ statschangeNotify("mc_b",mcI)
                call addtime(1,False) from _call_addtime_28
                jump repeat_event
            else:
                $ renpy.notify("This time, you didn't manage to improve the status of your bike")
                call addtime(1,False) from _call_addtime_29
                jump repeat_event
       
            return
The issue is as follows. Lets for this purpose say I start with
count = 0
maxcount = 1
(seen in shift+d as starting points for the variables)

The first if triggers without problem, the

Code: Select all

if count <= maxcount:
Right after that, we add 1 to count, so the current variable count is 1

But, when it reaches this:

Code: Select all

 if count >= maxcount:
it never triggers. It's... really weird. Anyone see anything that might be wrong with the code? The weird part is... this worked perfectly. I have not, as far as I know, changed anything that has anything to do with these things.

What happens now, instead of going into the inner if, it just continues, starts over (loops), and end up on the "else" of the menu-choices in the beginning.
Currently working on: Image

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

Re: Issue with comparing variables

#2 Post by Ocelot »

Pay more attention to control flow:

Code: Select all

if count <= maxcount:
    # Everything from here to the same-level else
    # will be executed only if condition count <= maxcount is true

    # Skipped

    # Including this line: This condition will be checked 
    # ONLY if count <= maxcount 
     if count >= maxcount:

        # Skipped

    else:
        jump update_stat
else:
    fP "It's late, and probably time to call it a day third"
    jump front_door
< < insert Rick Cook quote here > >

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Issue with comparing variables

#3 Post by Errilhl »

How...? I thought the logic would continue to flow since the if has already been checked? ie, you're within that if? Does it keep that if "in memory" so to speak, and jump out of it if it no longer matches?

However, regardless of that, this should still run, given that it checks if smaller or equal - it's still equal to, so it should still run. Ie, basically, both of those statements are true.

if smaller than OR equal to == True, given 1 and 1 for both variables.
if bigger than OR equal to == True, given 1 and 1 for both variables.
Currently working on: Image

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

Re: Issue with comparing variables

#4 Post by Ocelot »

Yes, you are right. I didn't pay enough attention.
Did you check values on "I've already worked on this bike for [sc] today" line? Are they correct?
< < insert Rick Cook quote here > >

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Issue with comparing variables

#5 Post by Errilhl »

Yes, that's the weird part. Looking at the variables with shift+d, before entering the loop, count = 0, and maxcount = 1 (for this instance) - and after adding one, count = 1, and maxcount = 1. There is nothing else anywhere that should modify those variables... at all. I'm gonna tear this apart, and try again, because... this is really weird. And, it worked earlier. I'm sort of trying to tweak my brain about whether or not I could've done something else that has fucked up this piece of code, but... I just can't get where that should be. This "function" doesn't call anything (apart from the add_time(), but that doesn't affect anything inside this label) so... yeah. It's very weird.
Currently working on: Image

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Issue with comparing variables

#6 Post by Errilhl »

Okay, update...

New code (it's mostly the same, just done some modifications, and tried to change things around a little bit) (some new information at the bottom):

Code: Select all

    label w_mc():
        # $ maxc = 1 if int(current_hour[:2]) == 6 else 2 if int(current_hour[:2]) > 14 and int(current_hour[:2]) < 22 and day_week <= 4 else 3
        $ maxc = 2

        if not backpack.has_item(toolbox_item):
            if toolbox_added:
                $ backpack.add_item(toolbox_item)
                $ toolbox_added = False

        if int(current_hour[:2]) == 6 and day_week <= 4:
            $ choice1 = "Hm... I have at least an hour before school. Maybe I can get a bit done on the bike"
            $ choice2 = "Or, I could just go back inside, eat breakfast and leave early"
            $ event = 1
        elif int(current_hour[:2]) == 6 and day_week >= 5:
            $ choice1 = "Hm... I have at least an hour before anyone else is out of bed. I can probably get a bit done before breakfast"
            $ choice2 = "Or... I could go back inside, take a shower, get some breakfast, and get back out here later"
            $ event = 2
        elif 14 < int(current_hour[:2]) < 20:
            $ choice1 = "I can probably do at least a couple hours of bike repair today"
            $ choice2 = "Or I could go back in the house, see if there's anything on TV, or play a game..."
            $ event = 3
        else:
            $ choice1 = "I have the entire day off. Maybe I could spend some time on the bike, see if I can get some traction on the rebuild"
            $ choice2 = "Or, I could just slack off today, and work on the bike another day"
            $ event = 4

        menu:
            "[choice1]":
                $ goto = 1
            "[choice2]":
                $ goto = 2

        if goto == 2:
            if event == 2:
                jump breakfast_interaction
            else:
                jump front_door

        label .repeat_event:
        if count <= maxc:
            $ count += 1
            call addtime(1, False)
            $ sc = "{0} hour{1}".format(count,"" if count == 1 else "s")
            "I've already worked on this bike for [sc] today"
            if count >= maxc:
                if int(current_hour[:2]) == 7 and day_week <= 4:
                    fP "I should get ready for school"
                    jump after_fS_mad_morning
                elif int(current_hour[:2]) == 7:
                    fP "I should go get some breakfast"
                    jump breakfast_interaction
                else:
                    fP "I'm spent. Probably time to call it a day"
                    $ end_bike_repair = True
                    jump front_door
            else:
                "testing"
                jump .update_stat

        label .update_stat:
        $ c = 0
        $ mcu = False
        if mc_b >= 50 and mc_b < 100:
            $ c = .35
        elif mc_b >= 100 and mc_b <= 150:
            $ c = .25
        else:
            $ c = .5
        if renpy.random.random() > c:
            if mc_b <= 49:
                $ mcu = .5 if backpack.has_item(toolbox_item) else .25
            elif mc_b <= 99:
                $ mcu = 1 if backpack.has_item(toolbox_item) else .5
            else:
                $ mcu = 2 if backpack.has_item(toolbox_item) else 1
        if mcu:
            $ statschangeNotify('mc_b',mcu)
            call addtime(1, False)
            $ mcu = False
            jump .repeat_event
        else:
            $ renpy.notify("You didn't manage to improve the state of your bike this time")
            call addtime(1, False)
            $ mcu = False
            jump .repeat_event
Okay. What happens (I think) is this:
When you first enter the w_mc() label, it runs through, as it should. It picks the menu-choices to display based on other variables (this works), and after you've chosen the correct choice, it shows the "You've already worked for X hours on the bike today"-line. After that is where it gets funky.

It seems that even though it seemingly goes throughthe update_stat-label, it doesn't actually do anything - neither the statchangeNotify() returns anything (a renpy.notification if it anything happened), or the renpy.notify("you didn't repair anything") - neither of those gets triggered. Nor does it loop to the .repeat_event label - it just starts the whole thing again, from the beginning of w_mc(), displaying the menu again (basically the last choice on the menu, since that's the only one without criteria).

Again, I've tested a few different things, moved the labels and the content about, added returns for each of the sub-labels, tested making the sub-labels local... nothing works. And I'm still at a loss as to why this is a problem. I have reverted the code back to before adding the returns and moving the indentation for the labels - it didn't do anything either way.

Still looking for ideas as to why this happens, and why it doesn't respect the labels within the label itself, or the if-statements, or the jumps. Also, just to make sure there wasn't anything "hiding" somewhere, I've written it all over again (started a new), just typed out everything I wanted to keep from the old version, modified some phrases, recalculated rewards and such - but it doesn't seem to be anything like that, because it still doesn't work as it should.
Currently working on: Image

User avatar
trooper6
Lemma-Class Veteran
Posts: 3712
Joined: Sat Jul 09, 2011 10:33 pm
Projects: A Close Shave
Location: Medford, MA
Contact:

Re: Issue with comparing variables

#7 Post by trooper6 »

I never nest labels. Nowhere in the official Renpy examples or documentation is there an instance of nested labels. If you note the documentation on labels, even when they mention the local labels...they are all in the outermost indentation and none of them are nested.

Also if your code example is accurate, you haven't properly indented the things that are supposed to be inside .repeat_event and inside .update_stat. I'd work on the indentation first.
A Close Shave:
*Last Thing Done (Aug 17): Finished coding emotions and camera for 4/10 main labels.
*Currently Doing: Coding of emotions and camera for the labels--On 5/10
*First Next thing to do: Code in all CG and special animation stuff
*Next Next thing to do: Set up film animation
*Other Thing to Do: Do SFX and Score (maybe think about eye blinks?)
Check out My Clock Cookbook Recipe: http://lemmasoft.renai.us/forums/viewto ... 51&t=21978

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Issue with comparing variables

#8 Post by Remix »

trooper6 wrote: Wed Dec 20, 2017 3:56 amI'd work on the indentation first.
That was my first thought when I looked at the code...

Errilhl, is there a highly valid reason for using local rather than global labels? Are all the relevant variables local too or are they 'defaulted' in the global scope? (truth be told, I do not know if Ren'py has a global/local namespace for label variables like it does for screens... It could easily be the cause of the problem if it does though)

Realistically, unless you *need* locally scoped labels I would suggest not using that approach. Most things that do use it really do not need it and it just ends up making the code one step harder to work with.
Frameworks & Scriptlets:

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Issue with comparing variables

#9 Post by Errilhl »

1. Labels do not need to have a block-indentation after it - they are, however, indented properly for clarity in the actual code - not sure why that is messed up here.
2. The local label thing was a test, just to see if it made a difference. It doesn't. They were originally global labels.

As for the nesting of labels - this is the weird part. This thing worked fine. Then I must have done _something_ to upset it, because it no longer works fine :D

EDIT
Currently trying to mix together two different versions of the script.rpy, to see if I can learn where the problem originated from - one from when this was not an issue, and another the newest one - mostly just fixing different call-methods and adding / changing pure content, to see if I can get it down to something that works, before trying to add in different things on top of that.
Currently working on: Image

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Issue with comparing variables

#10 Post by Errilhl »

Hm, gonna update this. Seems there is an issue with something else in the game, that causes this (running the function / label within a clean Renpy-setup "works", in that it does the loops properly and exits out of it). However, for some reason, this thing does not work within my game. Every other loop and / or similar construct works, so I'm a little at a loss as to where it sort of "short circuits". I will keep digging.
Currently working on: Image

User avatar
Remix
Eileen-Class Veteran
Posts: 1628
Joined: Tue May 30, 2017 6:10 am
Completed: None... yet (as I'm still looking for an artist)
Projects: An un-named anime based trainer game
Contact:

Re: Issue with comparing variables

#11 Post by Remix »

Yeah, ignore my last remark anyway... found out in another thread that nesting labels does not make them local to the parent label, they are all global unless expressly defined using the .name syntax.
Frameworks & Scriptlets:

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]