Game Development Community

ObjectiveC problem

by Hilbert Reijngoudt · in iTorque 2D · 05/17/2012 (7:53 am) · 10 replies

I'm trying to add some functionallity for communicating with a web server. I'm doing this in objectivec and then expose the functionality to the script with console functions. Since i'm new to objectivec and normally only program in c++ i've been following some tutorials about objectivec first, after this i was following this tutorial :

docs.garagegames.com/it2d/official/content/documentation/Tutorials/iOS%20Functio...

But there's one thing that doesn't work, as soon as i add :

#import "platformiPhone/platformiPhone.h"

I'm getting errors(20) on type.h, "unexpected type name 'U16': expected expression"

I'm getting this error for: F32, S8, S16, S32

I have no clue why this doesn't work, my files have the same content as shown in the examples, as soon as i remove the import for platformiPhone.h it compiles fine.

Does someone know what's going on and how to fix it?

#1
05/17/2012 (8:53 am)
What's the name of the source file? If you're mixing C++ (Torque's innards) with Obj-C you need to name the source files with the .mm ending.
#2
05/17/2012 (9:57 am)
Thanks for the reply!
The source file's got the .mm extention so that's not the problem.
#3
05/17/2012 (5:55 pm)
Sometimes it has to do with the order of includes, sometimes including too many times - check that you aren't also using #include of the same files somewhere down the chain multiple times. You'll have to post more code before anyone can guess if that isn't it :)
#4
05/18/2012 (11:43 am)
Ronny is spot on correct. The order is extremely important. I just ran into this myself yesterday when rebuilding an Xcode project from scratch.
#5
05/19/2012 (5:40 am)
I don't think it's an issue with the order of the includes since i've only got 2 files included and i've tried both ways:-)

This is the code of my .h file :

@interface Communication : NSObject
{

}

@end

static Communication* communicationObject;

And this my .mm file :

#include "Communication.h"
#include "platformiPhone/platformiPhone.h"

@implementation Communication

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

@end

It compiles when i comment out the include for platformiphone.h
#6
05/20/2012 (7:03 am)
Anyone got any ideas about this one? I'm really going nuts about this:-(
#7
05/20/2012 (7:27 am)
For the halibut, try also importing types.h first, as that is where those are defined. You shouldn't need to, but worth a shot. Also try using standard #include instead of #import. #import is technically better, but is an objC thing so maybe the compiler is getting confused?

TBH though i'm shooting in the dark here.
#8
07/20/2012 (7:27 am)
I seem to be having the same problem and still haven't figured it out. I've reordered the includes, tried it with imports, including platform.h, others, etc. Still the same issue. When I uncheck the files to be built as part of the app, everything is fine.

The code is pretty simple...

in avgJoeIAPHandler.h
#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>

@interface avgJoeIAPHandler : NSObject <SKProductsRequestDelegate, SKPaymentTransactionObserver>

@end

In avgJoeIAPHandler.mm:
#include "console/console.h"

#import "avgJoeIAPHandler.h"

@implementation avgJoeIAPHandler 

-(void) initIAP {
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];  
}

- (void) requestProductData:(NSString*)productIdentifier {  
..
ConsoleFunction(avgJoeIAPInit, void, 1, 1, "Init the iWTIAPHandler") {  
    initIAPHandler();  
}
@end

The error is:

In file included from /Users/Joe/Projects/torque/MyProjects/Robot/buildFiles/Xcode_iPhone/../../../../engine/source/platformiPhone/avgJoeIAPHandler.m:10:
/Users/Joe/Projects/torque/MyProjects/Robot/buildFiles/Xcode_iPhone/../../../../engine/source/platform/types.h:38:31: error: unexpected type name 'F32': expected expression
static const F32 Float_One = F32(1.0); ///< Constant float 1.0

and so on... any help or pointers would be greatly appreciated!
#9
07/20/2012 (10:34 pm)
So I've got an idea for how I'll approach the problem and why this is an issue anyway.

First, it's probably because the object is being compiled with the types.h and has it's own definition of types.h that conflicts with other objects that are compiled together (thinking at the obj c level).

Second, when I comment out all the torque bits in the obj c class, everything works.

Going forward I'll make the TGBAppDelegate a delegate of the IAP code here, then when something needs to call a console function I'll just have it delegate the call and the TGBAppDelegate will deal with it. I used this same approach elsewhere in my code and it worked fine. It's annoying but I'm sure it will work.
#10
07/26/2012 (7:15 am)
Just wanted to circle back on this one. Here's what I did to solve my compiling issues:

#import <Foundation/Foundation.h>
#import <StoreKit/StoreKit.h>

@protocol avgJoeIAPDelegate 
-(void) onIAPPurchaseProduct:(NSString*)identifier;
-(void) onIAPPurchaseFailed;
-(void) onIAPProductsReceived:(NSString*)product;
-(void) onIAPTransactionsRemoved;
@end

@interface avgJoeIAPHandler : NSObject <SKProductsRequestDelegate, SKPaymentTransactionObserver>

  @property (strong, nonatomic) NSObject *delegate;
@end

This thing defines a protocol for the delegate (I'm no Obj C expert so please forgive any naive implementations). Any place I wanted to call a console function is now going to the delegate.

In TGBAppDelegate I did:

#import <UIKit/UIKit.h>
#import "avgJoeIAPHandler.h"

@interface TGBAppDelegate : NSObject <UIApplicationDelegate, avgJoeIAPDelegate> {
	IBOutlet UIWindow *window;
}

@property (nonatomic, retain) UIWindow *window;

-(void) onIAPPurchaseProduct:(NSString*)identifier;
-(void) onIAPPurchaseFailed;
-(void) onIAPProductsReceived:(NSString*)product;
-(void) onIAPTransactionsRemoved;

@end

And here:

avgJoeIAPHandler *iapHandler;  

void initIAPHandler() {  
    if (!iapHandler) {  
        iapHandler = [[[avgJoeIAPHandler alloc] init] retain]; 
        TGBAppDelegate *appDel = (TGBAppDelegate*) [[UIApplication sharedApplication] delegate];
        iapHandler.delegate = appDel;
        [iapHandler initIAP];  
    }  
}  

ConsoleFunction(avgJoeIAPInit, void, 1, 1, "Init the IAP handler") {  
    initIAPHandler();  
} 
..

It would be nice to know how to do this without the delegate but I couldn't get the various objects compiling in types to place nicely when those objects were compiled together.