Automatically generate variable reference

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
Tachyglossus
Regular
Posts: 164
Joined: Wed Dec 01, 2010 7:43 pm
Projects: Sprite Art for: "Final Banquet", "365 Days"
Location: British Columbia
Contact:

Automatically generate variable reference

#1 Post by Tachyglossus »

To me, the subject looks like nonsense. I don't know the jargon, or rather, have forgotten it since not working with any sort of code for well over a year.

I wasn't sure how to search for what I wanted to know, or even how to ask this.

Is there a way to set it up so new variables or "objects" are made with some sort of serial number attached?

Let us pretend I have a game that involves collecting Easter Eggs.

If I already have:

$ eggcol = ["pink", "yellow", "blue", "purple", "green"]
$ eggsfound = []

$ Egg1 = "pink"
$ Egg2 = "blue"
$ Egg3 = "yellow"

But then what if I wanted the Easter bunny to randomly lay more eggs, or have an event that triggers laying more unique eggs? So that I don't have to type in a long list of egg1, egg2, egg3, it makes egg4, egg5, and egg6 for me when it is needed? And that it will also assign one of the listed colours to this new, unique egg? And perhaps other attributes, like "sparkly" or "hatchable" or something...

So something like... um...

Code: Select all

label search:
     "You search for eggs."
     call find
     if find = True:
        "AHA!"
        call newegg
        "You found an egg!"

     else:
         jump nofind

label find:
    $find = renpy.random.randint(1,100)
    if find >= 50:
        return True
    else:
        return False

label newegg:
     (Code for generating egg# then appending it to list of collected eggs)
label nofind:
    "You fail to find an egg. Try again?"
    menu tryagain:
        "Yes.":
             jump search
        "No.":
             jump stop

label stop
    etc etc
At some point, the list of eggs can be viewed, and selected for more information, such as it's colour, pattern, or some other attribute.

Is this possible?

Would the following work?

$ egg%(serial)d = renpy.random("eggcol")

where $ serial is defined by a counter that constantly adds one each time there is a successful find?

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Automatically generate variable reference

#2 Post by PyTom »

I think you probably want a list here, rather than a series of variables. So you could then do:

Code: Select all

$ eggs = [ ]

# ...

$ eggs.append(renpy.random.choice(eggcol))

# ...

if len(eggs) >= 2 and egg[1] == "red":
    "The second egg is red!"
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Tachyglossus
Regular
Posts: 164
Joined: Wed Dec 01, 2010 7:43 pm
Projects: Sprite Art for: "Final Banquet", "365 Days"
Location: British Columbia
Contact:

Re: Automatically generate variable reference

#3 Post by Tachyglossus »

Ah I was imagining that might be what someone would respond with... sometime after i posted. I used a really bad example rather than saying exactly what I want to do, but I tend not liking to share exactly what I'm up to because I like being all mysterious.

But mysterious and vague people don't get help, so I'll suck it up.

What I'm doing is more complex than eggs with colours. It's rats. Rats with colours. Rats that can breed and make new rats.

What I have set up is a Rat class, with attributes I defined myself, such as colour, gender, value (because there will be buying and selling of rats). So I have this:

Code: Select all

init python:

    class Rat:
        def __init__(self, name, age, gender, health, furcol, gene1, gene2, worth):
            self.name = name
            self.age = age
            self.gender = gender
            self.health = health
            self.furcol = furcol
            self.gene1 =gene1
            self.gene2 = gene2
            self.worth = worth

    class Own:
        def __init__(self, funds=100):
            self.funds = funds
            self.Rat = []
And then later on...

Code: Select all

python:
        own = Own()
        Rat1 = Rat("Pepper", 3, "Female",10, "black", "B", "b", 100)
        Rat2 = Rat("Angel", 2, "Female", 8, "white", "b", "b", 150)
        Rat3 = Rat("Sable", 5, "Male", 9, "black", "B", "B", 100)
        Rat4 = Rat("Salt", 4, "Male", 10, "white", "b", "b",150)
When I breed them, is there a way I can have it generate a Rat5 that pulls its attributes from defined parameters? So that it does a coin toss for gender, and then assigns either B or b depending on what the parents have? I'm doing a thing on very basic Mendellian heredity and displaying it through a game rather than a lengthy paper with a series of punnet squares.

User avatar
PyTom
Ren'Py Creator
Posts: 16093
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Automatically generate variable reference

#4 Post by PyTom »

I think the question is - how would you use Rat5 if it was defined? It's certainly possible to write the code:

Code: Select all

$ Rat5 = breed(Rat1, Rat2)
but that wouldn't extend to Ratn. If you want a non-fixed number of rats, you need a list.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Tachyglossus
Regular
Posts: 164
Joined: Wed Dec 01, 2010 7:43 pm
Projects: Sprite Art for: "Final Banquet", "365 Days"
Location: British Columbia
Contact:

Re: Automatically generate variable reference

#5 Post by Tachyglossus »

Rats (pun intended). Oh well.

I might try attacking this from a completely different angle. Thanks for the prompt replies!

Post Reply

Who is online

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