Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by sprhughes · Jun 16, 2012 at 12:17 AM · javascripttextdictionaryfile-io

Searching through a text file with Unity?

I'm teaching myself Unity over the summer as a side project, and decided a good way to start out was porting a word puzzle game I made previously over to the Unity engine. This has been successful so far, but I've run into a block I can't find a way around, which is checking my dictionary to check if the words the player inputs are actually valid.

My dictionary is in simple .txt format, with each word listed in the following format:

 aals
 aardvark
 aardwolf
 aargh

The game was originally built in Python, where I used ReadLines() to load the dictionary into an array (set) so I could loop through it when searching for words. I can't find any comparable way to do that with Unity however; text input/output functionality seems to be lacking with UnityScript.

Anyone know how I could check if a string variable exists within the dictionary?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by whydoidoit · Jun 16, 2012 at 12:23 AM

Create your file as a .txt and put it in a Resources folder.

    import System.Collections.Generic;
    import System.Linq;

    ...

    var lex : Dictionary.<String, String>;

    function Awake() {
  
        var textAsset = Resources.Load("YourFileNameWithoutTheDotTxt", typeof(TextAsset))  as TextAsset;
        lex = textAsset.text.Split("\n"[0]).ToDictionary(function(w){return w;});
    }

    function Checkword(word : String) {
         return lex.ContainsKey(word);
    }
Comment
Add comment · Show 16 · 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 Wolfram · Jun 16, 2012 at 12:47 AM 2
Share

Note Lists (as well as Arrays) are slow when searching entries, it's O(n). Using a binary search for alphabetized sorted Lists is still O(log n).

avatar image whydoidoit · Jun 16, 2012 at 12:49 AM 0
Share

Yeah I know I'd never use one in anger :) Shouldn't have been so lazy.

avatar image whydoidoit · Jun 16, 2012 at 12:51 AM 1
Share

Have updated it to use an O(1) operation.

avatar image Wolfram · Jun 16, 2012 at 12:55 AM 0
Share

Ah, nice, didn't know you could parse the file and fill the Dictionary in one line :D

avatar image Wolfram · Jun 09, 2013 at 03:57 PM 1
Share

Declare your dictionary as:

 Dictionary<String,String> wordList = new Dictionary<String,String>();

, and then try something like:

 wordList = textAsset.text.Split(new Char[] {'\n'}).ToDictionary(w => w);

Not absolutely sure about the syntax, though, never really used this.

Show more comments
avatar image
3

Answer by Wolfram · Jun 16, 2012 at 12:42 AM

The by far fastest method to test whether a string is within an arbitrary large array/set/list of strings is by using a Hashtable, it will find objects in O(1) time.

Instead of storing your strings in a regular array or list, use:

 myHashtable.Add(myWord,myWord);

or:

 myHashTable[myWord]=myWord;
 

(the second "myWord" entry doesn't really matter, as long as it's non-null. You could also use it to store additional info)

To test whether a word exists use:

 myHashtable.Contains(searchWord);
 

To retrieve the second parameter mentioned above use:

 secondParameter=myHashtable[myWord];

Note you can also store different objects instead of just strings, even GameObject references and so on.

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 whydoidoit · Jun 16, 2012 at 12:44 AM 0
Share

Damn my laziness of not using a Dictionary has been found out!!!

avatar image
0

Answer by Kurius · Jul 24, 2018 at 09:45 PM

I had to combine some ideas from a couple users here in order to arrive at this correct line...

lex = textAsset.text.Split("\n"[0]).ToDictionary(w => w);

User whydoidoit didn't have the w => w

User Wolfram didn't have the "\n"[0]

Hope this helps others :-)

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
avatar image
0

Answer by unity_Jbg_1vKSGY4bdQ · Apr 03, 2021 at 06:56 PM

For those struggling with why ContainsKey() does not find your strings a match, it's because Split() adds an empty character to the end of your strings/keys. To solve this issue, simply add a Remove() to your code:

 lex = textAsset.text.Split("\n"[0]).ToDictionary(w => w.Remove(w.Length - 1);

Also, if you want to read your .txt file from a path, you may want to put the file into the StreamingAssets folder. See details here

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

10 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

Related Questions

High score local [Android] 1 Answer

How to managing text file 1 Answer

Reading from a file not working on Android 0 Answers

dictionary, random element (js) 1 Answer

importing english dictionary as text file? 1 Answer


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