Trying to get OpenFeint working

I am trying to use OpenFeint and have some issues:

When adding the user defined setting GCC_OBJC_CALL_CXX_CDTORS it says it already exists but I don’t see it anywhere ?

Adding the #import “OpenFeintPrefix.pch” gives me the error: #import expects “FILENAME” or . In the build the header file is iPhone_target_Prefix.pch

I added the OpenFeint folder ok, and managed to add the framework folders (security etc.) but that is as far as I have got.

If anyone here has OpenFeint working then any help would be much appreciated - thanks (I also posted over at OpenFeint)

Don’t add the C++ flag manuaully as a user defined setting. Go into the build settings and search for “dtors”, you’ll see a setting with a checkbox.

Your import is correct, but it looks like you’ve fallen victim to the ole’ copy/paste smart quotes :slight_smile: Re-type those quotes as straight to see if that fixes it.

Thank you that helped heaps. I have pm’d you on this (many thanks)

I followed through the tutorial video on how to set up OpenFeint in Xcode. How does one go about setting this up with Unity? ( ex. Getting events to trigger achievements). Would I need Unity iPhone Advance? Thanks,

brandon

You need to use some method to call external code. In Unity 1.0.x most people are using PlayerPrefs to bridge between the Cocoa and Unity layers. Unity 1.1 will support direct native calls.

The Enhancement Pack is an option that does all this for you, and spares you grief on the build side (Unity iPhone Enhancement - Better iPhone Games). Its next release for Unity 1.1 supports OpenFeint (although it doesn’t set up the project for you, so the work you’ve already done is not wasted, at least not until Unity overwrites it :wink: and direct native calls.

I keeped getting asked about this privately, so I’m going to post a way to do it here. But I would really recommend using the Enhancement Pack or holding off until 1.1 comes out. (HiggyB: feel free to kill this if I’m stepping out of line)

This works for iPhone Basic. I have several other things going on in my code, but this should be close to working code. This will get you set up for launching the OpenFeint dashboard from within Unity and give you an idea of how to hop in/out of Unity and pass data back/forth.

AppController.h should look something like this:

#import "OpenFeint.h"
// Notice the OpenFeintDelegate
@interface AppController : NSObject<UIAccelerometerDelegate, OpenFeintDelegate>
{
   UIWindow*	_window;
   bool         _inUnity; //<- this is new too
}
@end

What’s going on here is a simple safety flag is added to prevent nasty things from happening if we double up our calls on the script side and also to short circuit the Repaint if we don’t need it. We’re also an OpenFeint delegate so, at a minimum, we’ll know when the dashboard closes.

AppController.mm:
Add these functions to the file (just below the accelerometer function and before the @implementation EAGLView)…

-(void) launchUnity
{
   if (_inUnity)
      return;
   
   NSLog(@"AppController::launchUnity\n");
   _inUnity = true;
   [[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait];
   [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"_openfeint_dashboard"];
   [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"_start_unity"];
   
   UnitySetAudioSessionActive(true);
   UnityPause(false);
   
}

- (void) launchFeint
{
   if (!_inUnity)
      return;
   
   _inUnity = false;
   [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"_start_unity"];
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"_openfeint_dashboard""];
   
   UnitySetAudioSessionActive(false);
   UnityPause(true);
   
   [OpenFeint launchDashboardWithDelegate:self];
}

- (void) dashboardDidDisappear
{
   [self launchUnity];
}

That takes care of switching in/out of Unity at will. Now we need to init OpenFeint. So find the applicationDidFinishLaunching function and make it something like this:

- (void) applicationDidFinishLaunching:(UIApplication*)application
{
	printf_console("-> applicationDidFinishLaunching()\n");
	[application setStatusBarHidden:TRUE];
   
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"_openfeint_dashboard"];
   _inUnity = true;

	char const* appPath = [[[NSBundle mainBundle] bundlePath]UTF8String];
	UnityInitApplication(appPath);
   
   // Obviously you'll need your credentials here!
   [OpenFeint initializeWithProductKey:@"xxxxxxxxxxxx" andSecret:@"xxxxxxxxxxxx" andDisplayName:@"UnityApp" andSettings:nil andDelegate:self];
   
	_timer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / kFPS) target:self selector:@selector(Repaint) userInfo:nil repeats:YES];
	[self registerAccelerometer];
}

