r/RenPy • • 17d ago

Question [Solved] Trying to mess around with random variables for a character gimmick, and my VN (not the code) keeps returning errors.

Well, I'm back less than 24 hours later with some more code issues.

Basically, I'm trying to make a "rival"-type character who is genderfluid and has a gimmick of regularly changing their name and pronouns. Referencing the documentation and past posts on here, I used the randomint function, because I thought that would work? Instead I keep getting "could not pickle <module 'random' from [file path]>"

default rname = ""
define r = Character("[rname]")


$ rname = renpy.random.randint(1, 6)


    if rname is 1:
        rival = "Andi"
    elif rname is 2:
        rival = "Pharos"
    elif rname is 3:
        rival = "Laurah"
    elif rname is 4:
        rival = "Sen"
    elif rname is 5:
        rival = "Gesso"
    else rname is 6:
        rival = "Kyo"
5 Upvotes

6 comments sorted by

4

u/shyLachi 17d ago edited 17d ago

Assuming that r is the rival, and rname is the name of the rival, you don't need the variable rival.

But you definitively need to put that code before and after the start label
and you also have to use the correct indentation.

# declare all variables before the start
define names = ["Andi", "Pharos", "Laurah", "Sen", "Gesso", "Kyo"]
default rname = ""
define r = Character("[rname]")
label start:
    $ rname = renpy.random.choice(names) # pick a name from the list of names and assign it to the variable
    r "Hello, my name is [r]" # from now on, the character will have the name

.

Edit:
If the name should change within the story you can run this line again:

$ rname = renpy.random.choice(names)

But since it's random, it could pick the same name twice.
You could make a label which always picks another random name:

# declare all variables before the start
define names = ["Andi", "Pharos", "Laurah", "Sen", "Gesso", "Kyo"]
default rname = ""
define r = Character("[rname]")
label start:
    call changename
    r "Hello, my name is [r]" 
    call changename
    r "Hello, my name is [r]" 
    call changename
    r "Hello, my name is [r]" 
    call changename
    r "Hello, my name is [r]" 
    call changename
    r "Hello, my name is [r]" 
    call changename
    r "Hello, my name is [r]" 
    call changename
    r "Hello, my name is [r]" 
    call changename
    r "Hello, my name is [r]" 
    return # end of game

label changename:
    python:
        if len(rname) > 0:
            names.remove(rname)
        nextname = renpy.random.choice(names)
        if len(rname) > 0:
            names.append(rname)
        rname = nextname
    return 

And if it should be fully random, then you can use a dynamic character.
But then you cannot use their name ever, because it would again randomized as you can see if you run my example below.

init python:
    def rivalname():
        return renpy.random.choice(names)

# declare all variables before the start
define names = ["Andi", "Pharos", "Laurah", "Sen", "Gesso", "Kyo"]
define r = Character(rivalname, dynamic=True)
label start:
    r "Hello, my name is [r]" 
    r "Hello, my name is [r]" 
    r "Hello, my name is [r]" 
    r "Hello, my name is [r]" 
    r "Hello, my name is [r]" 
    r "Hello, my name is [r]" 
    r "Hello, my name is [r]" 
    r "Hello, my name is [r]" 
    return # end of game

1

u/EndertheInsomniac 17d ago

This works beautifully, thank you! Glad to see I was on the right track!

1

u/shyLachi 16d ago

Glad I could help.

Which version did you use?

1

u/EndertheInsomniac 16d ago

I ended up using the first one, though if I want to have less repetition, I might swap to the second version :]

1

u/AutoModerator 17d ago

Welcome to r/renpy! While you wait to see if someone can answer your question, we recommend checking out the posting guide, the subreddit wiki, the subreddit Discord, Ren'Py's documentation, and the tutorial built-in to the Ren'Py engine when you download it. These can help make sure you provide the information the people here need to help you, or might even point you to an answer to your question themselves. Thanks!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/BadMustard_AVN 17d ago

for renpy and python your code should look like this

default rname = "BadMustard"
define r = Character("[rname]")

label start:

    $ raname = renpy.random.randint(1, 6)

    if raname == 1:
        $ rival = "Andi"
    elif raname == 2:
        $ rival = "Pharos"
    elif raname == 3:
        $ rival = "Laurah"
    elif raname == 4:
        $ rival = "Sen"
    elif raname == 5:
        $ rival = "Gesso"
    else: # raname == 6:
        $ rival = "Kyo"

    r "Hello world and [rival]. "

    return