Works in Python, Doesn't Work in RenPy Python block... (solved)
Posted: Mon Apr 04, 2022 3:02 pm
I am using this code to generate weighted random numbers: https://www.renpy.org/wiki/renpy/doc/co ... han_others
My Git is public, if you would like download and try yourself: https://github.com/noeinan/Fucksekai
I did change the names a little to make it make sense to me but I didn't change how the code works.
I am creating a screen that exists while renpy is paused to allow players to click the image buttons outside of dialogue. I'm using weighted random to determine how many buttons to generate on this screen.
Here's the WeightedRandom function:
Here is where I am using the WeightedRandom function:
Here is the part of my screen that loop creates buttons based on the random number:
Here is my traceback:
What I don't understand is, I am feeding the function a list made up of tuples. The values stored inside each tuple are both integers. So when the function pulls out one of the values, it **should** still be an integer. I don't understand why it isn't one.
I previously asked for help on the python discord, but the person who helped me thought the issue must be RenPy related. Here's the link to that: https://discord.com/channels/2676243358 ... 5887279194
I tested this code in only python using this: https://www.w3schools.com/python/trypyt ... om_choice2
I erased their code and put my own:
And this works! But for some reason, when that same code is in RenPy, it doesn't work.
Any help is greatly appreciated!
My Git is public, if you would like download and try yourself: https://github.com/noeinan/Fucksekai
I did change the names a little to make it make sense to me but I didn't change how the code works.
I am creating a screen that exists while renpy is paused to allow players to click the image buttons outside of dialogue. I'm using weighted random to determine how many buttons to generate on this screen.
Here's the WeightedRandom function:
Code: Select all
init python:
class WeightedRandom(object):
def __init__(self, list_of_values_and_weights):
"""
expects a list of [ (value, weight), (value, weight),...]
"""
self.the_list = list_of_values_and_weights
self.the_sum = sum([ v[1] for v in list_of_values_and_weights])
def pick(self):
"""
return a random value taking into account the weights
"""
import random
r = random.uniform(0, self.the_sum)
s = 0.0
for k, w in self.the_list:
s += w
if r < s: return k
return k
Code: Select all
label multiverse_loop:
$ renpy.pause()
jump multiverse_loop
label multiverse:
python:
# updating global variables
current_loc = "multiverse void"
# defining local variables
top_artificial_dimensions_number = 0
i = 0
# initialize our random picker with the options and their likelyhood of being selected:
n = WeightedRandom( [(0, 50), (1,25), (2,10), (3,8), (4,6), (5,1)] )
# Now generate a random item from the list
top_artificial_dimensions_number = n.pick()
show screen multiverse_generator()
jump multiverse_loop
Code: Select all
hbox:
xpos 20 ypos 0
spacing -200
for i in range (top_artificial_dimensions_number):
imagebutton auto "gui/multiverse/dimension_top_artificial_base_%s.png" focus_mask True action MainMenu()
Code: Select all
While running game code:
File "game/code/multiverse-generator-screen.rpy", line 16, in script
$ renpy.pause()
File "game/code/multiverse-generator-screen.rpy", line 16, in <module>
$ renpy.pause()
File "game/code/multiverse-generator-screen.rpy", line 51, in execute
screen multiverse_generator():
File "game/code/multiverse-generator-screen.rpy", line 51, in execute
screen multiverse_generator():
File "game/code/multiverse-generator-screen.rpy", line 80, in execute
hbox:
File "game/code/multiverse-generator-screen.rpy", line 84, in execute
for i in range (top_artificial_dimensions_number):
File "game/code/multiverse-generator-screen.rpy", line 84, in <module>
for i in range (top_artificial_dimensions_number):
TypeError: an integer is required
I previously asked for help on the python discord, but the person who helped me thought the issue must be RenPy related. Here's the link to that: https://discord.com/channels/2676243358 ... 5887279194
I tested this code in only python using this: https://www.w3schools.com/python/trypyt ... om_choice2
I erased their code and put my own:
Code: Select all
class WeightedRandom(object):
def __init__(self, list_of_values_and_weights):
"""
expects a list of [ (value, weight), (value, weight),...]
"""
self.the_list = list_of_values_and_weights
self.the_sum = sum([ v[1] for v in list_of_values_and_weights])
def pick(self):
"""
return a random value taking into account the weights
"""
import random
r = random.uniform(0, self.the_sum)
s = 0.0
for k, w in self.the_list:
s += w
if r < s: return k
return k
top_artificial_dimensions_number = 0
i = 0
x = 0
n = WeightedRandom( [(0, 50), (1,25), (2,10), (3,8), (4,6), (5,1)] )
top_artificial_dimensions_number = n.pick()
print("Weighted Random Number: ")
print(top_artificial_dimensions_number)
for i in range (top_artificial_dimensions_number):
x += 1
print("Number of Buttons Generated: ")
print(x)
Any help is greatly appreciated!