Random Name Generator

A place for Ren'Py tutorials and reusable Ren'Py code.
Forum rules
Do not post questions here!

This forum is for example code you want to show other people. Ren'Py questions should be asked in the Ren'Py Questions and Announcements forum.
Post Reply
Message
Author
User avatar
FamuFamu
Regular
Posts: 111
Joined: Thu Oct 11, 2012 4:52 pm
Contact:

Random Name Generator

#1 Post by FamuFamu »

I was messing around and I thought some could use this example. This code is to create a name out of a list of possible outcomes. It also serves as an example on how to use a list or classes/functions.

Code: Select all

init:

    #First we set up some variables:

    $ gen_name = "" #The name that will appear

    #Then we create the list with possible names.

    $ name_list = ["Paul", "John", "George", "Ringo"]
    $ l_name_list = ["McCartney", "Lennon", "Harrison", "Starr"]

After this, we move on to create the generator class somewhere else in the code.

Code: Select all

init python:

    class GENERATOR:

        def malename(self):
            
            generatedname = renpy.random.choice(name_list) 
            #Take one name from the name_list list and store it at generatedname
            generatedlastname = renpy.random.choice(l_name_list) 
            #Ditto

            store.gen_name = generatedname + " " + generatedlastname 
            #Show the result of putting both together and store it on the gen_name variable. 
            #If not for the " " part we could get RingoStarr
Now, there are many uses to this, but if you want to use it for NPCs and whatnot you can do this:

Code: Select all


define e = DynamicCharacter("gen_name") 
#This means the character name will be the one stored at gen_name

label start:

    python:

        gen = GENERATOR()
        #This lets us use the GENERATOR class as just "gen"

    $ gen.malename() 
    #At this point the game will run what you made it do on the malename 
    #function of the GENERATOR (now just gen) class. With that, it will 
    #create a name out both lists nad store it at gen_name

    e "Now my name is [gen_name]"

You can create as many generators as needed inside the class (depending on race, gender, species, etc), you could have gen.femalename() or even gen.dogname(), just make sure you create an specific list each time (So femalename() doesn't take anything from the malename() list). It can also have as many parts as needed (Just name or three names).

Anyways, that's that, hope this helps you. If you have any trouble with this just let me know.

User avatar
soggybottoms
Regular
Posts: 35
Joined: Fri May 09, 2014 12:52 am
Contact:

Re: Random Name Generator

#2 Post by soggybottoms »

Thank you for making this tutorial! I don't need to generate random names, but I have been wanting to generate random lines of text under certain conditions to add more replayability and this should work perfectly!

verysunshine
Veteran
Posts: 339
Joined: Wed Sep 24, 2014 5:03 pm
Organization: Wild Rose Interactive
Contact:

Re: Random Name Generator

#3 Post by verysunshine »

Just a heads-up: if you can't get the code to work with only one variable or dynamic character name, try adding a second. My code wouldn't set when only using a first name. I used the "malename" directly, without the store function, but it would show up blank. Adding a last name variable and list and using store worked.

Build the basics first, then add all the fun bits.

Please check out my games on my itch.io page!

Post Reply

Who is online

Users browsing this forum: No registered users