Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tastman · Apr 20, 2016 at 11:28 AM · performancedata storagecachingcollections

Best way to store large amounts of data for random generation

I'm creating methods to randomly generate a lot of data in my game. For instance, my "Person" generator takes a large amount of data and randomizes things such as name, address, phone number, social security number, place of work (company name), etc.

My current solution is loading each type of data from a separate .txt file in the resources folder into lists at start, then generating a random index from those lists:

 public class Person
       {
         public string firstName;
       }
 
 public class GenericPerson
       {
         public List<string> firstNames;
       }
 
 // Class instance containing data collections
 public GenericPerson genericPerson;
 
 void PopulateFirstNames()
     {
         if (!PlayerController.loadedGame)
         {
             // Load first names from text file
             TextAsset names = Resources.Load("Names/FirstNames", typeof(TextAsset)) as TextAsset;
 
             // Split text file into rows and populate list
             genericPerson.firstNames = names.text.Split("\n"[0]).ToList();
 
             // Parse the empty char from each entry
             for (int i = 0; i < genericServer.names.Count; i++)
             {
                 genericPerson.firstNames[i] = genericPerson.firstNames[i].Replace("\r", string.Empty);
             }
         }
     }
 
 string GenerateFirstName()
     {
         int index = Random.Range(0, genericPerson.firstNames.Count);
         string str = genericPerson.firstNames[index];
         return str;
     }
 
 public Person GeneratePerson()
       {
         Person person = new Person();
         person.firstName = GenerateFirstName();
         return person;
       }

My concern is performance - I want to load as much as possible at the start of a new game to be able to simply get a set of values when generating a new person, but might end up storing tens of thousands of strings in 10-15 different lists.

Should I be using list collections for this? I'm not very well acquainted with dictionaries or hashtables, but how much better are they at handling large amounts of data?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image axldoyle · Apr 20, 2016 at 01:02 PM 1
Share

$$anonymous$$y friend has just been making a procedurally generated game within Unity and he uses a combination of lists of individual variable (e.g. ints etc) and lists of classes which obviously have more data in. He can run a 16x16 grid size generation in 0.1s with no hit on performance. He has enemies, walls, objects etc that all get generated and spawned after.

I would suggest running the generation at the start and seeing how many combinations you can generate before you feel a performance hit, I'd assume you could get quite alot generated though if youre smart with youre code :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Free_Radical · Apr 20, 2016 at 05:10 PM

It depends on what you're doing. If all you're doing is generating a random value and using that to access an element in your list, that's about as efficient as you're going to get outside of just a standard array. If however, you're going to ever manipulate these values or attempt to return a specific object, it would be far more efficient to utilize a dictionary.

May I suggest some light reading:

https://msdn.microsoft.com/en-us/library/6tc79sx1(v=vs.110).aspx

http://geekswithblogs.net/BlackRabbitCoder/archive/2011/06/16/c.net-fundamentals-choosing-the-right-collection-class.aspx

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tastman · Apr 20, 2016 at 09:11 PM 0
Share

In the case of this Person class example, all I need is a whole lot of different combinations of personal information to create a read-only rap sheet for a random person. The only reason I want to generate it randomly is so that I can create, say a thousand people in one playthrough that are somewhat unique and not repeated in another playthrough.

So in that case, would you say the example I posted is a viable solution?

Thanks for the links, I'll dig into those.

avatar image Free_Radical Tastman · Apr 29, 2016 at 10:41 PM 0
Share

Absolutely. If you don't plan on ordering the list, searching for specific names, or anything along those lines, you really can't beat direct access by index.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

60 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it okay to assign and 'un-assign' game object tags at runtime, will it come back to bite me? 0 Answers

Too many Lists? 1 Answer

Caching stacked UI Image GameObject 0 Answers

Sprite shape culling performance 1 Answer

Meshcollider count VS Meshcollider vertex count? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges