Works in Python, Doesn't Work in RenPy Python block... (solved)

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
User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Works in Python, Doesn't Work in RenPy Python block... (solved)

#1 Post by noeinan »

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:

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
Here is where I am using the WeightedRandom function:

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
Here is the part of my screen that loop creates buttons based on the random number:

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()
Here is my traceback:

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
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:

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)
And this works! But for some reason, when that same code is in RenPy, it doesn't work.

Any help is greatly appreciated!
Last edited by noeinan on Tue Apr 05, 2022 11:36 am, edited 1 time in total.
Image

Image
Image

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

Re: Works in Python, Doesn't Work in RenPy Python block...

#2 Post by Ocelot »

Code you posted works fine within RenPy. Did you check value of top_artificial_dimensions_number at crash point?
< < insert Rick Cook quote here > >

User avatar
noeinan
Eileen-Class Veteran
Posts: 1153
Joined: Sun Apr 04, 2010 10:10 pm
Projects: Ren'Py QuickStart, Crimson Rue
Organization: Statistically Unlikely Games
Deviantart: noeinan
Github: noeinan
Location: Washington State, USA
Contact:

Re: Works in Python, Doesn't Work in RenPy Python block...

#3 Post by noeinan »

Okay, weirdest thing. I slept overnight and suddenly I'm not getting the error message anymore. I actually fully exited RenPy and opened it back up and still got the error. But now it suddenly stopped. I have no idea what did this, but you know what, I'll take it. Maybe it was holding onto some persistent data or something, who knows.
Image

Image
Image

Post Reply

Who is online

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