Game Development Community

A Brief Scoreloop Integration Tutorial

by Craig Fortune · in iTorque 2D · 10/18/2009 (12:57 pm) · 43 replies

Quite a few people seem to be struggling with integrating Scoreloop in to their projects, which is a shame as it is a great little system. Here is a quick little tutorial on how to do it. This is barebones and simply gets you “on your feet” with working with Scoreloop.

DISCLAIMER: This was very quickly typed up (had to strip some of my own bits out) so I have probably missed some bits etc, if you spot them please let me know so I can update these instructions for other people. Thanks. (Also, you'll probably see some weird/odd comments - ignore those, this is just ripped out from my working build for my game :D)

First off, I’m assuming you have followed the instructions as part of the Scoreloop download , if not, go do those steps first!

TGBAppDelegate.h needs to be changed, first up is to add the include - I have my Scooreloop directory inside my XCode_iPhone directory alongside my project file)
#import <Scoreloop/Scoreloop.h>

the @interface part needs the protocols it abides to changing too, like thus:
@interface TGBAppDelegate : NSObject <UIApplicationDelegate, SLClientDelegate>

Also, in the @interface part add the following to add the score loop client object in.
id<SLClient> scoreloopClient;

and add a property below like thus:
@property (nonatomic, retain) id<SLClient> scoreloopClient;

Now in TGBAppDelegate.mm make the following changes and additions:

Add scoreloopClient to our synthesize list
@synthesize window, scoreloopClient;

In - (void)applicationDidFinishLaunching:(UIApplication *)application

Add the following two lines: (these are features in Scoreloop, read the docs for more info)
[scoreloopClient setFeature:kSLFeatureTeaserStrategy enabled: NO];
[scoreloopClient setFeature:kSLFeatureAutorotateInterface enabled: YES];

- (id) init
Should be changed to look like the following: (this setups up scoreloop - NOTE THE BITS YOU NEED TO CHANGE!)
- (id) init
{
	self = [super init]; 
	if (self != nil) 
	{ 
		// Create the scoreloop client. 
		// The init method of the Application Delegate is 
		// the recommended place 
		scoreloopClient = SLClientCreateWithGameAndDelegate( 
							@"    GAME ID FROM SCORELOOP GOES HERE!!!!!   ",  
							@"    SECRET FROM SCORELOOP GOES HERE!!!!!    ",   
							self); 
	}	
	return self;
}

Add the following: (The Con::executef stuff is because I like to handle changes with Scoreloop status in script – they are just global script functions – always handy to have callbacks etc :D)
- (BOOL) application:(UIApplication *)application handleOpenURL:(NSURL *)url 
{ 
	// let Scoreloop know that another app started the game. 
	// If Scoreloop can't handle the URL, it will return NO 
	// This is important to implement as this enables starting 
	// challenges from other applications 
	return [scoreloopClient handleOpenURL:url]; 
}
- (void) scoreloopDidShow:(id<SLClient>) aClient
{
	// Scoreloop UI Active and showing
	Con::executef(1, "onScoreloopDidShow");
}

- (void) userDidFinishScoreloop:(id<SLClient>) aClient usingContext:(NSDictionary*) aContext
{
	// User successfully finished with Scoreloop UI
	// WITH CHALLENGE
	Con::executef(2, "onUserDidFinishScoreloop", "");
}

- (void) userDidCancelScoreloop:(id<SLClient>) aClient
{
	// User unsuccessfully finished with Scoreloop UI
	// NO CHALLENGE
	Con::executef(1, "onUserDidCancelScoreloop");
}

- (BOOL)client:(id) aClient shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}

Now create ScoreloopHandling.h inside the same folder as TGBAppDelegate with the following code:
//
//  ScoreLoopHandling.h
//  Torque2D
//
//  Created by Craig Fortune on 07/08/2009.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#ifndef SCORELOOPHANDLING_INCLUDED
#define SCORELOOPHANDLING_INCLUDED

namespace ScoreLoopHandling
{
	void submitScore(int score);
	void showScoreLoop();
}

#endif

Here is the matching .mm file…
#include "platformiPhone/ScoreLoopHandling.h"
#import <Scoreloop/Scoreloop.h>

void ScoreLoopHandling::submitScore(int score)
{
	id<SLClient> scoreloopClient = [(id)[UIApplication sharedApplication].delegate scoreloopClient];

	NSNumber* scoreResult;
	scoreResult = [NSNumber numberWithInt: score];
									
	// for advanced game play (levels...) you can pass a context 
	NSDictionary* context = nil; 
	
	[scoreloopClient gameDidEndWithScoreResult: scoreResult usingContext: context];
}

void ScoreLoopHandling::showScoreLoop()
{
	id<SLClient> scoreloopClient = [(id)[UIApplication sharedApplication].delegate scoreloopClient];	
	[scoreloopClient show: kSLTabOverview];
}

The above files are your glue between your scoreloop and your game, lets add the consolemethods to actually utilize them now. In ConsoleFunctions.cc add the following right at the bottom.

ConsoleFunctionGroupBegin(Scoreloop, "Functions for dealing with Scoreloop");

ConsoleFunction( endGameWithScore, void, 2, 2, "")
{
	argc; argv;
	
	int score = dAtoi(argv[1]);
	ScoreLoopHandling::submitScore(score);
}

ConsoleFunction( showScoreLoopGUI, void, 1, 1, "")
{
	ScoreLoopHandling::showScoreLoop();
}

ConsoleFunctionGroupEnd(Scoreloop);

You need to add this to the top of ConsoleFunctions.cc too...

#include "platformiPhone/ScoreLoopHandling.h"

You can now call the functions from script. For instance you could have a button that calls showScoreLoopGUI(); to load up scoreloop, or when it is game over you can call endGameWithScore(%playerScore);
Page«First 1 2 3 Next»
#41
10/30/2009 (9:39 pm)
John, can you post your Greystipe TGBAppDelegate functions in a different thread please?
For some reason I can't get the adds to display and yet I don't get any errors either.
I wonder if my GSInit is correct.
Thanks.
#42
10/31/2009 (7:20 am)
I've not got the code with me at the moment, I'll post on Monday morning!
#43
09/15/2011 (12:53 am)
Hey i tried what you posted with cocos2d-iphone..
the code compiles fine.. but when i call

ScoreLoopHandling::showScoreLoop();

It gives an exception :

[HelloWorldAppDelegate scoreloopWillShow:]: unrecognized selector sent to instance 0x9705b50
Page«First 1 2 3 Next»