0

I'm currently finishing my game for iOS using ue4. I'm using some specific iOS Code and try to call Methods using the 'self' variable

void Aaccessactorios::Shareoption() {
    [self Sharebutton];//ios                 
}

the Code is within a prepocessor tag #if PLATFORM_IOS and #endif

I think that I've declared the self variable at the start of my cpp file:

-(id)init
{
    self = [super init];

    return self;
}

Other Forums Mention that Unreal engine compiles all the cpp .mm at the end, so I don't think that's the issue.

the self variable is used all around the objective c part, so it is crucial to my Code. How do I mix the Code properly and get my self variable to be declared?

Thank you

edit: this is the part i try to recreate https://answers.unrealengine.com/questions/422323/callback-from-objective-c-back-to-the-c-layer.html

  • 1
    As far as I know, a class can't be both a C++ class and an Objective-C class. `self` is to Objective-C what `this` is to C++ - you don't declare it, it's provided for you by the language.( I also believe that your `init` should produce an error if it's in a .cpp file.) – molbdnilo Aug 24 '18 at 13:19
  • `self` is undeclared identifier. What is unclear? Declare variable and assign object to it. – Cy-4AH Aug 24 '18 at 13:51
  • Note that the example you linked does not use `self` in a C++ class – molbdnilo Aug 24 '18 at 18:56

1 Answers1

2

I mix C++ and Obj-C all the time. An Obj-C class instance is not the same as a C++ instance. A C++ class instance has this. There is no self. Obj-C class instance has self. You can read more about it here:

Difference b/w Objective C's self and C++'s this?

Assuming from what you have written, you have a C++ class Aaccessactorios. It is not clear who has defined Sharebutton. If it is in the C++ class, then you would call it as

this->Sharebutton();

or simply

Sharebutton();

If Sharebutton belongs to an Obj-C class, then you need to have the C++ either have a pointer to the Obj-C class instance, or have it passed into the function. An example of the latter would be:

void Aaccessactorios::Shareoption(ObjCClassObj *obj) {
    [obj Sharebutton];//ios                 
}

In that code reference link, you can see they actually do the former.

 class IOSMediaPlayerController
 {
 public:
     IOSMediaPlayerController();
     ~IOSMediaPlayerController();
     void startPlayback();
     void stopPlayback();
     void callbackFunc();

     #if PLATFORM_IOS
     IOSMediaPlayerControllerObj *playerObj;
     #endif    
 };

Here you can see that playerObj is a reference (pointer) to the Obj-C object. This created in the constructor with this

 IOSMediaPlayerController::IOSMediaPlayerController()
 {
     #if PLATFORM_IOS
     playerObj = [[IOSMediaPlayerControllerObj alloc] init];
     #endif
 }

And then gets invoked by

 void IOSMediaPlayerController::startPlayback()
 {
     #if PLATFORM_IOS
     [playerObj startPlayback];
     #endif
 }

Note the example code does not call self, it calls the constructor created instance playerObj.

Mobile Ben
  • 7,121
  • 1
  • 27
  • 43