Now we need to monitor the PlayerPrefs that may be set in Unity script and react accordingly. Find the Repaint function and add this logic to it…

- (void) Repaint
{
   if (!_inUnity)
      return;
   
   if ([[NSUserDefaults standardUserDefaults] integerForKey:@"_openfeint_dashboard"] == 1)
      [self launchFeint];

For build settings: go to “Get Info” on your project in Xcode. Click on the build tab, select all configurations, and search for “dtors”. Check the box for that setting. Now search for “other linker”. See how it says ? Don’t add to that!!! You have to deal with Debug/Release separaely. Select Release in the Configuration drop down and add -ObjC to the linker flags. Now select Debug and add -ObjC to the linker flags.

The easiest way to add the libraries is to right click on your App in the Targets list and “Add Existing Frameworks” and click on the [+] in that dialog. Hint: the SQLLite lib will be under the Dylibs category.

Shwew. I think that’s it. Now in your Unity script, just do PlayerPrefs.SetInt("_openfeint_dashboard", 1); whenever you want to pause Unity and bring up OpenFeint. 8)

Some caveats: this is just a primer to get you started. I would really think about the performance implications of checking tons of PlayerPrefs in the Repaint thread. All this code will do is launch OpenFeint, I leave it as an exercise to the reader on submitting high scores and achievements.

Finally, you’ll want a PostprocessBuildPlayer script to copy your files for you when you build in Unity so you can keep your modifications. (I’m not really a fan of having the script inject code)

I’m not a part of the 1.1 Beta, so I have no idea how much this will break when 1.1 is out.

Awesome! Thanks for providing this for us.

Here’s one more step that I had to do after following these instructions that I thought I’d add here for anyone following this:

  • You have to add the following line to the existing .pch file in your project:
    #import “OpenFeintPrefix.pch”

After all this, I got it working for me… with the exception of trying to figure out how to get it into landscapeLeft mode instead of portrait.

Thanks again BlastOffGames!

so I figured out how to change it to landscape mode. Here it is incase anyone else needs it.

Put this line of code just after you initialize OpenFeint:

[OpenFeint setDashboardOrientation:UIInterfaceOrientationLandscapeRight];

Yeah, I purposely sidestepped landscape/portrait issues. :slight_smile: OpenFeint seems to be able to cope just fine, but it’s good to keep in mind that Unity always runs in portrait mode behind the scenes. It makes things, uh, interesting if you’re doing any mixing of Cocoa UI views in the Unity window.

I’ve got OpenFeint working now thanks to your instructions posted here.

I’ve got one main issue that I’m trying to figure out though.

My app initializes OpenFeint when it loads up, and there’s a button on the home screen to launch the openfeint dashboard. I found that if I click on the button to launch the dashboard before openfeint has popped up the notification window telling me it’s logged in or can’t login , then my app freezes completely.

Any idea on what I could call to check if openfeint is ready to have the dashboard launched? That way I can check before launching, thus not freezing?

I posted over at openfeint, but they’re too slow to respond.

nevermind, I figured it out.

Hello, I recently released a casual game made with Unity called Prairie Chute.

http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=324616043&mt=8

Several players are asking for global high scores, so I’m looking into the major social platforms. I want to try and get a test up and running tomorrow, but my lack of ObjC / XCode knowledge might be a problem. I have seen the instructions here for editing the C code (which I’m sure I can handle :)) but my bigger question is about the comment regarding “direct native calls” and Unity 1.1. My game is released with 1.1RC2… so does that support OpenFeint natively (without the need for the pref storing above)? If so, can someone provide me with an example of calling one of the OpenFeint functions from JavaScript? Not that familiar with this process… so any help would be much appreciated.

Thanks,
Matt

It’s not that simple. OpenFeint is implemented as an Objective-C class. The native calls supported by Unity 1.1 are simply C function calls. Now, if you’re clever you know that Obj-C is really just C anyway, and you can make calls through objc_msgsend() (although it will be tough passing valid arguments).

