- Home /
How To Take a Randomly Generated Name And Add it as well as some values to a nested Dictionary?
I've been able to create the random first name, and then a random last name, but I want to then combine them into a full name, and then take that full name and some values and assign them to a nested dictionary for storage.
I'm trying to create a way for my game to create random NPC's that will serve as Noble's of a given rulers court. These nobles could be selected to be part of a council, which will then have attributes that add to the main players attributes.
I've figured out how to take my possible first names, and select one randomly, and do the same with last names. But I can't seem to figure out how to then store them in the Dictionary with the values that I want.
I've been using an array with the possible first names, and another array with the possible last names, then using a random.range to pick one of them. That works fine. But I'm really stuck from there, and I can't seem to find a tutorial or answer to a different question that I can use to figure this out.
Any ideas?
Answer by Lo0NuhtiK · May 24, 2015 at 09:29 AM
This should give you some ideas on what you're wanting to do. Script is fairly easy to see whats going on in it when you read through it, just ask if you have any questions. You can see the changes in the enhancedAttys in the inspector panel of whatever object you put this script on when you click one of the GUI buttons to change the user-kingdom during play-mode.
using UnityEngine;
using System.Collections;
using System.Collections.Generic ;
public class Tester_CS : MonoBehaviour
{
public enum Kingdoms
{
Tamriel, Los_Santos, Mushroom,
}
[System.Serializable]
public class Attys
{
public int goldStorage ;
public int knights ;
public int archers ;
public void Add(Attys attys)
{
this.goldStorage += attys.goldStorage ;
this.knights += attys.knights ;
this.archers += attys.archers ;
}
public Attys(){}
public Attys(int goldStorage, int knights, int archers)
{
this.goldStorage = goldStorage ;
this.knights = knights ;
this.archers = archers ;
}
}
[System.Serializable]
public class Noble
{
public Attys attys = new Attys() ;
public string name_ ;
public Noble(string name_, int goldStorage, int knights, int archers)
{
this.name_ = name_ ;
this.attys.goldStorage = goldStorage ;
this.attys.knights = knights ;
this.attys.archers = archers ;
}
public Noble(string name_, int min, int max, bool randomAll = false)
{
this.name_ = name_ ;
if(!randomAll)
{
int n = Random.Range(min, max) ;
this.attys.goldStorage = n ;
this.attys.knights = n ;
this.attys.archers = n ;
return ;
}
this.attys.goldStorage = Random.Range(min, max) ;
this.attys.knights = Random.Range(min, max) ;
this.attys.archers = Random.Range(min, max) ;
}
}
public Dictionary<Kingdoms, List<Noble>> nobles = new Dictionary<Kingdoms, List<Noble>>() ;
public string[] possibleFirstNames = new string[] {"Mario", "Kratos", "Razael", "Q-Bert"} ;
public string[] possibleLastNames = new string[] {"Obama", "Bush", "Clinton"} ;
[System.Serializable]
public class UserAttributes
{
public Attys enhancedAttys = new Attys() ;
public Attys attys = new Attys(10, 3, 12) ;
public Kingdoms kingdom ;
public void ChangeKingdom(Kingdoms kingdom, Dictionary<Kingdoms, List<Noble>> nobles)
{
this.kingdom = kingdom ;
enhancedAttys = new Attys() ;
foreach(Kingdoms k in nobles.Keys)
{
if(k != this.kingdom) continue ;
if(nobles[k] == null) break ;
for(int i = 0 ; i < nobles[k].Count ; i++)
{
enhancedAttys.Add(nobles[k][i].attys) ;
}
}
enhancedAttys.Add(attys) ;
}
}
public UserAttributes userAttys = new UserAttributes() ;
void Start()
{
int pfn = possibleFirstNames.Length ;
int pln = possibleLastNames.Length ;
int k = System.Enum.GetNames(typeof(Kingdoms)).Length ;
int createNobles = Random.Range(5, 10) ;
for(int i = 0 ; i < createNobles ; i++)
{
string name_ = possibleFirstNames[Random.Range(0, pfn)] + " " + possibleLastNames[Random.Range(0, pln)] ;
Kingdoms kd = (Kingdoms) Random.Range(0, k) ;
if(!nobles.ContainsKey(kd))
nobles[kd] = new List<Noble>() ;
nobles[kd].Add(new Noble(name_, 3, 12, true)) ;
}
userAttys.ChangeKingdom(Kingdoms.Tamriel, nobles) ;
}
Vector2 sp ;
void OnGUI()
{
string[] names = System.Enum.GetNames(typeof(Kingdoms)) ;
for(int i = 0 ; i < names.Length ; i++)
{
if(GUILayout.Button("Select Kingdom : " + names[i]))
{
userAttys.ChangeKingdom((Kingdoms) i, nobles) ;
}
}
Rect r = new Rect() ;
r.y = 75f ;
r.height = Screen.height - r.y ;
r.width = Screen.width / 3f ;
GUILayout.BeginArea(r, GUI.skin.box) ;
sp = GUILayout.BeginScrollView(sp) ;
foreach(Kingdoms k in nobles.Keys)
{
GUILayout.BeginVertical("box") ;
GUILayout.Label(k.ToString()) ;
for(int x = 0 ; x < nobles[k].Count ; x++)
{
GUILayout.BeginHorizontal("box") ;
GUILayout.Box(nobles[k][x].name_) ;
GUILayout.BeginVertical() ;
GUILayout.Label("Gold Storage : " + nobles[k][x].attys.goldStorage) ;
GUILayout.Label("Knights : " + nobles[k][x].attys.knights) ;
GUILayout.Label("Archers : " + nobles[k][x].attys.archers) ;
GUILayout.EndVertical() ;
GUILayout.EndHorizontal() ;
}
GUILayout.EndVertical() ;
}
GUILayout.EndScrollView() ;
GUILayout.EndArea() ;
}
}
Your answer
Follow this Question
Related Questions
Store multiple random integers in an array? 4 Answers
Select from an array based on values 1 Answer
move enemy to random transform array 2 Answers
Making an Array of Animations Play at Random With No Repeats 3 Answers
Can you make an array of functions? 2 Answers