2

I am using the Parse.com service to store names, Facebook IDs, scores and e-mail for my game.

So far, I can get one specific object (by specifying the unique objectID generated by Parse), but how can I get the first 10 objects sorted by the score value?

This is the code I have so far:

using UnityEngine;
using System.Collections;
using Parse;

public class GetFromParse : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    void getParseResults()
    {
        ParseQuery<ParseObject> query = ParseObject.GetQuery("IdealStunts");
        query.GetAsync("xWMyZ4YEGZ").ContinueWith(t =>
        {
            ParseObject gameScore = t.Result;
            float time = gameScore.Get<int>("time");
            string playerName = gameScore.Get<string>("name");
            string fbid = gameScore.Get<string>("fbid");
            string email = gameScore.Get<string>("email");
        });
    }
}

This only gets me the values for that specific objectID (xWMyZ4YEGZ), but not being a programmer, I have absolutely NO clue on how to get the rest and store them for usage.

I know that for sorting and getting the first 10 results I can use

var query = ParseObject.GetQuery("GameScore")
    .OrderBy("score")
    .Limit(10);

But I don't know where to go from there.

Any help would be greatly appreciated.

2 Answers 2

2

The answer turns out is in changing the generic type to float or int instead of ParseObject:

var score = obj.Get<float>("score");
        Debug.Log("Score: " + score);

Parse.com - How can I get all objects from a class in C#/Unity?

Sign up to request clarification or add additional context in comments.

1 Comment

If i have DateTime field 'ExpiredOn', will this work ? var expiry = obj.Get<DateTime>("ExpiredOn");
2

Don't pass the object ID in the query.

Assuming you saved a score like this:

ParseObject gameScore = new ParseObject("GameScore");
gameScore["score"] = 1337;
Task saveTask = gameScore.SaveAsync();

then you'll have to do something like this to retrieve some scores:

var query = ParseObject.GetQuery("GameScore").OrderBy("score").Limit(10);
query.FindAsync().ContinueWith(t =>
{
    IEnumerable<ParseObject> results = t.Result;
    foreach (var obj in results)
    {
        var score = obj.Get<ParseObject>("score");
        Debug.Log("Score: " + score);
    }
});

You might want to try Kii Cloud instead which is easier (see section "Retrieve Game Data"):

http://docs.kii.com/en/samples/Gamecloud-Unity/

12 Comments

Kii is really nice and easy, but what's really putting me off is that the players have to register/login.
Unfortunately, the loop doesn't start.
Double check that you're using the correct object name (eg. "GameScore") and the correct field (eg. "score") both when saving the object and in the query. The loop not executing means there are no objects matching the query.
I have checked and rechecked, and I'm not getting anything, not even errors.
Do you see the objects using the web interface?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.