[SOLVED] VariableValue delay when moving bar quickly/not showing the correct values?

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
Deimos96
Regular
Posts: 26
Joined: Tue Nov 15, 2022 5:34 am
Contact:

[SOLVED] VariableValue delay when moving bar quickly/not showing the correct values?

#1 Post by Deimos96 »

Hello I'm trying to develop a system of chance based on a given number by the player.

To explain as briefly as I can; There is a shop system in the game, you can "custom order" items if the item you are looking for isn't in the shop inventory during your visit (it reshuffles every week). This custom order takes whatever amount you want to invest in from 350G to 5000G using a bar VariableValue function. The more you invest the better chance the quality outcome will be. So if you invest 350G you will get "cheap" quality, invest 5000G you will likely get "masterwork" quality.

The Function Code:

Code: Select all

QUALITY_TO_INDEX = {
            "Cheap": 0,
            "Simple": 1,
            "Fine": 2,
            "Elegant": 3,
            "Ornate": 4,
            "Masterwork": 5
} 

APPAREL_MIN_MAX = {
            "Cheap": (28, 2000),
            "Simple": (322, 3500),
            "Fine": (600, 5000),
            "Elegant": (1200, 6000),
            "Ornate": (2500, 7000),
            "Masterwork": (4000, 6000)
}

DISPLAY_PERCENTAGES = [0, 0, 0, 0, 0, 0]
    
def custom_chance(price):
    AVAIL_QUALITIES = []        
    POSSIBLE_PERCENTAGES = []               
    CUSTOM_ORDER_WEIGHTS = []

    for key, value in APPAREL_MIN_MAX.items():
        min_value = value[0]
        if price > min_value:
            AVAIL_QUALITIES.append(key)

    min_value = [tup[0] for tup in APPAREL_MIN_MAX.values()]
    max_value = [tup[1] for tup in APPAREL_MIN_MAX.values()]

    idx = QUALITY_TO_INDEX[AVAIL_QUALITIES[0]]

    count = 0
    while count < len(AVAIL_QUALITIES):
        qual_min_weight = price - min_value[idx]
        qual_max_weight = price - max_value[idx]
        idx += 1
        count += 1

        qweight = min([qual_min_weight, abs(qual_max_weight)])
        CUSTOM_ORDER_WEIGHTS.append(qweight)

    for weight in CUSTOM_ORDER_WEIGHTS:
        percent = (weight/(sum(CUSTOM_ORDER_WEIGHTS)))*100
        POSSIBLE_PERCENTAGES.append(percent)

    count = 0
    for quality in AVAIL_QUALITIES:
        DISPLAY_PERCENTAGES[QUALITY_TO_INDEX.get(quality)] = round(POSSIBLE_PERCENTAGES[count], 2)
        count += 1

    renpy.show_screen("tailor_hud")
The Screen:

Code: Select all

screen shop_list():

    tag first

    text "Choose where to shop." xoffset glist_posX+290 yoffset glist_posY+22 font "font/maian.ttf" size 18

    hbox:
        xoffset glist_posX+12
        yoffset glist_posY+15
        spacing 11
        imagebutton:
            auto "tailor_btn_%s.png"
            action (Function(custom_chance(CUSTOM_ITEM_PRICE), SetScreenVariable("selected_shop", "tailor")))
        imagebutton: #ignore this for now
            auto "merchant_btn_%s.png"
            action (Show("merchant_hud"), SetScreenVariable("selected_shop", "merchant"))

screen tailor_hud():
    
    tag second

    add "gui/main_hud/tailor_hud.jpg" xoffset glist_posX yoffset glist_posY+62

    bar:
        value VariableValue("CUSTOM_ITEM_PRICE", 4650, offset=350, step=50, force_step=True)
        xoffset glist_posX+32
        yoffset glist_posY+560
        base_bar "gui/bar/shop_price.png"
        thumb "gui/bar/mini_thumb.png"
        thumb_offset 7
        xysize (380, 30)
        left_gutter 10
        right_gutter 22

    text "[CUSTOM_ITEM_PRICE]" xoffset glist_posX+178 yoffset glist_posY+528 size 21 font "font/maian.ttf" color gold_color

    vbox:
        for txt in DISPLAY_PERCENTAGES:
            text str(txt)
        xalign 0.5
        yalign 0.5
TL;DR I'm trying to get a list of DISPLAY_PERCENTAGES to show up and update every time. This is what I get in my game:
Image

At first this is the correct output. But when I slide my slider the numbers go all wonky. This is after sliding it to 750G. The numbers here are already wrong as some chances should even be 0%.:
Image

