I'm trying to make a bar which value is a VariableValue. Depending on the value CUSTOM_ITEM_PRICE
, a list of DISPLAY_PERCENTAGES
changes.
To explain what I'm trying to do, I'm making a shop system where instead of just buying an item, you put an upfront investment and the shop will return an item of varying quality to you. The higher the investment you put, the more likely you'll get a better quality item. There are 6 qualities: Cheap, Simple, Fine, Elegant, Ornate, Masterwork.
Here is the code for calculating the DISPLAY_PERCENTAGES
:
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)
}
CUSTOM_ITEM_PRICE = 350 #ill change this into an input number based on hbar in game frontend later
DISPLAY_PERCENTAGES = [0, 0, 0, 0, 0, 0]
def count_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
DISPLAY_PERCENTAGES
will return as a list of six items corresponding to the quality of the item. If you put 350 gold, the DISPLAY_PERCENTAGES
should be [92, 8, 0, 0, 0, 0]
. I want to display that list on a screen.
This is the code for the displaying the screen
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
#Important line below, I suppose
value VariableValue("CUSTOM_ITEM_PRICE", 4650, offset=350, action=Function(count_custom_chance, CUSTOM_ITEM_PRICE), 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
The problem is when first showing the screen, all values of DISPLAY_PERCENTAGES
is its default:
After moving the bar value, DISPLAY_PERCENTAGES
updates itself but the values are wrong:
The correct values for putting 450 gold should be [76.73, 23.27, 0, 0, 0, 0]
.
And finally the most mind-boggling from me is when I play around with the bar, the values get really messed up. Here is the values after going back to the default 350 gold:
Many thanks beforehand for any advice. I'm still on my second month in learning code and renpy, so hopefully my code isn't just a jumbly mess.