Get a Random Type
BlitzMax Forums/BlitzMax Beginners Area/Get a Random Type
| ||
I'm having trouble getting a random type member, I'm not sure how to do it, and the code I've got below doesn't work -- it keeps complaining that it can't convert to a string. Ideally I want to pick a random type member and return the pointer to my game so I can use it. ie; i = Get_Random_Name Print i.name$ Rem ############################################################## Global Name data ############################################################## End Rem Global global_name_list:TList=CreateList() 'To Store all the names Type TNames Field name:String Field gender:String Function create(_name:String, _gender:String="male" ) Local p:TNames = New TNames p.name$ = _name$ p.gender$ = _gender$ ListAddLast global_name_list, p End Function End Type Function Get_Random_Name:TNames() Local member%=Rand(1,CountList(global_name_list)) Local counter%=0 For Local i:TNames=EachIn(global_name_list) counter:+1 If counter=member Return i Exit End If Next End Function Print Get_Random_Name:TNames() ' LOAD MALE NAMES ------------------------------------- RestoreData lbl_male_names Repeat Local m_name:String ReadData m_name If m_name = "end" Then Exit Else ReadData m_name TNames.Create(m_name, "male") End If Forever ' LOAD FEMALE NAMES ---------------------------------- RestoreData lbl_female_names Repeat Local f_name:String ReadData f_name If f_name = "end" Then Exit Else ReadData f_name TNames.Create(f_name, "female") End If Forever ' -------------------------------------- #lbl_male_names DefData "John" DefData "Francisco" DefData "Walter" DefData "Mike" DefData "Sanchez" DefData "Franz" DefData "Hector" DefData "Yuri" DefData "Jan" DefData "Jakob" DefData "Phelps" DefData "Carlos" DefData "Williams" DefData "Bolo" DefData "Mark" DefData "Bob" DefData "Fred" DefData "Bruce" DefData "Lee" DefData "Denis" DefData "Pablo" DefData "Escobar" DefData "Gray" DefData "Warren" DefData "Richard" DefData "Screwface" DefData "Hans" DefData "Roger" DefData "Gomez" DefData "Donald" DefData "Alan" DefData "Charlie" DefData "Ortega" DefData "Bush" DefData "Woo" DefData "Hill" DefData "Cortez" DefData "Forelli" DefData "Walters" DefData "Banner" DefData "Smith" DefData "Diaz" DefData "Chris" DefData "Lopez" DefData "Fedrov" DefData "Ben" DefData "White" DefData "Sosa" DefData "Paul" DefData "Mike" DefData "Ryan" DefData "Edu" DefData "Johnny" DefData "Jack" DefData "Liu" DefData "Zack" DefData "Chuck" DefData "Leong" DefData "Ying" DefData "Li" DefData "Adam" DefData "Bryan" DefData "Chin" DefData "Greg" DefData "Tim" DefData "Pete" DefData "Owen" DefData "Nick" DefData "George" DefData "Tyrone" DefData "Santana" DefData "Chan" DefData "Tommy" DefData "Montana" DefData "Rodriguez" DefData "Curtis" DefData "Warren" DefData "Tanaka" DefData "Benny" DefData "Demetri" DefData "Harrison" DefData "Chris" DefData "Hall" DefData "Stuart" DefData "Benedict" DefData "Arnold" DefData "Escobedo" DefData "Cory" DefData "James" DefData "Imran" DefData "Conners" DefData "Fernando" DefData "Kanaga" DefData "Charles" DefData "Fleming" DefData "Sean" DefData "Rodriguez" DefData "Orejuela" DefData "Cesar" DefData "Enrico" DefData "end" #lbl_female_names DefData "Anna" DefData "Bianca" DefData "Camilla" DefData "Carina" DefData "Clarissa" DefData "Daniela" DefData "Donna" DefData "Emilia" DefData "Francesca" DefData "Gabriela" DefData "Gema" DefData "Gianna" DefData "Gina" DefData "Giovanna" DefData "Isabella" DefData "Jovanna" DefData "Lucia" DefData "Maria" DefData "Michele" DefData "Mila" DefData "Paola" DefData "Pia" DefData "Rosa" DefData "Rosetta" DefData "Speranza" DefData "Venecia" DefData "Eliza" DefData "Peggie" DefData "Ann" DefData "Natasha" DefData "Alice" DefData "Louise" DefData "Georgie" DefData "Annett" DefData "Teresa" DefData "Marisha" DefData "Katie" DefData "Yvette" DefData "Kandy" DefData "Blossom" DefData "Jenny" DefData "Tara" DefData "Emily" DefData "Emma" DefData "Madison" DefData "Abigail" DefData "Olivia" DefData "Isabella" DefData "Hannah" DefData "Samantha" DefData "Ava" DefData "Ashley" DefData "Lydia" DefData "Chloe" DefData "Donna" DefData "Dawn" DefData "Julie" DefData "Candy" DefData "Holly" DefData "Grace" DefData "Cassie" DefData "Jennifer" DefData "Ginger" DefData "Cassie" DefData "Tamara" DefData "Gloria" DefData "Lois" DefData "Jane" DefData "Cassandra" DefData "Maud" DefData "Felicia " DefData "Eleanor" DefData "Vanessa " DefData "Natasha " DefData "Jaclyn" DefData "Jade" DefData "Jax" DefData "Josie" DefData "Jodie" DefData "Lark" DefData "Lauren" DefData "Lyn" DefData "Abigail" DefData "Aiko" DefData "Audrey" DefData "Alex" DefData "Ally" DefData "Angelina" DefData "Angelica" DefData "Annie" DefData "Antoinette" DefData "Ashby" DefData "Azure" DefData "Haley" DefData "Harley" DefData "Hiroko" DefData "Hoshiko" DefData "Huong" DefData "Hilary" DefData "Harriet" DefData "end" |
| ||
This line:Print Get_Random_Name:TNames() shoud be: Print Get_Random_Name().Name and to do this: i = Get_Random_Name Print i.name$ You should: Global i:TNames 'This can be changed to Local i:Tnames or whatever needed I = Get_Random_Name() Print I.Name I highly recomend you to use SuperStrict always in your code, this will help you with this kind of issues |
| ||
Sorry, its been months since I actually coded with Blitz Max , but I'm slowly making the transition... Thank you very much for your help! |
| ||
Your code worked fine -- I just had to ensure that the randomisation of a type member happened after it was initialised. Thanks a lot! |
| ||
Shouldn't gender be integer or boolean instead of string? And going through the list looking for a random name is going to be slow with lots of names. An array might be better, depending upon what you want to do. |
| ||
I only use the name once in initialisation. The names aren't randomised again. I tried gender:boolean, but it came up with an error. An integer would be better, yes. Yes, I thought an array would be better, as I'm using a similar method of getting data into the game a lot. |