And sliding it back again to 350G. In theory, it should go back to 92% and 8%, but it just messes up:
Image

Sorry for the long thread but I've spent a couple days trying to figure this out and it sure is frustrating lol. Very much thanks to anyone who has read my long ass thread and is willing to give advice. ^^~
Last edited by Deimos96 on Mon Jan 09, 2023 10:32 pm, edited 3 times in total.

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

Re: VariableValue not showing the correct values?

#2 Post by Ocelot »

One thing I noticed immideately: nothing changes DISPLAY_PERCENTAGES when CUSTOM_ITEM_PRICE is changed by bar. Notmally, you would notice that problem right away, because nothing would change, but another mistake prevents you from doing that: Function(custom_chance(CUSTOM_ITEM_PRICE), ... should be:

Code: Select all

Function(custom_chance, CUSTOM_ITEM_PRICE)
In short, you are not calling function here, you are storing function and values which will be invoked, when button is pressed. Slightly longer explanation is here:
viewtopic.php?p=557062#p557062

This causes values to update only when parent screen would be redrawn or predicted for some reason. Which is not often.

Also, this very mistake? It hides yet another mistake, which otherwise would crash the game. Your parentheses are wrong, SetScreenVariable("selected_shop", "tailor") should not be an argument to Function.
< < insert Rick Cook quote here > >

Deimos96
Regular
Posts: 26
Joined: Tue Nov 15, 2022 5:34 am
Contact:

Re: VariableValue not showing the correct values?

#3 Post by Deimos96 »

Ocelot wrote: Wed Jan 04, 2023 7:19 am One thing I noticed immideately: nothing changes DISPLAY_PERCENTAGES when CUSTOM_ITEM_PRICE is changed by bar. Notmally, you would notice that problem right away, because nothing would change, but another mistake prevents you from doing that: Function(custom_chance(CUSTOM_ITEM_PRICE), ... should be:

Code: Select all

Function(custom_chance, CUSTOM_ITEM_PRICE)
In short, you are not calling function here, you are storing function and values which will be invoked, when button is pressed. Slightly longer explanation is here:
viewtopic.php?p=557062#p557062

This causes values to update only when parent screen would be redrawn or predicted for some reason. Which is not often.

Also, this very mistake? It hides yet another mistake, which otherwise would crash the game. Your parentheses are wrong, SetScreenVariable("selected_shop", "tailor") should not be an argument to Function.
Thanks for the quick reply. I really messed up with the Function syntax. I'll try and study up the link you shared. I gotta admit, studying screens is probably one of the hardest things in Renpy aside from the python code itself.

Deimos96
Regular
Posts: 26
Joined: Tue Nov 15, 2022 5:34 am
Contact:

Re: VariableValue not showing the correct values?

#4 Post by Deimos96 »

Okay so I've updated some code but the results are still kinda weird.

I'm still wondering if its possible for VariableValue to experience lag/delay? Check out the video below for a clearer detail. When I move the bar quickly, the results become wrong.
https://youtu.be/fGTomapSFk0

Here's the updated code:

Code: Select all

    def update_custom():

        avail_qualities = []

        for key, value in APPAREL_MIN_MAX.items():
            min_value = value[0]
            max_value = value[1]
            if CUSTOM_ITEM_PRICE >= min_value and CUSTOM_ITEM_PRICE <= max_value:
                avail_qualities.append(key)

        custom_order_weights = []

        min_value = [tup[0] for tup in APPAREL_MIN_MAX.values()]
        max_value = [tup[1] for tup in APPAREL_MIN_MAX.values()]

        idx = QUALITY_TO_INDEX[avail_qualities[0]]
        count = 0
        while count < len(avail_qualities):
            qual_min_weight = CUSTOM_ITEM_PRICE - min_value[idx]
            qual_max_weight = CUSTOM_ITEM_PRICE - max_value[idx]
            idx += 1
            count += 1
            
            qweight = min([qual_min_weight, abs(qual_max_weight)])
            custom_order_weights.append(qweight)

        possible_percentages = []

        for weight in custom_order_weights:
            percent = (weight/(sum(custom_order_weights)))*100
            possible_percentages.append(percent)

        idx = 0
        for quality in avail_qualities:
            DISPLAY_PERCENTAGES[QUALITY_TO_INDEX.get(quality)] = round(possible_percentages[idx], 2)
            idx += 1
Screen code:

Code: Select all

screen tailor_hud():
    
    tag second

    add "gui/main_hud/tailor_hud.jpg" xoffset glist_posX yoffset glist_posY+62

    bar:
        xoffset glist_posX+32
        yoffset glist_posY+560
        base_bar "gui/bar/shop_price.png"
        thumb "gui/bar/mini_thumb.png"
        thumb_offset 7
        xysize (380, 30)
        left_gutter 10
        right_gutter 22
        value VariableValue("CUSTOM_ITEM_PRICE", 4650, offset=350, action=Function(update_custom), step=50, force_step=True)

    text "[CUSTOM_ITEM_PRICE]" xoffset glist_posX+178 yoffset glist_posY+528 size 21 font "font/maian.ttf" color gold_color

    vbox:
        for txt in DISPLAY_PERCENTAGES:
            textbutton str(txt):
                action NullAction()
        xalign 0.3
        yalign 0.5

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

Re: VariableValue delay when moving bar quickly/not showing the correct values?

#5 Post by Ocelot »

Now you have something to update the value, but I am not sure that screen is completely updated, since string substiturions are generally considered const.

Try:
1) replace text "[CUSTOM_ITEM_PRICE]" with text str(CUSTOM_ITEM_PRICE).
2) if previous didn't work, add call to renpy.restart_interaction at the end of bar action.
< < insert Rick Cook quote here > >

