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 donimation · Dec 22, 2016 at 07:30 PM · arrayleaderboardsearchwww classdatabase handling

SOLVED - How can I search and retrieve a single element from a database array? [C#]

Hello everyone,

I am trying to search a database so that I may get the score for a single player. My goal is to be able to store said score inside a int variable so that I may be able to compare it with something else.

At the moment, I'm trying to figure out how IEnumerator and the WWW module work. Luckily, I have some working examples to go by. For instance, this is how a tutorial showed me how to get all the scores with dreamlo.

HiScores.cs

 public void DownloadHighscores()
 {    StartCoroutine("DownloadHighscoresFromDatabase");
 }

 IEnumerator DownloadHighscoresFromDatabase()
 {    WWW www = new WWW(webURL + publicCode + "/pipe/");
     yield return www;

     if (string.IsNullOrEmpty(www.error))
     {    FormatHighscores(www.text);
         highscoresDisplay.OnHighscoresDownloaded(highscoreList);
     }
     else 
     {    print("Error Downloading: " + www.error);
     }
 }

The tutorial however does not go into how to download a single score. Luckily, the dreamlo asset comes with a dreamloLeadboard.cs class. Inside I found the following method:

 IEnumerator GetSingleScore(string playerName)
     {
         highScores = "";
         WWW www = new WWW(dreamloWebserviceURL +  publicCode  + "/pipe-get/" + WWW.EscapeURL(playerName));
         yield return www;
         highScores = www.text;
     }

I've incorporated (more like "Frankensteined") this into my HiScores.cs class like this:

 public void GetTheSingleScore()
 {    // print("say something");
     StartCoroutine(GetSingleScore(nameFromInputFieldString));
 }

 IEnumerator GetSingleScore(string playerName)
 {
     highScores = "";
     WWW www = new WWW(webURL +  publicCode  + "/pipe-get/" + WWW.EscapeURL(playerName));
     yield return www;
     highScores = www.text;
     // print("anything");
 }

Good news is, I'm not getting any errors:| Howerver I can't tell if this is even doing anything. When I call the GetTheSingleScore() method from a button I don't even get the printout logs I've commented out above.

Am I on the right track or am I "missing the mark" completely? I have a feeling I'm making this more complicated than it needs to be..

If this is something you've come across recently, please let me know how you approached it. If you can think of Anything that might steer me in the right direction, this humble noob would be most grateful! Any advice, comment or suggestion is welcome.

Best regards,

Don

(This is related to another Question I posted a few days ago. I've made a little bit of progress since; I've at least figured out how to delete a username entry! I will be posting the answer for that there soon. I think/hope I'm on the right track with this too, Cheers..)

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 donimation · Dec 28, 2016 at 06:31 PM 0
Share

This thread seems to address the same thing. I will try to implement the method found here: [https://forum.unity3d.com/threads/string-get-specific-username-from-string-array.361470/][1]

Getting close.. [1]: https://forum.unity3d.com/threads/string-get-specific-username-from-string-array.361470/

3 Replies

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

Answer by donimation · Jan 10, 2017 at 08:39 PM

Thanks again @TheyLeftMe4Dead (and btw welcome to the community!) and @GrayMatterTutorials for the feedback! The solution heavily involved this helpful UF thread.

The following addition to my HiScores.cs class, along with some other messy code, did the trick for me. First I deleted my GetTheSingleScore() and IEnumerator GetSingleScore(string playerName) methods . I then I added a slightly different version of some existing methods like this:

     public void DownloadJustOneScore(){
         StartCoroutine("DownloadOneScoreFromDatabase");
     }
 
     IEnumerator DownloadOneScoreFromDatabase()
     {    WWW www = new WWW(webURL + publicCode + "/pipe/");
         yield return www;
         if (string.IsNullOrEmpty(www.error))
         {    nameFromInputFieldString = inputFieldnameGraber.text;
             // check for (and replase) spaces
             nameFromInputFieldString = nameFromInputFieldString.Replace (" ","");
 
             GetSinglePlayerScoreB(www.text , nameFromInputFieldString);
         }
     }
 
 
     void GetSinglePlayerScoreB(string textStream, string playerName)
     {    string[] entries = textStream.Split(new char[] {'\n'}, System.StringSplitOptions.RemoveEmptyEntries);
         for (int i = 0; i < entries.Length; i ++)
         {    string[] entryInfo = entries[i].Split(new char[] {'|'});
             string username = entryInfo[0];
             if (username == playerName)
             {    print("name already exists");        
                 int score = int.Parse(entryInfo[1]);
                 PlayPref.nameTaken = true;
                 print(username + ": " + score);
                 stateSet.checkAvailability();
                 return;
             }
             else
             {    print ("name not found"); 
                 PlayPref.nameTaken = false;
                 stateSet.checkAvailability();
             }
 
         }
 
     }


The if else statements at the end basically trigger the .nameTaken variable. They also call the .checkAvailablility() method that uses said variable to enable/disable the appropriate UI.

I have a feeling that pipe-get --amongst many, many other things-- would have worked better, but I'm glad I got it to work at all!:D:

Anyway, I hope this helps others.

Cheers! :D

P.S. This answer also pertains to this related post.

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
1

Answer by GrayMatterTutorials · Jan 09, 2017 at 08:09 AM

I just watched the video tutorial. First off, I'm not sure how he is getting "print" to work, but you need to use Debug.Log("Message"); That will display everything you want to display. So pass in the www.text like that. Next, I hope you kept the FormatHighscores() method. You will be able to pass in the results from the single search and the get high scores search through that method. Otherwise, you will need to copy that method for the use of a single Highscore. I'll explain how that works:

First you make a request to dreamlo for the highscore of the specific person. Dreamlo has an example url stating:

Get your data as pipe delimited: http://dreamlo.com/lb/5872fb68b6dd1500a4c95652/pipe

and for a single person:

To get the score for just 1 person, just add -get and the person's name: http://dreamlo.com/lb/5872fb68b6dd1500a4c95652/pipe-get/Carmine

This (the url) is the value you are creating and then passing into the new WWW. So this is fine, you actually get the person the right way. If you type Debug.Log(highscores); instead of print("anything"); you will get the very thing you were looking for. However, I'm assuming it will come in an undesirable format. This was why the person who made the tutorial created the FormatHighscores() method. It takes the first part (username) and the second part (score) of the string you pass it. I recommend, for testing purposes, you call that method and pass the www.text to it instead of assigning your highscores variable. It will then overwrite the current array of high scores you have and have just that single high score stored in it.

I hope this helps! Good luck!

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 donimation · Jan 10, 2017 at 05:27 PM 0
Share

Thanks for the Feedback!

I actually worked out a solution to this last week. I am about to post how I did it in a bit.

print() has been working for me in C# but, based on recent comments, I think I'll start using debug.log more.

After reading through this forum thread I ended up -- as you said -- pretty much recycling the FormatHighscores() and some other IEnumerator methods until I got a "match" so to speak.

Thanks for looking out!

Cheers :D

avatar image
0

Answer by TheyLeftMe4Dead · Jan 08, 2017 at 06:51 PM

I don't know anything about the WWW module or the IEnumerator (but I'd like to check them out, they seem exiting (: ) but I do have suggestion for debugging. Try to use Debug.Log("") in your IEnumerator method rather than print(). I script in Java so I don't know if print() works or not. If that does nothing, then perhaps IEnumerator needs to be called from another method to run. Otherwise, I would research what exactly the WWW module and IEnumerator do.

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 donimation · Jan 10, 2017 at 04:23 PM 0
Share

Thank you for the response! I actually did do some research and worked this out last week. I will post my solution soon.

As I'm getting better at coding I'm realizing how useful debugging is!:]

I reeeally haven't found much difference between print(() and debug.log() on the C# side of things, at least, so far.

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

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

Related Questions

Find index number in an Array of a transform by it's name 1 Answer

Search an array? 2 Answers

Splitting textasset into an array and binary search 1 Answer

Add lap time in scoreboard 1 Answer

Accessing List Array in Other Script Problem 2 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