Help with replacing an attribute with a variable?

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
GeeSeki
Regular
Posts: 112
Joined: Sat Dec 17, 2016 3:39 am
Projects: A Town Uncovered
itch: geeseki
Contact:

Help with replacing an attribute with a variable?

#1 Post by GeeSeki »

I'm not sure if the question makes sense but basically this is the code I'm working on.

I'm trying to get it so that after picking a random burger (in this case the "beefburger") from a list, it then has it list out the fillings of the beefburger ("beefpatty"). I want it so that whatever burger it randomly picks out, it'll be able to fill in the gaps of Burger.[random burger choice].fillings

I hope that makes sense.

Code: Select all

init -20 python:
    class Burger(object):
        def __init__(self, id, name, fillings = [], *args, **kwargs):
            self.id = id
            self.name = name
            self.fillings = fillings

init -19 python:
    class Burger:
        beefburger = Burger(
            "beefburger",
            "Beef Burger",
            fillings = [
            "beefpatty"]
        )
        
default possible_burgers = ["beefburger"]

label lbl_pick_burger:
    ## Pick randomly from list of possible burgers
    $ pick_burger = renpy.random.choice(possible_burgers)
    "Pick Burger: [pick_burger]."
    # $ pick_burger_2 = Burger.pick_burger.fillings ## Doesn't work
    # $ pick_burger_fillings = Burger.[pick_burger].fillings ## Doesn't work

    "Burger fillings: [pick_burger_2]" ## Attempt 1 Doesn't work
    "Burger fillings: [pick_burger_fillings]." ## Attempt 2 Doesn't work
    "Burger fillings: [Burger.[pick_burger].fillings]." ## Attempt 3 Doesn't work

    return

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

Re: Help with replacing an attribute with a variable?

#2 Post by Ocelot »

Code: Select all

$ fillings_str = ', '.join(pick_burger.fillings)
"Burger fillings: [fillings_str]"
< < insert Rick Cook quote here > >

User avatar
GeeSeki
Regular
Posts: 112
Joined: Sat Dec 17, 2016 3:39 am
Projects: A Town Uncovered
itch: geeseki
Contact:

Re: Help with replacing an attribute with a variable?

#3 Post by GeeSeki »

Ocelot wrote: Wed Apr 14, 2021 4:38 am

Code: Select all

$ fillings_str = ', '.join(pick_burger.fillings)
"Burger fillings: [fillings_str]"
Not sure how I'm supposed to implement that, best I can get is it literally printing out "Burger fillings: Burger.beefburger.fillings"

Code: Select all

$ fillings_str = "Burger".join(pick_burger.fillings)
    "Burger fillings: [fillings_str]"
Doing this gives me an AttributeError: 'RevertableList' object has no attribute 'fillings'

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

Re: Help with replacing an attribute with a variable?

#4 Post by Ocelot »

Oh. I didn't notice that possible_burgers do not contain actual objects. You will need either to place actual objects here or go an extra step of extracting object information from class: $ fillings_str = ', '.join(getattr(Burger, pick_burger).fillings)
< < insert Rick Cook quote here > >

User avatar
GeeSeki
Regular
Posts: 112
Joined: Sat Dec 17, 2016 3:39 am
Projects: A Town Uncovered
itch: geeseki
Contact:

Re: Help with replacing an attribute with a variable?

#5 Post by GeeSeki »

Ocelot wrote: Wed Apr 14, 2021 5:17 am Oh. I didn't notice that possible_burgers do not contain actual objects. You will need either to place actual objects here or go an extra step of extracting object information from class: $ fillings_str = ', '.join(getattr(Burger, pick_burger).fillings)
This worked perfeclty, thank you!

User avatar
GeeSeki
Regular
Posts: 112
Joined: Sat Dec 17, 2016 3:39 am
Projects: A Town Uncovered
itch: geeseki
Contact:

Re: Help with replacing an attribute with a variable?

#6 Post by GeeSeki »

Ocelot wrote: Wed Apr 14, 2021 5:17 am Oh. I didn't notice that possible_burgers do not contain actual objects. You will need either to place actual objects here or go an extra step of extracting object information from class: $ fillings_str = ', '.join(getattr(Burger, pick_burger).fillings)
Okay, so I've come across a new problem with this: When I ask for a fillings list for a burger with multiple fillings, it combines them and counts it as one item eg.
[u'chicken, tomato, lettuce'] when I actually want each element to be it's own item in the list.

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

Re: Help with replacing an attribute with a variable?

#7 Post by Ocelot »

filling_str is a string intended for displaying as a string, comma-separated and all. You can just skip conversion to string, if you need a list for something else: $ fillings = getattr(Burger, pick_burger).fillings
< < insert Rick Cook quote here > >

User avatar
GeeSeki
Regular
Posts: 112
Joined: Sat Dec 17, 2016 3:39 am
Projects: A Town Uncovered
itch: geeseki
Contact:

Re: Help with replacing an attribute with a variable?

#8 Post by GeeSeki »

Ocelot wrote: Wed Apr 14, 2021 6:31 am filling_str is a string intended for displaying as a string, comma-separated and all. You can just skip conversion to string, if you need a list for something else: $ fillings = getattr(Burger, pick_burger).fillings
Got it, thanks! That was the last piece to my puzzle :D

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot]