MonoTouch and GameKit
Hi all,
Does anyone have a simple example that shows how to use the GameKit's GMSession and GKPeerPickerController classes within MonoTouch.
I've tried following the GKTank example ( developer.apple.com/iphone/library/samplecode/GKTank/listing4.html ), but am finding Objective C hard going and can't work out how to hook up this magical GKSession.Delegate or tap into the GKSession.ReceiveData events. To clarify I'm having a hard time working out how ObjC maps to the C# equivalents.
GKPeerPickerController peerPickerController = new GKPeerPickerController();
peerPickerController.Delegate = new GKPeerPickerControllerDelegate(); // Not too sure about this...
Then tried
peerPickerController.Delegate.PeerConnected += new EventHandler<GKPeerConnectionEventArgs>( MyPeerConnectedMethod );
but that gave me a compiler error of...
"Cannot assign to 'MyPeerConnectedMethod' because it is a method group"
Any ideas/pointers?
Does anyone have a simple example that shows how to use the GameKit's GMSession and GKPeerPickerController classes within MonoTouch.
I've tried following the GKTank example ( developer.apple.com/iphone/library/samplecode/GKTank/listing4.html ), but am finding Objective C hard going and can't work out how to hook up this magical GKSession.Delegate or tap into the GKSession.ReceiveData events. To clarify I'm having a hard time working out how ObjC maps to the C# equivalents.
GKPeerPickerController peerPickerController = new GKPeerPickerController();
peerPickerController.Delegate = new GKPeerPickerControllerDelegate(); // Not too sure about this...
Then tried
peerPickerController.Delegate.PeerConnected += new EventHandler<GKPeerConnectionEventArgs>( MyPeerConnectedMethod );
but that gave me a compiler error of...
"Cannot assign to 'MyPeerConnectedMethod' because it is a method group"
Any ideas/pointers?
In case anyone's wondering ( wow these forums are quiet! ), the way I managed to get GameKit Delegates working, was as follows.
GKPeerPickerController peerPickerController = new GKPeerPickerController();
peerPickerController.Delegate = new XnaTouchPeerPickerControllerDelegate(gkSession, ReceiveData);
peerPickerController.Show();
public class XnaTouchPeerPickerControllerDelegate : MonoTouch.GameKit.GKPeerPickerControllerDelegate
{
private GKSession gkSession;
private EventHandler<GKDataReceivedEventArgs> receivedData;
public XnaTouchPeerPickerControllerDelegate( GKSession aSession, EventHandler<GKDataReceivedEventArgs> aReceivedData )
{
gkSession = aSession;
receivedData = aReceivedData;
}
public override void ConnectionTypeSelected(GKPeerPickerController picker, GKPeerPickerConnectionType type)
{
Console.WriteLine( "User Selected a ConnectionType of : " + type);
if (type == GKPeerPickerConnectionType.Online)
{
picker.Dismiss();
picker.Delegate = null;
// Implement your own internet user interface here.
}
}
/*public override GKSession GetSession(GKPeerPickerController picker, GKPeerPickerConnectionType forType)
{
Console.WriteLine( "GetSession" );
return gkSession;
}*/
public override void PeerConnected(GKPeerPickerController picker, string peerId, GKSession toSession)
{
Console.WriteLine( "Peer ID " + peerId + " Connected to Session ID : " + toSession.SessionID );
// Use a retaining property to take ownership of the session.
this.gkSession = toSession;
// Assumes our object will also become the session's delegate.
gkSession.Delegate = new XnaTouchSessionDelegate();
gkSession.ReceiveData += new EventHandler<GKDataReceivedEventArgs>(receivedData);
picker.Dismiss();
// Remove the picker.
picker.Delegate = null;
// Start your game.
}
public override void ControllerCancelled(GKPeerPickerController picker)
{
Console.WriteLine( "ControllerCancelled");
picker.Delegate = null;
}
}
As you can see at the below links, the PeerPicker dialog displays correctly
http://twitpic.com/q9bsy
http://twitpic.com/q9qe8
This is not how delegates usually work in C#, or at least not how I would normally use them, and is more of an aggregated override of methods.
Well I hope this helps someone as it took me nearly a week to work out it was not standard .NET delegates.
GKPeerPickerController peerPickerController = new GKPeerPickerController();
peerPickerController.Delegate = new XnaTouchPeerPickerControllerDelegate(gkSession, ReceiveData);
peerPickerController.Show();
public class XnaTouchPeerPickerControllerDelegate : MonoTouch.GameKit.GKPeerPickerControllerDelegate
{
private GKSession gkSession;
private EventHandler<GKDataReceivedEventArgs> receivedData;
public XnaTouchPeerPickerControllerDelegate( GKSession aSession, EventHandler<GKDataReceivedEventArgs> aReceivedData )
{
gkSession = aSession;
receivedData = aReceivedData;
}
public override void ConnectionTypeSelected(GKPeerPickerController picker, GKPeerPickerConnectionType type)
{
Console.WriteLine( "User Selected a ConnectionType of : " + type);
if (type == GKPeerPickerConnectionType.Online)
{
picker.Dismiss();
picker.Delegate = null;
// Implement your own internet user interface here.
}
}
/*public override GKSession GetSession(GKPeerPickerController picker, GKPeerPickerConnectionType forType)
{
Console.WriteLine( "GetSession" );
return gkSession;
}*/
public override void PeerConnected(GKPeerPickerController picker, string peerId, GKSession toSession)
{
Console.WriteLine( "Peer ID " + peerId + " Connected to Session ID : " + toSession.SessionID );
// Use a retaining property to take ownership of the session.
this.gkSession = toSession;
// Assumes our object will also become the session's delegate.
gkSession.Delegate = new XnaTouchSessionDelegate();
gkSession.ReceiveData += new EventHandler<GKDataReceivedEventArgs>(receivedData);
picker.Dismiss();
// Remove the picker.
picker.Delegate = null;
// Start your game.
}
public override void ControllerCancelled(GKPeerPickerController picker)
{
Console.WriteLine( "ControllerCancelled");
picker.Delegate = null;
}
}
As you can see at the below links, the PeerPicker dialog displays correctly
http://twitpic.com/q9bsy
http://twitpic.com/q9qe8
This is not how delegates usually work in C#, or at least not how I would normally use them, and is more of an aggregated override of methods.
Well I hope this helps someone as it took me nearly a week to work out it was not standard .NET delegates.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| GameKit bluetooth failing on iPad? | Cirdan | 0 | 3,174 |
Sep 3, 2010 01:20 AM Last Post: Cirdan |
|
| Question re: GameKit and online connection types | Malarkey | 0 | 1,731 |
Jan 8, 2010 05:12 PM Last Post: Malarkey |
|
| GameKit - Bluetooth not shutting off when disconnected | bruss14 | 0 | 2,076 |
Aug 5, 2009 12:20 PM Last Post: bruss14 |
|

