Another Game and Facebook Woes (er… Tutorial)

Guillotine Image

Who Cut the Deck - A Facebook card game.

First, I’ll cut to the chase and announce the alpha release of a card game I’ve been working on called Who Cut The Deck.

The idea for the game  was born of a necessity.  A friend of mine is leaving town, but we regularly get together to play cards or board games.  We considered the idea of using some kind of Skype arrangement to handle the games, but a basic problem exists for any game which uses cards… we can’t network the arrangement of shuffling.  So, I decided to make a network card shuffler.

It’s expanded, as these things do.  And the goal for me has now become to as closely match as possible the feel of being in the same room sitting around a table playing these games.  Thus it’s in 3d, it has physics simulation, and hopefully at some point I can get sound and video achieve the verisimilitude.

Finally, I’ll admit the point wasn’t to use a standard deck, but some of the more interesting card and board games that are out there.  Of course, these won’t be released as I’m keeping them for our personal use, but I may approach the copyright owners at some point and see if something can be arranged.  Until then, I hope people outside our group of friends will enjoy playing any game possible with a normal deck of cards.  There are, after all, quite a few of them.

Now, to maintain some of the technical quality of this site, I’ll talk about some of the trials and tribulations of integrating Facebook and Unity.  It’s not as bad as trying to network from scratch (the worst possible programming hell you’ll ever face, thank you Unity/Raknet for taking that out of my hands), but it’s not as easy as you might expect from some of the tutorials around the web.

One thing is that Facebook has very recently changed the API.  You can still use existing methods, but they are probably going to be deprecated.  I continue to use them (specifically the original PHP api) myself, because I just wanted to get it working, but it’s good to know where things break down.

I won’t list them here because it’s easy enough to search the web for “Unity3d Facebook Tutorial”, but a good one I used to setup is here.  As of this writing, it is not yet complete.  I’ll expand on it a little bit here.

The last entry ends with a query to the image FQL.  This is a nice start, but one of the most important things you’ll run into is that using PHP you’ll need to do a large portion of your queries on the front end.  This is because PHP processes on the server, but Unity runs on the client.  Now, you can also setup some PHP helpers callable from Unity using it’s WWW class, but none of these can use authorization directly.  I believe it is possible to establish the authorization with Unity in the initial load and pass this into unity to forward back to these helper queries, but I haven’t made that work yet.

In the meantime, you can only get basic information available without access to the login (in my case I query name and profile image).  The following code is how I pull out the name and image you see when you first load up the game.

<?php

$user_id = $_REQUEST["id"];

require_once 'facebook-platform/php/facebook.php';

$apikey = XXXXXX;

$secret = YYYYYY;

$facebook = new Facebook ($apikey, $secret);

$info = $facebook->api_client->users_getInfo ($user_id, array ('first_name'));

$query = "SELECT pic_big FROM profile WHERE id = $user_id;";

$result = $facebook->api_client->fql_query($query);

print $info[0]['first_name'].",".$result[0]["pic_big"];

?>

The Unity code that accesses this is:

var url = correctURL+"getFBProfile.php?id="+userId;

var www = new WWW (url);

yield www;

var data = www.data.Split(","[0]);

playerName = data[0];

if (imageCache.Contains(userId)) {

playerImage = imageCache[userId];

} else {

www  = new WWW (data[1]);

yield www;

playerImage = www.texture;

imageCache.Add (userId, playerImage);

}

Page 1 of 2 | Next page