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
1
Question by Fexception · Nov 09, 2015 at 01:08 PM · databasedatasqlitestorage

Alternative to storing data in DB's

Hi,

I don't know much about any type of DB modules on any game development platform, I'm looking for a way to simply store a string somewhere, and then have 7 more strings related to it. So for example, if I have 10 words, and I want to store a bunch of synonyms for each word, and have them be displayed to the user depending on what they click on, what would I use to accomplish this?

The user would also, themselves be able to add and remove their own words from the list, create new lists of words.. etc..

Thoughts?

Thanks!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Bunny83 · Nov 09, 2015 at 02:22 PM

So you want to have a synonym lookup table? The easiest way is to use a Dictionary of a List of strings.

 public class SynonymDB
 {
     private Dictionary<string, List<string>> db = new Dictionary<string,List<string>>();
     private List<List<string>> lists = new List<List<string>>();
     public void AddWord(string aWord, string aRelated)
     {
         var list = FindSynonyms(aRelated);
         if (!list.Contains(aWord))
             list.Add(aWord);
         for(int i = 0; i < list.Count; i++)
         {
             if (!db.ContainsKey(aWord))
                 db.Add(aWord, list);
         }
     }
     public List<string> FindSynonyms(string aWord)
     {
         List<string> list;
         if (!db.TryGetValue(aWord, out list))
         {
             list = new List<string>();
             lists.Add(list);
             list.Add(aWord);
             db.Add(aWord, list);
         }
         return list;
     }
     public void RemoveWord(string aWord)
     {
         List<string> list;
         if (db.TryGetValue(aWord, out list))
         {
             list.Remove(aWord);
             db.Remove(aWord);
             if (list.Count == 0)
                 lists.Remove(list);
         }
     }
     public void ReadFromStream(System.IO.Stream aStream)
     {
         db.Clear();
         lists.Clear();
         using(var reader = new System.IO.StreamReader(aStream))
         {
             string text = reader.ReadLine();
             var data = text.Split(',');
             var list = new List<string>();
             list.AddRange(data);
             for(int i = 0; i < data.Length; i++)
             {
                 db.Add(data[i], list);
             }
         }
     }
     public void WriteToStream(System.IO.Stream aStream)
     {
         using (var writer = new System.IO.StreamWriter(aStream))
         {
             StringBuilder sb = new StringBuilder();
             foreach(var i in lists)
             {
                 if (i.Count == 0)
                     continue;
                 sb.Length = 0;
                 sb.Append(i[0]);
                 for (int n = 1; n < i.Count; n++)
                     sb.Append(",").Append(i[n]);
                 writer.WriteLine(sb.ToString());
             }
         }
     }
 }

This is a quick and dirty solution. You shouldn't add or remove elements to the lists inside the dictionary manually. Always use the methods this class provides. You can save and load the "database" with WriteToStream / ReadFromStream.

Comment
Add comment · Show 1 · 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 Bunny83 · Nov 09, 2015 at 02:28 PM 0
Share

The additional "lists" variable usually isn't needed, but it simplifies the saving of the whole structure. The save file would look like this:

 word1,synonym,synonym,synonym
 word2,synonym
 word3,synonym,synonym,synonym,synonym
 word4,synonym,synonym,synonym
 word5

So in each line you have one word group which belong together. The words are seperated by commas. It's also possible to have words without synonyms. See "word5".

FindSynonyms will always return a List. Even when the word doesn't exist yet in the database, it will add it. If you want / need a way to check if a word exists, you might want to add another method for that.

 public bool Contains(string aWord)
 {
     return db.Contains$$anonymous$$ey(aWord);
 }

avatar image
0

Answer by Fexception · Nov 11, 2015 at 10:53 AM

Thank you for that. Although as a beginner its tough to follow, I'll have to break it down piece by piece.

How would one print out the actual key name on the screen? I know how to print the value of the key in the console but not the key itself.

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Database (SQLite) Setup for Unity 0 Answers

Sq lite Date base in unity 3d? 1 Answer

Data Storage 5 Answers

Storing Constant data in a mobile game 0 Answers

Database entegration or data storage 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