Deimos96
Regular
Posts: 26
Joined: Tue Nov 15, 2022 5:34 am
Contact:

Re: VariableValue delay when moving bar quickly/not showing the correct values?

#6 Post by Deimos96 »

Ocelot wrote: Thu Jan 05, 2023 8:05 am Now you have something to update the value, but I am not sure that screen is completely updated, since string substiturions are generally considered const.

Try:
1) replace text "[CUSTOM_ITEM_PRICE]" with text str(CUSTOM_ITEM_PRICE).
2) if previous didn't work, add call to renpy.restart_interaction at the end of bar action.
For number 2 just plop it at the end of the update_custom function? The results are still similar or I did something wrong lol.

Currently checking out this thread: viewtopic.php?t=32511, seems to have a simlar probelm

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

Re: VariableValue delay when moving bar quickly/not showing the correct values?

#7 Post by Ocelot »

One thing I noticed: it seems that not all qualities are processed by your function. If you do not process them, they will not change. If certain quality was set to some value and then you moved bar to the position, where it is not processed, it won't change. Bar only processes values on which you stopped at least for a moment.
Your function should set all data for each possible value.

If you want actual debugging, you will need to produce a minimal example: a code, which would launch when someone would create a new project and replace script.rpy content with it, and would exhibit your problems.
< < insert Rick Cook quote here > >

Deimos96
Regular
Posts: 26
Joined: Tue Nov 15, 2022 5:34 am
Contact:

Re: [SOLVED] VariableValue delay when moving bar quickly/not showing the correct values?

#8 Post by Deimos96 »

OKAY so after about a week I've spent thinking of some overly complex way to solve this problem. I've tried learning things like threading, multiprocessing, throttling and some renpy screen stuff thinking that it might fix my problem. None of them worked. What did work was a simple tweak to my code. All I had to do was refresh [DISPLAY_PERCENTAGES] in the func like so (i changed CUSTOM_ITEM_PRICE to INVESTMENT):

Code: Select all

def update_custom():
        global DISPLAY_PERCENTAGES
        DISPLAY_PERCENTAGES = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
        avail_qualities = []

        for key, value in APPAREL_MIN_MAX.items():
            min_value = value[0]
            max_value = value[1]
            if INVESTMENT >= min_value and INVESTMENT <= max_value:
                avail_qualities.append(key)

        custom_order_weights = []

        min_val_list = [tup[0] for tup in APPAREL_MIN_MAX.values()]
        max_val_list = [tup[1] for tup in APPAREL_MIN_MAX.values()]

        idx = QUALITY_TO_INDEX[avail_qualities[0]]
        count = 0
        while count < len(avail_qualities):
            qual_min_weight = INVESTMENT - min_val_list[idx]
            qual_max_weight = INVESTMENT - max_val_list[idx]
            idx += 1
            count += 1
            
            qweight = min([qual_min_weight, abs(qual_max_weight)])
            custom_order_weights.append(qweight)

        possible_percentages = []

        for weight in custom_order_weights:
            percent = (weight/(sum(custom_order_weights)))*100
            possible_percentages.append(percent)

        idx = 0
        for quality in avail_qualities:
            DISPLAY_PERCENTAGES[QUALITY_TO_INDEX.get(quality)] = round(possible_percentages[idx], 2)
            idx += 1
I'm relieved and so frustrated at the same time. At least I've learned some new python tricks along the way... :lol:

Post Reply

Who is online

Users browsing this forum: Houseofmine