This article is simultaneously published on my wechat official account. You can follow it by scanning the QR code at the bottom of the article or searching HelloWorld Jieshao on wechat.

preface

In the blink of an eye, it has entered 2021. Today is the first day of work in 2021. First of all, I would like to congratulate you here :” I wish you a happy New Year and a wide range of financial resources “. After all, the New Year should be fresh and energetic for the rest of the day.

As the first working day of 2021, there will certainly be a lot of good articles. Today, I will talk about the most important part of UE4 development in mobile platform: data interaction.

How to implement data interaction

I wrote an article before called: If you’ve seen how to create plug-ins for the iOS platform, you already know how to create plug-ins for the UE4 to call on our native Third-Party libraries for iOS. The benefit of this, as I mentioned in that article, is that it allows game developers to focus on the game. And some UI adaptation is not good sub-functions or use UE development advantages are obviously lower than the native way to do the function of plug-in to solve, so that each of the solution will greatly improve the quality of the game.

But some careful people might have looked at my previous article on making plug-ins and said, “This article explains how to create plug-ins and call their interfaces, but it doesn’t tell you how to get the return value of plug-ins. If I want to log in, I need to get the login token returned by the plug-in ah! So I can have the game server verify whether the login was successful or failed.”

Don’t worry, today I will give you this to make up, please continue to read.

I first simulated a login environment. My plug-in provides a login interface. The interface is very simple.

LoginMangment.h

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

typedef void(^getLoginStr)(NSString *loginStr);


@interface LoginMangment : NSObject

+ (LoginMangment *)shareInstance;

- (void)callLogin:(NSString *)account pwd:(NSString *)pwd block:(getLoginStr)loginStr;

@end

NS_ASSUME_NONNULL_END
Copy the code

LoginMangment.m

@implementation LoginMangment

+ (LoginMangment *)shareInstance {
    static LoginMangment *_instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (_instance == nil){ _instance = [LoginMangment new]; }});return  _instance;
}


- (void)callLogin:(NSString *)account pwd:(NSString *)pwd block:(nonnull getLoginStr)loginStr{
    
    NSString *ss = [NSString stringWithFormat:@"LoginString is : %@+%@", account, pwd];
    loginStr(ss);
}

@end
Copy the code

Next, I want to introduce you to an important content, which is the core of this article: UE delegation.

You might laugh, but isn’t a delegate? Yes, UE4 delegates are the same as iOS delegates, but they are different in the form of code implementation.

Light says not to practice false handle type, then I give everybody to realize a simple entrust!

To implement a delegate, perform the following steps:

Commissioned by the statement

UE4 provides the following macro definition for delegates:

This one is a little simpler, because the callback function I defined above only returns one value, so I declare my delegate using the DECLARE_DELEGATE_OneParam macro, The delegate name is FStringDelegate, and we define the delegate member WriteToLogDelegate in the UE4 class. Finally, we declare a callback function, testCallBack, as follows:

#pragma once

#include "CoreMinimal.h"
#include "TestPlugin.h"
#include "Blueprint/UserWidget.h"
#include "MyUserWidget.generated.h"


DECLARE_DELEGATE_OneParam(FStringDelegate, FString);
/**
 * 
 */
UCLASS(a)class TESTUE4DEMO_API UMyUserWidget : public UUserWidget
{
    FStringDelegate WriteToLogDelegate;
    FTestPluginModule *module;
    
	GENERATED_BODY(a)UFUNCTION(BlueprintCallable)
    void callInitSDK(int param);
    
    UFUNCTION(BlueprintCallable)
    void callTriger(int param);
    
    void testCallBack(FString str);
    
};

Copy the code

Commissioned by the binding

The CreateUObject function is used to bind the delegate, so that when the delegate executes the callback, testCallBack will be triggered, and we will get the callback value as follows:

#include "MyUserWidget.h"


void UMyUserWidget::callInitSDK(int param){
    FStringDelegate writeToLogDelegate = FStringDelegate::CreateUObject(this, &ThisClass::testCallBack);
   #if PLATFORM_IOS
    module = new FTestPluginModule(a);module->initCollection(writeToLogDelegate);
    #endif
}


void UMyUserWidget::callTriger(int param){
    #if PLATFORM_IOS
    module->trigerCrash(a);#endif
}


void UMyUserWidget::testCallBack(FString str){
    const TCHAR* result = *str;
    FPlatformMisc::MessageBoxExt(EAppMsgType::Ok, result, TEXT("Callback"));
}

Copy the code

Perform entrusted

The callback bound to the delegate is executed by calling Execute(), but to check that the delegate is already bound before it executes, it’s safer for me to use this function: ExecuteIfBound() :

#pragma once

#include "Modules/ModuleManager.h"
DECLARE_DELEGATE_OneParam(FStringDelegate, FString);

#if PLATFORM_IOS
#import <TestLogin/LoginMangment.h>
#endif

class FTestPluginModule : public IModuleInterface
{
    FStringDelegate testDelegate;
public:

	/** IModuleInterface implementation */
	virtual void StartupModule(a) override;
	virtual void ShutdownModule(a) override;
    
    // Display the user UI
    void initCollection(FStringDelegate &myDelegate);
    
    / / collapse
    void trigerCrash(a);

private:
	/** Handle to the test dll we will load */
	void*	ExampleLibraryHandle;
};
Copy the code
void FTestPluginModule::initCollection(FStringDelegate &myDelegate){
    #if PLATFORM_IOS
    testDelegate = myDelegate;
    #endif
}

void FTestPluginModule::trigerCrash(){
    #if PLATFORM_IOS
    [[LoginMangment shareInstance] callLogin:@"zhangsan" pwd:@"123456" block:^(NSString * _Nonnull loginStr) {
        FString UE4Str = loginStr;
        testDelegate.ExecuteIfBound(UE4Str);
    }];
    #endif
}
Copy the code

Ok, after setting the Xcode signature certificate, plug in the real computer and run it. If there is no problem, the result should be as follows: click the post-initialization button and then click the login button, an Alert box will pop up with the account password, as shown in the picture:

Write in the last

This article tells the story of how for everyone today by going to entrust with the iOS native data interaction, until here, with a total of 4 article 3 article in front of, is all about UE4 docking and iOS tutorial, because I also came from a small white beginning step by step, to tell the truth to the novice UE4 development does not too friendly, There is too little information on the Internet, and I also have a lot of bugs. I have been filling holes in UE4 in the company, so I wrote the content of these articles according to the training standards for newcomers, including environment configuration, simple UI creation, iOS native plug-in docking, and data interaction. If you are an iOSer working in a game company, and happens to be the company set up a UE4 project, I hope my article can help you, thank you.

Related reading:

How does UE4 interact with native iOS data

UE4 Development how to create iOS platform plug-ins

UE4 development implements button event response

UE4 development configuration Xcode debugging environment

Please drink a cup ☕️ + attention oh ~

  1. After reading, remember to give me a thumbs-up oh, there is 👍 power

  2. Follow the public number – HelloWorld Jie Shao, the first time push new posture