Alex Weyland
Alex Weyland

Reputation: 177

Get all objects from Parse.com C# (Unity)

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.

Upvotes: 2

Views: 3992

Answers (2)

user3551807
user3551807

Reputation: 36

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?

Upvotes: 2

German
German

Reputation: 10412

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/

Upvotes: 2

Related Questions