• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Ibzy

"Parse" and Querying

8 posts in this topic

Hi All,

 

I have hit a bit of a snag on Android with regards to Querying a database.

 

I have set up an account with Parse.com which seems a very reasonable and clever solution to server hosting as a new developer.

 

In my set up I have 3 tables (or as Parse call them, classes) - Users, Cards, and Payer_Cards. All Users have a unique objectID, all Cards have a unique objectID and Player_Cards is a lookup between the two, containing Player ID and Card ID (not all players have all cards).

 

The below logic is my attempt, but for some reason I cant figure it out.  I can get the Card IDs listed based on all cards against the player ID (currentUser), but when I try to expand this to look at table 3 (Cards) it all falls apart.

public void cardList(List<ParseObject> cardList){
    	StringBuilder cards = new StringBuilder();
		for(int i=0; i<cardList.size(); i++){
			cards.append("Card:");
			
			ParseObject[] cList = cardList.toArray(new ParseObject[0]);
			String cardId = cList[i].getString("CARD_ID");
			
					ParseQuery<ParseObject> query = ParseQuery.getQuery("CARDS");
			        query.whereEqualTo("objectId", cardId);
			        query.getInBackground(cardId, new GetCallback<ParseObject>() {
			            public void done(ParseObject object, com.parse.ParseException e) {
			                if (e == null) {
			                	cards.append(object.getString("CARD_NAME"));
			               } else {
			                    
			                }
			            }
			        });
	    	
    	}
		
		TextView CardsList = (TextView) findViewById(R.id.CardsList);
        CardsList.setText(cards.toString());
	}

The current error in my code is that "cards" isn't recognised within the if block.

 

Any assistance on this is greatly appreciated - I have exhausted all of my ides of how to get around this.

 

Thanks

Edited by Ibzy
0

Share this post


Link to post
Share on other sites

I'm preety sure cards isn't visible within the if block because you're overriding a method with that anonymous class. That method can't see whats within cardList method (ie, the cards StringBuilder).

 

You're basically referencing a local variable that is inside another method that your GetCallback object simply doesn't knows about.

 

At most, from inner classes (or this anonymous class) you can reference fields of the enclosing object. ie, if cards were a field of whatever object has that cardList method, then you could access it from the anonymous class (and with a bit of magic from javac if the field isn't public).

Edited by TheChubu
0

Share this post


Link to post
Share on other sites

You should be able to make your code compile by declaring [tt]cards[/tt] to be final. This will make it accessible within the anonymous class.

 

However, that won't actually make what you are trying to do work. It is highly unlikely that the [tt]GetCallback<ParseObject>.done()[/tt] will be invoked until sometime after the [tt]cardList()[/tt] method has already returned, which means that your textview will be set to an empty string.

 

You really need to make StringBuilder an instance variable, and update the textview in each parse callback.

2

Share this post


Link to post
Share on other sites

You can also reference local variables or parameters marked as final. Given that you don't appear to assign to "cards", this should work compile here.

 

Edit: as swiftcoder mentions, an asynchronous call like that cannot be guaranteed to have run by the time you try to use the variable. In addition to his suggestions, you may also have to deal with threading here, which means you'll need to correctly synchronise your code, or use a thread safe class (such as [url="http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuffer.html"]StringBuffer[/url]). Read the documentation very carefully about how getInBackground() works.

Edited by rip-off
1

Share this post


Link to post
Share on other sites

Hi Chubu,

Thanks - that makes a lot of sense, and is kind of what I thought was happening.  Can you think of a way to get around this?

 

I considered setting a public variable and having it set to the new string on each tick of the for loop, but seems to return null.

 

I imagine the main problem here is that Parse use a bit of their own terminology which no-Parse users wouldn't be familiar with?  If the query didn't need that public void done() I'd probably be ok.

 

Thanks

0

Share this post


Link to post
Share on other sites

Hi Guys,

 

Thanks for the pointer - setting the StringBuilder as an instance var works like a charm when it comes to letting me update it from various functions.

 

Now I just need to figure out why the CARD_NAME is blank :)

 

Thanks again!

0

Share this post


Link to post
Share on other sites

Hi Again,

 

After some debugging I found that it is not blank, the StringBuilder just isn't populated prior to being set.  Very much a simpleton question here I'm sure, but how do I synchronise my code? I've always thought it runs in order and each function is completed before it moves on.

 

I've changed StringBuilder to StringBuffer, and the app still compiles, but the data is still blank? I assume this is due to the synchronising you mention?

 

Thanks

 

Edit:

My ParseQuery public void section now looks like this:

public void done(ParseObject object, com.parse.ParseException e) {
			                if (e == null) {
			        			cards.append("Card: ");
			                	Log.d("Card","Contains " + object.getString("CARD_NAME"));
			                	cards.append(object.getString("CARD_NAME")+"\n");
			                	
			            		TextView CardsList = (TextView) findViewById(R.id.CardsList);
			                    CardsList.setText(cards.toString());
			               } else {
			                    
			                }
			            }

This compiles and gives me the list I expect, however when the page loads I can see the list being populated - is this something I need to drag into the very beginning and cache, or am I doing something wrong which slows it all down?

 

Thanks

Edited by Ibzy
0

Share this post


Link to post
Share on other sites

You are kicking off a number of background jobs in a loop there. As they return, the list gets updated with the results. Perhaps the code could be modified to use one of the builk queries that the ParseQuery API appears to offer?

 

It is hard to offer any advice about what you should be doing without knowing the specifics of what your application is trying to achieve.

Edited by rip-off
0

Share this post


Link to post
Share on other sites

I have looked at the documentation around the ParseQuery API but am only seeing example queries for single table queries (If Parse supported SQL I would have this nailed).

 

Apologies for being rather vague in my question - I felt that general practice would cover the issues I'm having here, so maybe that's not the case.  My end goal is a card game (as I imagine you may have guessed), but with this snippet what I am trying to achieve is a list of cards owned by the current user.  There will be situations where cards get added, I would need a separate list for a deck which will contain some of the cards in the entire list, and perhaps the ability to trade cards with other players (this part is a long way off).

 

I know it might appear as though I'm starting from the middle, however I am trying to ensure I can fully query the Parse database in the way that I need to before deciding it is what I want.  Having read about peoples issues when taking a standalone app and making it work with users/logins, I am covering this part off first and adding the game logic to it.

 

Cheers

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0