But it’s generally easier to just write a layer of C functions that create and use the OpenFeint library. This is what I am doing in the new Enhancement pack. I have a “startOpenFeint” function defined in C, and that function creates the facebook delegate and opens the session (basically just like their sample code.) Mono passes in strings as const char * so you’ll have to convert them to NSStrings using stringWithCString:. Overall, not so hard.

Of course, you could just get your hands on the Enhancement Pack!

Thanks bliprob, that helps my understanding. My objective is to get to market next week with the cleanest solution possible. The enhancement pack may do the trick it sounds. I have been looking into all 3 of the platforms listed in this thread and I like Agon, but OpenFeint seems to be the leader in terms of visibility.

Does your solution have these capabilities right now, or will they be released in a future version? Also, does it come with code examples and documentation?

Thanks,
Matt

Hey guys… thanks for this topic it has been a great read. I am interested in implementing OpenFeint in my Unity project but I am having some… issues.

At this point I am simply attempting to add the OpenFeint application files to my project and get it to build. I do this by simply dragging the folder into Xcode and dropping it on the root element of my project tree, selecting to generate groups recursively and copy files local if needed. After this I attempt to compile and it begins to compile the OpenFeint code and returns around 5k errors. Obviously there is additional setup required but I cannot find resources on what exactly is required. If anyone could provide me with information or links I have overlooked that would be very helpful!

I second that. There’s a video on the Open Feint Dev site that’s shows how to get it implemented with Xcode, but I can’t seem to find anything on getting this to work with Unity.

I would also be interested in any more in-depth instructions.

I’ve just done this actually - remember to add the right Frameworks, including the sqldylib one which is tricky to find correctly. There’s another thread on here that was massively helpful - http://forum.unity3d.com/viewtopic.php?t=29022&highlight=openfeint

Implementing it as a plugin is a different matter, however.

EDIT: Man, oh man, I linked back to this very thread. Forgive me… here’s a better help!

In AppController.mm

- (void) applicationDidBecomeActive:(UIApplication*)application
{
	printf_console("-> applicationDidBecomeActive()\n");
	// OPENFEINT ADD
	[application setStatusBarHidden:TRUE];
	
	NSDictionary* settings = [NSDictionary dictionaryWithObjectsAndKeys:
							  [NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight], OpenFeintSettingDashboardOrientation,
							  @"appname", OpenFeintSettingShortDisplayName,
							  nil
							  ];
	
	OFDelegatesContainer* delegates = [OFDelegatesContainer containerWithOpenFeintDelegate:nil
																	  andChallengeDelegate:nil
																   andNotificationDelegate:nil];
	
	[OpenFeint initializeWithProductKey:@"productkey
							  andSecret:@"secretkey
						 andDisplayName:@"yourname"
							andSettings:settings
						   andDelegates:delegates];
	
	thisApp = self;
	// AFTER OPENFEINT
	
	UnitySetAudioSessionActive(true);
	UnityPause(false);
}

and up at the top

#import "OpenFeint.h"
#import "OFHighScoreService.h"
// extras...
#import "OpenFeint+Private.h"
#import "OFControllerLoader.h"

and in AppController.h

@interface AppController : NSObject<UIAccelerometerDelegate, UIApplicationDelegate, OpenFeintDelegate, OFNotificationDelegate>
{
	UIWindow*			_window;
}

and in iPhone_target_prefix.pch

#import "OpenFeintPrefix.pch"

And a bunch more for plugin-functionality, but this should get you to the point where the dashboard // notifications popup at the beginning.

If it doesn’t - I’ll try again.

one thing to note though, is that the instructions outlined in this thread were for OpenFeint 2.0, but the new 2.1 version is a little trickier to get working.

Hi,

You can use the free AGON Online for easy integration into Unity:

Developer info: http://developer.agon-online.com
Developer signup: http://devdb.agon-online.com/developer

We provide a UnityPackage for easy integration with Unity: “http://developer.agon-online.com/downloads/db_downloads/unity/AGON Online v 1.2.4 Unity Plugin.unityPackage”

The package has still not been officially released on our developer site - any feedback can be sent to developer@agon-online.com or provided at http://devsupport.agon-online.com

Best regards,
Morten