[Solved] Selecting a class object at random, but not as clumsy

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
dellcartoons
Regular
Posts: 29
Joined: Sat Jul 13, 2019 3:35 pm
Contact:

[Solved] Selecting a class object at random, but not as clumsy

#1 Post by dellcartoons »

I have some class objects and I want to select one at random so I can manipulate it

This code technically works, but there HAS to be a more elegant way

Code: Select all




init python:
    class Traits(object):
        def __init__(self, *args, **kwargs):
            self.__dict__.update(kwargs)




init python:

    class TraitSelection(Traits):

        STAT_DEFAULTS = {
            't1' : 0,
            't2' : 0,
            't3' : 0,
            't4' : 0,
            't5' : 0,
        }



default s1 = TraitSelection("s1", t1 = 1, t2 = 1, t3 = 1, t4 = 1, t5 = 1)
default s2 = TraitSelection("s2", t1 = 1, t2 = 2, t3 = 2, t4 = 2, t5 = 2)
default s3 = TraitSelection("s3", t1 = 1, t2 = 3, t3 = 3, t4 = 3, t5 = 3)
default s4 = TraitSelection("s4", t1 = 2, t2 = 1, t3 = 4, t4 = 4, t5 = 4)
default s5 = TraitSelection("s5", t1 = 3, t2 = 1, t3 = 5, t4 = 5, t5 = 5)
default s6 = TraitSelection("s6", t1 = 4, t2 = 4, t3 = 1, t4 = 6, t5 = 6)
default s7 = TraitSelection("s7", t1 = 5, t2 = 5, t3 = 1, t4 = 7, t5 = 7)
default s8 = TraitSelection("s8", t1 = 6, t2 = 6, t3 = 6, t4 = 1, t5 = 8)
default s9 = TraitSelection("s9", t1 = 7, t2 = 7, t3 = 7, t4 = 1, t5 = 9)
default s10 = TraitSelection("s10", t1 = 8, t2 = 8, t3 = 8, t4 = 8, t5 = 1)
default s11 = TraitSelection("s11", t1 = 9, t2 = 9, t3 = 9, t4 = 9, t5 = 1)


define trait_list = ["s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8", "s9", "s10", "s11"]

label start:

    "I need to make a random choice from the trait list"

    $ random_choice =  renpy.random.choice(trait_list)

 

    if random_choice == "s1":
        $ random_choice=s1

    if random_choice == "s2":
        $ random_choice=s2

    if random_choice == "s3":
        $ random_choice=s3

    if random_choice == "s4":
        $ random_choice=s4

    if random_choice == "s5":
        $ random_choice=s5

    if random_choice == "s6":
        $ random_choice=s6

    if random_choice == "s7":
        $ random_choice=s7

    if random_choice == "s8":
        $ random_choice=s8

    if random_choice == "s9":
        $ random_choice=s9

    if random_choice == "s10":
        $ random_choice=s10

    if random_choice == "s11":
        $ random_choice=s11

    "Now I can have [random_choice[t3]]"

    return
    
    
Thank you
Last edited by dellcartoons on Sun Aug 18, 2019 8:30 am, edited 1 time in total.

User avatar
Kia
Eileen-Class Veteran
Posts: 1040
Joined: Fri Aug 01, 2014 7:49 am
Deviantart: KiaAzad
Discord: Kia#6810
Contact:

Re: Selecting a class object at random, but not as clumsy

#2 Post by Kia »

the part:

Code: Select all

$ random_choice =  renpy.random.choice(trait_list)
is the right way to do it, however you can be much more efficient by removing all of those "if"s and storing your objects without quotation marks

Code: Select all

define trait_list = [s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11]

dellcartoons
Regular
Posts: 29
Joined: Sat Jul 13, 2019 3:35 pm
Contact:

Re: Selecting a class object at random, but not as clumsy

#3 Post by dellcartoons »

Don't I need quotation marks in a list? Won't Renpy throw an error if I don't?

dellcartoons
Regular
Posts: 29
Joined: Sat Jul 13, 2019 3:35 pm
Contact:

Re: Selecting a class object at random, but not as clumsy

#4 Post by dellcartoons »

I tried it your way, and IT WORKED

Dunno why I thought I needed quotes in the first place, then

Thank you

Post Reply

Who is online

Users browsing this forum: Semrush [Bot]