Page 1 of 1

Random Name Generator

Posted: Mon Apr 28, 2014 11:55 pm
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.

Re: Random Name Generator

Posted: Tue May 13, 2014 12:54 pm
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!

Re: Random Name Generator

Posted: Tue Jan 16, 2018 11:00 am
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.