Search Unity

Would love a bit of help creating a Name Generator

Discussion in 'Getting Started' started by unity_EloTY-qRfCeFbA, Feb 11, 2018.

  1. unity_EloTY-qRfCeFbA

    unity_EloTY-qRfCeFbA

    Joined:
    Feb 11, 2018
    Posts:
    2
    Hello everyone, I'm new to this community and to programming.
    I would like to create my own name generator. Essentially I know there are loads of name generators on the internet but I basically want one where I can alter the names/syllables to my own selection. As there isn't such a program available to me I need to write my own.
    My knowledge of programming and writing scripts is minimal, I've completed some really basic tutorials so I have an understanding of how they work and how to put them together but I not enough to create anything complicated without help.

    The specific programs I need to make:
    - A name generator that picks from either 2 or 3 lists of words and sticks them together. (as if you were going to create a username e.g: SuperMicrowave, SpiderMonkey.
    - A name generator that creates multi- syllabled names. (Ideally one where before name generation a use could selevt how many syllables, but I sense even that might be too complicated right now) I need the script to pick from 4 or 5 lists of single syllables and then generate one word.

    Is this doable for a beginner?
    I did find this tuorial but it skips over a lot of details that probably aren't needed for someone familiar with coding but as a beginner I tried putting this information together and Unity then said it couldn't run the code because it had too many errors.
    Any help/advice on where to start would be greatly appreciated.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    The trouble with that tutorial is that it's JavaScript. You are (or should be) using C#. So, you'll have to adapt the code.

    The key to programming is simply to break big problems into smaller problems. And then break those smaller problems into even-smaller problems, and keep doing that until you've gotten down to a task that's trivial to do. For example:
    • Need to select several random strings and stick them together. This is too big. So break it down to: (a) need to have a list of strings defined somehow; (b) need to pick a random number; (c) need to pick a string from a list based on a number; (d) need to add strings together; (e) need to display the result somehow.
    • Pick any one of those. How about (b), pick a random number. Can you do that? If even that is daunting, then break it down to: (a) need to get a script — any script — to compile and run; (b) need to prove that it's running by outputting something (say with Debug.Log); (c) need to pick a random number, and print that out instead.
    Then you go back up and pick a different smallish task, and break that one into smaller parts. Note that at no point do you ever type a bunch of code into the code editor at once, then press Play and hope for the best. You make tiny, tiny changes to your code, testing after each one. You will always either have functional code, or be at most 1 or 2 lines away from functional code.

    So this means that when you get an error, you know exactly where it is — it's in the one or two lines of code you just changed. You can study those, and study what the error message is telling you, and read the docs related to whatever you're trying to do in those lines, and figure it out. Then you move on.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This example is crude, but it will work. Set the names in the inspector. Google each programming term to figure out what it's doing.

    Code (CSharp):
    1. [SerialiseField]
    2. List<strong> names;
    3.  
    4. void Update (){
    5.     Debug.Log(names[Random.Range(0, names.count)] + " " + names[Random.Range(0, names.count)];
    6. }
     
    Last edited: Feb 12, 2018
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    To expand upon what Kiwasi shared (and correct a small typo), if you want one like they use with an "AdjectiveNoun" form, try this:
    Code (CSharp):
    1. string[] adjectives;
    2. string[] nouns;
    3.  
    4. public string GenerateName() {
    5.     return adjectives[Random.Range(0, adjectives.length)] + nouns[Random.Range(0, nouns.length)];
    6. }
    For the syllable generator, you could do something really similar, with a big list of sounds. Your Generate method would take an integer parameter that would determine how many times you looped through your syllable array and added content to your word.

    Alternatively, if you want something with a bit more variety (and a lot more complexity), you can look into Markov chains.
     
  5. Dai-22

    Dai-22

    Joined:
    Jan 19, 2018
    Posts:
    130
    Look at Brackeys C# video series, it covers Randoms, this would seem to help, potentially...
     
  6. unity_EloTY-qRfCeFbA

    unity_EloTY-qRfCeFbA

    Joined:
    Feb 11, 2018
    Posts:
    2
    Hey thank you for your feedback and support everyone!
    I'm going to give this some thought and will look again at your suggestions. However this programming project is to support a much larger project in which writing and art creation are the primary skills. As my programming skill is minimal I need to really examine how much time I can devote to this support project.

    I think what I need to do is understand the syntax used in C#, and understand which bits I need before I can get creative with it.
    Thank you though, I want to make sure your help doesn't go to waste. :)
     
  7. Dai-22

    Dai-22

    Joined:
    Jan 19, 2018
    Posts:
    130
    I think it's the same as the basic console application system in visual studio for C#.

    use 3 randoms, in a 1,2,3, order, with another random added to each for syllable variation, maybe i'm wrong, noobie here...Random from 1to 51 = 50 names, x 3 for 1st,2nd,3rd names. Then apply a random under conditions of syllable isolation, or variation, depending on if you want to limit it or freely randomise it...You can type in every name for each random also giving full editing control. This is basic ish stuff. Shouldn't take too long.
    Connecting items and "calling" things is what drives me a bit mad :)
     
  8. Gandamir

    Gandamir

    Joined:
    Nov 22, 2017
    Posts:
    36
    Here is one in Python that may give you some ideas: https://www.reddit.com/r/procedural...ra0cz/english_place_name_generator_in_python/

    I've started translating it to C#. Here is where I am at in case it is a better starting point for you. Note that this may call some utility functions that I have. Let me know if you are missing any of them. :)
    Code (CSharp):
    1.         string[] first = new string[] { "Ald", "Alf", "Ash", "Barn", "Blan", "Brack", "Brad", "Brain", "Brom", "Bur", "Cas", "Chelm", "Clere",
    2.             "Cook", "Dart", "Dur", "Edg", "Eg", "El", "Elm", "En", "Farn", "Flit", "Hart", "Horn", "Hors", "Hurst", "Kings", "Leather", "Maiden",
    3.             "Marl", "Mel", "Nort", "Pem", "Pen", "Prest", "Rock", "Shaft", "Shriv", "Sod", "South", "Staf", "Stain", "Stap", "Sud", "Sun", "Walt",
    4.             "Watch", "Wen", "Wet", "Whit", "Win", "Wy", "Wych" };
    5.         string[] doubles = new string[] { "Abb", "Bass", "Booth", "Both", "Burr", "Camb", "Camm", "Cann", "Chedd", "Chill", "Chipp", "Cir",
    6.             "Dribb", "Egg", "Ell", "Emm", "End", "Fald", "Full", "Hamm", "Hamp", "Hann", "Kett", "Mill", "Pend", "Redd", "Ribb", "Roth", "Sir",
    7.             "Skell", "Sodd", "Sudd", "Sund", "Tipp", "Todd", "Warr", "Wolv", "Worr" };
    8.         string[] postdoubles = new string[] { "en", "er", "ing" };
    9.         string[] mid = new string[] { " on ", "bas", "ber", "comp", "den", "en", "mans", "mer", "re", "rens", "ring", "sey", "stan", "-under-" };
    10.         string[] last = new string[] { "avon", "bage", "beck", "borne", "borough", "bourne", "bridge", "brook", "brough", "bury", "by", "castle",
    11.             "cester", "chester", "combe", "den", "ditch", "don", "down", "ey", "field", "ford", "grove", "hall", "ham", "hampton", "head", "lake",
    12.             "ley", "ling", "low", "mere", "moor", "nell", "ney", "over", "port", "shot", "side", "smith", "sted", "stoke", "thorne", "ton", "tree",
    13.             "wang", "well", "wich", "wick", "wold", "wood", "worth" };
    14.  
    15.         /// <summary>
    16.         /// Gets the next name from the generator.
    17.         /// </summary>
    18.         internal override string Next()
    19.         {
    20.             string finished_name = "";
    21.             int pd = 0;
    22.  
    23.             if (Random.NextDouble() > 0.4)
    24.             {
    25.                 finished_name = finished_name + doubles[Random.Next(0, doubles.Length)];
    26.  
    27.                 if (Random.NextDouble() > 0.6)
    28.                 {
    29.                     finished_name = finished_name + postdoubles[Random.Next(0, postdoubles.Length)];
    30.                     pd = 1;
    31.                 }
    32.                 else
    33.                 {
    34.                     finished_name = finished_name.Substring(0, finished_name.Length - 1);
    35.                 }
    36.             }
    37.             else
    38.                 finished_name = finished_name + first[Random.Next(0, first.Length)];
    39.  
    40.             if (Random.NextDouble() > 0.5 && pd == 0)
    41.             {
    42.                 if (finished_name.EndsWith("r") || finished_name.EndsWith("b"))
    43.                 {
    44.                     if (Random.NextDouble() > 0.4)
    45.                         finished_name = finished_name + "ble";
    46.                     else
    47.                         finished_name = finished_name + "gle";
    48.                 }
    49.                 else if (finished_name.EndsWith("n") || finished_name.EndsWith("d"))
    50.                     finished_name = finished_name + "dle";
    51.                 else if (finished_name.EndsWith("s"))
    52.                     finished_name = finished_name + "tle";
    53.  
    54.                 if (Random.NextDouble() > 0.7 && finished_name.EndsWith("le"))
    55.                     finished_name = finished_name + "s";
    56.             }
    57.             else if (Random.NextDouble() > 0.5)
    58.             {
    59.                 if (finished_name.EndsWith("n"))
    60.                 {
    61.                     if (Random.NextDouble() > 0.5)
    62.                         finished_name = finished_name + "s";
    63.                     else
    64.                         finished_name = finished_name + "d";
    65.                 }
    66.                 else if (finished_name.EndsWith("m"))
    67.                     finished_name = finished_name + "s";
    68.  
    69.  
    70.                 if (Random.NextDouble() > 0.7)
    71.                     finished_name = finished_name + mid[Random.Next(0, mid.Length)];
    72.             }
    73.  
    74.  
    75.             finished_name = finished_name + last[Random.Next(0, last.Length)];
    76.  
    77.             string[] fix = finished_name.Split(' ');
    78.  
    79.             if (fix[1].Equals(" ", StringComparison.InvariantCultureIgnoreCase))
    80.                 finished_name = fix[0] + ' ' + fix[2].ToString().ToUpper();
    81.  
    82.  
    83.             fix = finished_name.Split('-');
    84.             if (fix[1].Equals("-", StringComparison.InvariantCultureIgnoreCase))
    85.                 finished_name = fix[0] + '-' + fix[2].ToString().ToUpper();
    86.  
    87.             return finished_name;
    88.         }
    I'm working on a similar problem, but more for city names than person names.
     
  9. Christopher-Taljaard

    Christopher-Taljaard

    Joined:
    May 15, 2019
    Posts:
    3
    Hi if anyone is still working on this I wrote a simpler c# script recently using that java script example posted earlier as the base, you'll have to get a button to call the CreateNewName() function, connect a UI text box to the public text variable and then it should work. It works out quite well most of the time :)

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7.  
    8. public class PirateNameGenerator : MonoBehaviour
    9. {
    10.     public string currentName = "BlackBeard";
    11.     string[] titleNameSyllables = new string[] { "black", "rumbly" ,"silver", "big", "brauny", "scraggley"};
    12.  
    13.     string[] firstNameSyllables = new string[] { "stan", "rup", "jack", "will", "barth", "stede", "franc", "calico", "ed", "ching", "zheng"};
    14.  
    15.  
    16.     string[] firstNameSuffixes = new string[] { "ert", "son", "iam", "olomew", "is", "ward", "less", "lin", "anor", "ridge", "west", "ing", "yi" };
    17.  
    18.  
    19.     string[] lastNameSyllables = new string[] { "beard", "silver", "swash", "barn", "trundle","blunder", "cut", "swinger", "trunk"};
    20.  
    21.  
    22.     string[] lastNameSuffixes = new string[] { "beard", "less","more", "cut", "pearl", "buckler", "inger", "swinger", "trunk", "cannon"};
    23.  
    24.  
    25.     string[] prepositionNameSylables = new string[] { "from", "of","from The Land Just Next To", "from", "of" , "from", "of" };
    26.  
    27.  
    28.     string[] landNameSyllables = new string[] { "wonder", "crab", "ham", "heath", "well", "drakens", "smuggler", "scavenger", "wreck", "west", "north", "east", "south","winter","night","sun","coastal"};
    29.  
    30.  
    31.     string[] landNameSuffixes = new string[] { "berg", "stein", "land","veld","belt","island","isle","and","ford","shire","county","town","ica","'s cove","coast" };
    32.  
    33.     public TextField maleText;
    34.     string titleName = "";
    35.     string firstName = "";
    36.     string lastName = "";
    37.     string landName = "";
    38.     string prepositionName = "";
    39.  
    40.     int numberOfSyllablesInFirstName;
    41.     int numberOfSyllablesInLastName;
    42.     int numberOfSyllablesInTitleName;
    43.     int numberOfSyllablesInLandName;
    44.     int numberOfSyllablesInPrepositionName;
    45.     string firstNameLetter = "";
    46.     string lastNameLetter = "";
    47.     string titleNameLetter = "";
    48.     string landNameLetter = "";
    49.     string prepositionNameLetter = "";
    50.  
    51.     public void CreateNewName()
    52.     {
    53.         //Title
    54.         if (Random.Range(0, 100) < 30)
    55.         {
    56.             titleName = "";
    57.             numberOfSyllablesInTitleName = Random.Range(1, 2);
    58.             for (int i = 0; i < numberOfSyllablesInTitleName; i++)
    59.             {
    60.                 titleName += titleNameSyllables[Random.Range(0, titleNameSyllables.Length)];
    61.             }
    62.             titleNameLetter = titleName.Substring(0, 1);
    63.             titleName = titleName.Remove(0, 1);
    64.             titleNameLetter = titleNameLetter.ToUpper();
    65.             titleName = titleNameLetter + titleName;
    66.             titleName = titleName + " ";
    67.         }
    68.         else
    69.         {
    70.             titleName = "";
    71.         }
    72.          
    73.  
    74.  
    75.         //First Name
    76.         firstName = "";
    77.         numberOfSyllablesInFirstName = Random.Range(1, 2);
    78.         for (int i = 0; i < numberOfSyllablesInFirstName; i++)
    79.         {
    80.             firstName += firstNameSyllables[Random.Range(0, firstNameSyllables.Length)];
    81.         }
    82.         firstNameLetter = firstName.Substring(0, 1);
    83.         firstName = firstName.Remove(0, 1);
    84.         firstNameLetter = firstNameLetter.ToUpper();
    85.         firstName = firstNameLetter + firstName;
    86.         if (Random.Range(0, 100) < 50)
    87.         {
    88.             firstName += firstNameSuffixes[Random.Range(0, firstNameSuffixes.Length)];
    89.          
    90.            
    91.         }
    92.        
    93.        
    94.  
    95.         //Last Name
    96.         lastName = "";
    97.         numberOfSyllablesInLastName = Random.Range(1, 2);
    98.         for (int i = 0; i < numberOfSyllablesInLastName; i++)
    99.         {
    100.             lastName += lastNameSyllables[Random.Range(0, lastNameSyllables.Length)];
    101.         }
    102.         lastNameLetter = lastName.Substring(0, 1);
    103.         lastName = lastName.Remove(0, 1);
    104.         lastNameLetter = lastNameLetter.ToUpper();
    105.         lastName = lastNameLetter + lastName;
    106.        
    107.         if (Random.Range(0, 100) < 50)
    108.         {
    109.             lastName += lastNameSuffixes[Random.Range(0, lastNameSuffixes.Length)];
    110.         }
    111.  
    112.         //Preposition
    113.         prepositionName = "";
    114.         numberOfSyllablesInPrepositionName = Random.Range(1, 2);
    115.         for (int i = 0; i < numberOfSyllablesInPrepositionName; i++)
    116.         {
    117.             prepositionName += prepositionNameSylables[Random.Range(0, prepositionNameSylables.Length)];
    118.         }
    119.         prepositionNameLetter = prepositionName.Substring(0, 1);
    120.         prepositionName = prepositionName.Remove(0, 1);
    121.         prepositionNameLetter = prepositionNameLetter.ToUpper();
    122.         prepositionName = prepositionNameLetter + prepositionName;
    123.        
    124.  
    125.         //Land Name
    126.         landName = "";
    127.         numberOfSyllablesInLandName = Random.Range(1, 2);
    128.         for (int i = 0; i < numberOfSyllablesInLandName; i++)
    129.         {
    130.             landName += landNameSyllables[Random.Range(0, landNameSyllables.Length)];
    131.         }
    132.         landNameLetter = landName.Substring(0, 1);
    133.         landName = landName.Remove(0, 1);
    134.         landNameLetter = landNameLetter.ToUpper();
    135.         landName = landNameLetter + landName;
    136.        
    137.  
    138.  //suffix
    139.    
    140.             landName += landNameSuffixes[Random.Range(0, landNameSuffixes.Length)];
    141.        
    142.  
    143.         //Current Name
    144.  
    145.         if (Random.Range(0, 100) < 70)
    146.         {
    147.             currentName = titleName + firstName + " " + lastName;
    148.            
    149.             maleText.text = currentName.ToString();
    150.         }
    151.         else
    152.         {
    153.             currentName = titleName + firstName + " " + lastName + " " + prepositionName + " " + landName;
    154.            
    155.             maleText.text = currentName.ToString();
    156.            
    157.         }
    158.     }
    159.  
    160. }
    161.  
     
    khoingo-security and Ryiah like this.
  10. bloodwolftico

    bloodwolftico

    Joined:
    Nov 13, 2017
    Posts:
    100
    I just wanted to say this is GREAT advice! Applies not only to OP, but to everyone learning how to code and develop games.
     
    JoeStrout likes this.
  11. berkninan

    berkninan

    Joined:
    Jan 7, 2020
    Posts:
    2
    try this one...

    Code (CSharp):
    1.  
    2. string rString = Path.GetRandomFileName();
    3. rString = rString.Replace(".", ""); // Remove period.
    4. MessageBox.Show(rString);
    5.  
    More examples on...random string
     
  12. khoingo-security

    khoingo-security

    Joined:
    Feb 7, 2020
    Posts:
    1
    Thanks