Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Free easy to implement LeaderBoard

Discussion in 'Works In Progress' started by carmine, May 6, 2012.

  1. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    EDIT:
    It's in the Asset Store now!
    https://www.assetstore.unity3d.com/en/#!/content/3862


    I'm wrapping up a free leaderboard/website idea. You don't need your own database server. Everything is done with simple HTTP GET requests.

    To add a score of 1000 points for Carmine
    http://dreamlo.com/lb/YOUR_PRIVATE_URL/add/Carmine/1000

    To get the list of scores as pipe delimeted
    http://dreamlo.com/lb/YOUR_PUBLIC_URL/pipe

    or xml
    http://dreamlo.com/lb/YOUR_PUBLIC_URL/xml

    You can even use a line of javascript to embed the leaderboard on your website/blog.
    <script src="http://dreamlo.com/lb/YOUR PUBLIC URL/js" type="text/javascript"></script>

    I'd love to get some feedback before I add my sample code to the asset store.

    Visit the site:
    http://dreamlo.com


    Thanks!
     

    Attached Files:

    Last edited: Jul 17, 2014
    ckaroun likes this.
  2. stefmen

    stefmen

    Joined:
    Apr 14, 2009
    Posts:
    784
    Simple and effective.
    Will this also work on mobile devices?
     
  3. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    Yes!

    Try it out and let me know how you like it. Thanks!
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,626
    Does it save like name, date of score? Also, does it cost anything once you get it on asset store?
     
    Last edited: May 8, 2012
  5. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    It saves just name and score. It costs nothing it's totally free. You can play with it right now.
     
  6. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,626
    I'll check it out. Sounds cool.
     
  7. groscalin

    groscalin

    Joined:
    Apr 4, 2011
    Posts:
    6
    Last edited: Jul 3, 2012
  8. cpasjuste

    cpasjuste

    Joined:
    Apr 2, 2012
    Posts:
    145
    Hi carmine,

    I was actually going to develop something similar for my game, but why would i reinvent the well !

    My only problem before using your api is what will happen if you can't host the server anymore or abandon the project, would you release the server sources and a database backup ?

    Thanks in advance,
    David.
     
  9. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    Hey,
    The serverside code is part of another project and I can't release it...

    If I ditch it one day it'll all be gone, but that's what happens with any cloud based system... there is always a risk of the server/system being down... and that company will go out of business...

    For this.... the bandwidth and usage is soooo minimal. I really have no reason to take it down. Can I guarantee it'll be up in 5 years? No... Can I guarantee it'll be there for another couple of years? Absolutely...

    It's not a lot of work to implement yourself and there are some sources for something similar... But... this is quick and easy and free :)

    If you have any add-on suggestions let me know.
     
  10. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    Is the download not working? Or the Package isn't working?
     
  11. Finjitzu

    Finjitzu

    Joined:
    Sep 8, 2011
    Posts:
    160
  12. skoandi

    skoandi

    Joined:
    Jul 9, 2012
    Posts:
    67
    Thanks you so much! :D This took me 5 min to implement this in my game! :D
     
  13. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    Awesome! Post a link to your game when it's up somewhere! I love checking this stuff out.
     
  14. incenseman

    incenseman

    Joined:
    Nov 20, 2012
    Posts:
    90
    This is an awesome idea.

    It lacks only one thing that would take just a few minutes to do.

    Instructions.

    I have the script. I know the website to go to.

    The only thing I need that I don't have is the instructions on how to make the leader board work in unity.

    I am thinking that this is something that many people would want but do not know how to set it up in unity.

    What is the script applied to?

    What needs to changed or added to the script and where?

    Like I said. It is a great idea but under used because there is no detailed readme file that comes with it.

    Would it be possible to post a detailed instruction on how the leader board is used?
     
  15. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    There is an asset in the store for free with code. Did you download it?
     
  16. incenseman

    incenseman

    Joined:
    Nov 20, 2012
    Posts:
    90
    I have the dreamlo-leaderboard-sample.unitypackage.

    I got it from the link in this thread.

    I will redownload it from the asset store.

    This may take several hours as the asset store is ssssooooooo sssslllloooowwww.
     
    Last edited: Dec 22, 2012
  17. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    I'm not sure what else is needed besides the code I provided... When someone scores in your game, call the code to send the score... when you want to display the list of scores... call the code to get the list... and then display it using whatever GUI you are using.
     
  18. incenseman

    incenseman

    Joined:
    Nov 20, 2012
    Posts:
    90
    What is the script supposed to be assigned to?

    Where do I drag the script for the leaderboard in the unity scene?

    The script that is in the package must go into the scene somewhere when the game is built in unity.

    What do I do with the script when building the game to get the scores from the game to the leader board?
     
    Last edited: Dec 22, 2012
  19. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    Hey... I kept the amount of lines of code really low and simple. Just copy and paste from my script whatever lines of code you need to make your script work.
     
  20. incenseman

    incenseman

    Joined:
    Nov 20, 2012
    Posts:
    90
    My question is not about the code at all.

    There is a script in the package called LeaderBoardSample.cs.

    I just need to know what to do with that script.

    There are no instructions on that and the sample level leads me to think that script is assigned to nothing.

    The unity editor will not allow that.

    The script has to be assigned to something in the scene in the unity editor.

    I have added the needed URLs to the script and that was easy.

    I just need to know what type of entity the script is connected to in the unity editor before the game is compiled.

    My question is not about the code at all.
     
  21. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    I'm updating my sample code (right now) to give you a prefab and simpler code for your scene... stay tuned.. I'm almost done...
     
  22. incenseman

    incenseman

    Joined:
    Nov 20, 2012
    Posts:
    90
    Will do.

    Thank you very much. :)
     
  23. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    OK so check this out.. I made a prefab you can drop onto your scene... so all you need to do is this...

    .. put your codes into the prefab... (you'll see it in the editor) JUST the codes.. not the URL part...

    // reference the leaderboard in one of your scripts...

    dreamloLeaderBoard dl;

    void Start ()
    {
    // get the reference here...
    this.dl = dreamloLeaderBoard.GetSceneDreamloLeaderboard();
    }

    When you're ready to submit a score just do...

    dl.AddScore(playername, score);

    To load the scores... now.. keep in mind web requests need to be called with coroutines... so you need to sort of poll for new scores... then wait..

    dl.LoadScores(); // do this ONCE .. not every frame..

    Look at the new example on how to read the scores, it's much easier....

    I just re-submitted it to the asset store, but you can download it right here:
    View attachment $dreamlo-leaderboard-sample.unitypackage
     
  24. incenseman

    incenseman

    Joined:
    Nov 20, 2012
    Posts:
    90
    Got it downloaded.

    Will give it a try.

    Edit:
    I followed your instructions to the letter.

    I am sorry to say that the leader board did not sow in the lobby and the example board did not update on my site or on the dreamlo site.
     
    Last edited: Dec 23, 2012
  25. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    Email me directly: carmine@carmine.com

    -Carmine
     
  26. incenseman

    incenseman

    Joined:
    Nov 20, 2012
    Posts:
    90
    Sent you an email with screenies of the screens I get in the order that I get them.

    The page script is installed on the site as you can see at the bottom left of some of the screenies but there is nothing there but the example scores.
     
  27. akstudio

    akstudio

    Joined:
    Feb 4, 2013
    Posts:
    33
    Hey Carmine,

    I am using the Dreamlo leader board which I have found really easy to implement. Its really nice!

    One constraint I have is that if someone else has the exact same name as another player and beats their score, it overwrites the players name and score... is there anyway to have unique score postings?

    K
     
  28. abhuva

    abhuva

    Joined:
    Dec 23, 2011
    Posts:
    76
    Hey,
    just tried the unitypackage you provided and it always crashes my Unity on import. (My unity version is 3.5.6f4)
    Anything i can do to get it installed correctly?

    The error message is:
    type == kMetaAsset.type PathName.Find ("library/Metadata") != 0

    EDIT:
    I just realized after finding the Asset Store link, that this is for Unity4.x ...

    I dont really see the requirement for any Unity4.x functionality, so could you provide a unitypackage for 3.x too? This would be much appreciated =)
     
    Last edited: Feb 9, 2013
  29. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    View attachment $dreamlo.zip
    That's weird.. I wish we could put up 2 versions of the package. All my package is, is just a couple .cs files... here is a .zip of it, let me know if that works..

    View attachment $dreamlo.zip
     
  30. abhuva

    abhuva

    Joined:
    Dec 23, 2011
    Posts:
    76
    Getting the same crash with the zipped files.
    I guess its related to the prefab - seems for me that 4.x prefabs arent compatible with 3.x
    I admit, i was lazy and hoped for a drag and drop method - well, time to just dig into the scripts and set it up myself, shouldnt be too hard to figure out =)
     
  31. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    I think all the prefab has is the one file attached to it :)
     
  32. abhuva

    abhuva

    Joined:
    Dec 23, 2011
    Posts:
    76
    I just did my own scripts now (i am using UnityScript and had some problems gaining access to your c# scripts, i am not used to c#).
    But with the information provided at your website it was rather easy to set it up. Its working great.

    I just hope this stays around for a while!
    Oh, and not to forget: A big thank you!!!

    Edit:
    A couple of questions:

    If i have several games that i want to use with this, shall i use the same leaderboard and handle seperation myself or is it meaned to get a new private leaderboard for each game.
    How do you prevent misuse in case its the second? I dont believe people would spam your database, but i am just curious about it now...
     
    Last edited: Feb 10, 2013
  33. abhuva

    abhuva

    Joined:
    Dec 23, 2011
    Posts:
    76
    Would it be possible to customize the javascript you provide for embedding the leaderboard on my own website?
    The reason is:
    I wanted to allow players to use whatever names they want, without the issue that there might be already another entry with the same name.
    In order to achieve this, i used the "name" field to store a unique ID, and the "text" field to store the actual user-name.

    Overall this works great (the only downside is that i need to make 3 calls on the database, one to get the data and calculate a new unique iD, one to save the new data, and one to delete the last in the list - so that this doesnt grow like crazy)

    Now the problem with your script for embedding on my own website is: Its showing only the "name" and "score" field. I would need to get the "text" field instead (cause i am storing the user-name there).

    A really great solution would be if i could toggle the fields this script displays.

    Beside this, i am really happy with what you provided here !
     
  34. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    Maybe you could do an ajax/xml call to the public URL and then parse whatever fields you want need? If you google for javascript, xml, etc. You can find code to do that.

    Check out the code down this page (longer code block)
    http://www.webdeveloper.com/forum/showthread.php?229386-Possible-to-read-XML-from-another-site

    -Carmine
     
  35. akstudio

    akstudio

    Joined:
    Feb 4, 2013
    Posts:
    33
    Great idea!, could you use this for the unique ID? Just found it but have not tried it...

    Code (csharp):
    1. public static string GetUniqueIdentifier()
    2. {
    3.    return System.Guid.NewGuid().ToString();
    4. }
     
  36. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    Eh... I'm not exactly sure what you're asking. Are you asking me to add an additional field for UniqueID, or are you going to use the textfield and put that ID in it?
     
  37. akstudio

    akstudio

    Joined:
    Feb 4, 2013
    Posts:
    33

    Hey,

    In my previous post (quoted above) I mentioned that scores overwrite if the same name is used as there is no unique ID for each player. My question being, how do I get around this (hoping that I'm doing something wrong). Having read Abhuva's idea, that won't be hard to do.

    Would adding an additional unique ID field be hard to implement?
     
  38. abhuva

    abhuva

    Joined:
    Dec 23, 2011
    Posts:
    76
    It really depends what you want to do, and personally i think the amount of data we can store per entry is already enough for most simple cases.
    The current database carmine provides here have the following entrys you can use:
    "name" (string)
    "score" (int)
    "seconds" (int)
    "text" (string)
    "date" (not sure, might be read-only, havent toyed around with this one)

    Now, beside the "date" field, you are basicly free to do whatever you want with them.
    The scripts on the server will use "score" to sort the entrys and will use "name" as some kind of identifier (not sure how to call it correctly in english)

    The solution i did for myself is working like this:
    "name" - i am generating a real unique identifier for this field, lets say i want to have a maximum of 20 entrys, so i would use for example "0" - "20" here
    "score" - storing my score here as ususal, so it sorts correctly
    "text" - storing the user-name here (user name can be existing several times, doesnt matter now)

    To make this work correctly i do the following:
    1) call database to get a complete set of the data (i am retrieving xml and deserialize it - mainly cause its just so easy to do and handle the data then)
    2) now i search through the data and find a not used "unique iD" by comparing to the stored "name" fields, there are several options how you can do this
    3) once i have a new iD, i call the database and write my new dataset, i am using the "add-pipe" command here so i get the already sorted new list back without an additional call
    4) i just get the "name" field of the last entry in this new data-set
    5) if your data-set have now more than the desired entrys (i.e. more than 20) i make a last call to the database to delete this entry

    This enables you to not worry about the names, and it also makes sure the lists dont grow endless.

    I hope this explains good enough what i did there...

    Edit:
    About adding additional fields - i dont think this is needed. Whats provided is more than enough.
    If you want to allow same names for different users, you have to do something similar to what i described above anyway - this means seperating the "user - name" from the "iD" of the entry. Additional fields doesnt change this. You need to handle the logic for when you do a "new unique entry" yourself - cause this can differ greatly from case to case.
    If you want to still use the text field with other stuff beside storing the user name there, just add a char that isnt allowed in your user-names - like for example "#" and put whatever you want behind this.
    I.e: storing the name "Abhuva" + the info "wrote a wall of text" would translate to store the string
    "Abhuva#wrote a wall of text" in the "text" - field... you just use your normal String functions later to seperate it again...
    Hope this explains it well enough =)

    EDIT 2:
    The downside of my method is, that if a user with a high score submits this lots of time, it will cut out the rest of the entrys - cause i submit each entry of the same user as a seperate iD now.
    In order to solve this, one could store the choosen uniqueID (used in the "name" field) together with the players name in your game (PlayerPrefs) - this way you could avoid in most cases creating new entrys and cut out lower scored ones... If you have a name and uniqueID stored already just submit this... if not use the method i described above. But this is very specific already to our games code - so its up to us to handle the logic...


    @carmine: thanks for the link, will have a look at this.
     
    Last edited: Feb 12, 2013
  39. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    I agree with Abhuva... you could technically.. just store GUID-Name in the Name field or GUID~Name or some other kind of character (that isn't allowed in the username) to separate the GUID and the Name.

    Then when you display the list of scores in your code, just do an extra split or substring or something to get the left and right side of the name...
     
  40. akstudio

    akstudio

    Joined:
    Feb 4, 2013
    Posts:
    33
    Hey,

    Thank you both for the time you have taken to explain this.. I have now made it happen... i have used the "shortText" for the player name and the playerName for the GUID... so far so good...

    This leaderboard is AWESOME!!! I'm so happy to have this working now thanks to you both!!!
     
    ckaroun likes this.
  41. rahuxx

    rahuxx

    Joined:
    May 8, 2009
    Posts:
    537
    What if i want to do all on my website only?
     
  42. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    Then you'll need to program your own system.
     
  43. rahuxx

    rahuxx

    Joined:
    May 8, 2009
    Posts:
    537
    Do you know some tutorial or where can I get some guidance for this?
     
  44. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    Not sure what your webhosting setup is, but you'll need to learn how to use a database. You should find out if you have access to a MYSQL server. Or you could use some other ajax/xml/web based database system. Such as mongohq.com or Amazon's SimpleDB.
     
  45. adev

    adev

    Joined:
    May 30, 2013
    Posts:
    3
    Very cool Carmine! Thank you for this tool.

    Is it possible to get a score position in the leaderboard?
     
  46. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    Hey.. what do you mean by score position? Do you mean the number in the javascript or something?

    (You could always add your own number in the unity script)
     
  47. adev

    adev

    Joined:
    May 30, 2013
    Posts:
    3
    something like retrieving the row index of a score after adding it.
    instead of retrieving all results and looking for it.
    thanks
     
  48. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    Hey,
    I don't have this built in, but you could do something like this after you add a score... (I'm writing this off the top of my head)...

    Code (csharp):
    1.  
    2.  
    3. string nameWeAreLookingFor = "Carmine";
    4.  
    5. List<dreamloLeaderBoard.Score> scoreList = dl.ToListHighToLow(); // or high to low, whatever makes sense
    6. int index = -1;
    7.  
    8. for (int i = 0; i < scoreList.Length; i++)
    9. {
    10.   if (scoreList[i].playerName == nameWeAreLookingFor)
    11.   {
    12.      index = i;
    13.      break;
    14.   }
    15. }
    16.  
    17.  
     
  49. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,562
    The only issue I see is related to security. As a web developer that works on enterprise level applications and APIs I immediately take notice of these things. :)

    Using GET requests expose the "super secret" key to anyone looking at the network traffic. Unless I'm misunderstanding how it works, a better approach would be to hash the request using the secret key and include the hash along with the data. Additionally, it could go a step further toward how Amazon handles requests with S3 by including an expiration (UTC) time stamp in the hash and request to make sure requests can't be replayed after a period of time.

    Even better would be to construct the requests using POST, PUT, GET and DELETE to make it a truly RESTful API and use SSL on the server side to add an extra layer of protection. You could also implement something similar to S3 by adding headers matching specific request configuration and included the canonicalized list of headers in your hashed key (and put the key in an Authorization header instead of as part of the URL).
     
  50. carmine

    carmine

    Joined:
    Jan 4, 2012
    Posts:
    391
    The site has SSL available. You can also use POST requests instead of GET requests. It's a free service. I don't think high scores is a mission critical thing in most of the people's games who are using the service.

    I think if someone has a security concern they can use one of the paid solutions.
     
unityunity