Initialize Chafkit - Unreal Engine

To integrate Chafkit SDK into your Unreal Engine project, follow the steps below:

Download and Unzip Plugins Directory

  1. Download the Chafkit SDK archive file by clicking on the following link: Download Chafkit SDK for Unreal Engine (opens in a new tab).
  2. Once the download is complete, locate the downloaded archive file on your computer.
  3. Check if your Unreal Engine project directory already has a "Plugins" directory. If it doesn't exist, create a new directory named "Plugins" in your project directory.
  4. Within the "Plugins" directory, create a new directory named "ChafKit".
  5. Unzip the downloaded archive file and extract its contents into the newly created "ChafKit" directory.
  6. Make sure that the ChafKit.uplugin file is located directly inside the "ChafKit" directory, and not nested within any subdirectories.

Once you have successfully placed the ChafKit files in the appropriate directory, you are ready to proceed with adding ChafKit as a dependency in your Unreal Engine project.

Add Public Dependency

To add ChafKit as a public dependency in your Unreal Engine project, follow the steps below:

  1. Open your Unreal Engine project in the Unreal Editor.
  2. Navigate to the "Source" directory of your project.
  3. Locate the .Build.cs file corresponding to your project (e.g. MyGameProject.Build.cs).
  4. Open the .Build.cs file with a text editor.
  5. Inside the .Build.cs file, locate the PublicDependencyModuleNames property.
  6. Add "ChafKit" to the list of module names within the PublicDependencyModuleNames, as shown in the example below:
using UnrealBuildTool;
 
public class MyGameProject : ModuleRules
{
    public MyGameProject(ReadOnlyTargetRules Target) : base(Target)
    {
        PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;
 
        PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "HeadMountedDisplay", "ChafKit" });
    }
}

By adding "ChafKit" to the PublicDependencyModuleNames, you have successfully configured the integration of Chafkit into your Unreal Engine project.

Let's proceed to the next steps to continue the integration process for Unreal Engine.

Checking Plugin Integration

To ensure that the Chafkit plugin is properly added to your Unreal Engine project, follow the steps below:

  1. Restart the Unreal Editor to refresh the project.
  2. Open the Edit menu at the top of the Unreal Editor.
  3. Select Plugins from the dropdown menu.
  4. In the Plugins window, navigate to the Project category on the left sidebar.
  5. Look for the "Chafkit" plugin in the list of plugins. If the plugin is properly installed, you should see it listed under the Other section.

If you can locate the Chafkit plugin under the Other section, it indicates that the plugin has been successfully added to your project.

In case you do not see the Chafkit plugin in the Plugins window, please review the previous steps to ensure that you have correctly placed the Chafkit files in the appropriate directory and added the necessary dependency in your project's .Build.cs file.

After confirming the plugin's presence, you can proceed with using the Chafkit features in your Unreal Engine project.

Opening Project with Visual Studio

  1. Navigate to your game project directory using Windows File Explorer or a similar file manager.

  2. Look for a file with the extension .sln. This file represents your game project's solution file and is typically named after your project, such as [ProjectName].sln.

  3. Double-click on the .sln file to open it with Visual Studio.

Modifying Include Directories

To ensure that your project includes the necessary directories for the Chafkit plugin, follow the steps below:

  1. In the Solution Explorer of your Unreal Engine project, locate the Games/[ProjectName] folder.
  2. Right-click on the [ProjectName] folder and select Properties from the context menu.

Solution Explorer

  1. In the project properties window, locate and click on VC++ Directories in the sidebar.
  2. Within the VC++ Directories settings, locate the Include Directories field.
  3. Click on the field to edit the include directories.

VC++ Directories

  1. Add a new line and enter the directory path: [projectDir]/Plugins/ChafKit/Sources.

Make sure to replace [projectDir] with the actual path to your project directory.

By adding this include directory, your project will have access to the necessary Chafkit headers and source files.

Remember to save the changes made to the project properties.

Let's proceed to the next steps to continue the integration process for Unreal Engine.

Create GameInstance

The GameInstance in Unreal Engine is a persistent object that maintains its presence throughout the entire duration of a game session. Its primary purpose is to serve as a centralized hub for managing and storing game-related data and functionality that needs to persist across levels or game mode transitions.

For integrating ChafKit, it is recommended to initialize it from the GameInstance. This ensures that ChafKit remains accessible and available throughout the entire game session, taking advantage of the GameInstance's persistent nature. However, depending on your game's logic and requirements, you have the flexibility to initialize ChafKit from different codebases such as the GameMode or other custom classes.

Note: If you already have an existing customized GameInstance for your project, you can use that instead of creating a new one.

To create a custom GameInstance in Unreal Engine, follow these steps:

  1. Open the Content Drawer from the left bottom corenr. In the Content Browser panel, navigate to the folder where you want to create your GameInstance. C++ Classes/[ProjectName] is recommended.
  2. Right-click in the Content Browser and select New C++ Class
  3. Select All Classes and search for GameInstance. Select GameInstance from the list and click Next.

  1. In the Unreal Editor, go to the main menu and select "Edit" -> "Project Settings".
  2. In the Project Settings window, navigate to the "Maps & Modes" category on the left sidebar.
  3. Locate the "Game Instance" property and select the newly created GameInstance class from the dropdown menu.

Override Init function of GameInstance

To customize the initialization process of your GameInstance, you can override the Init() function. This allows you to add your own code and perform specific actions when the GameInstance is initialized. Here's how you can do it:

  1. Open your custom GameInstance class from Visual Studio (Double click it from the Content Drawer or locate it directly from Solution Explorer of Visual Studio).
  2. In the same directory of the GameInstance class, there should be header file with the same name as the GameInstance class (e.g. MyGameInstance.h). Open this header file.
  3. Add the Init function declaration.
MyGameInstance.h
#pragma once
 
#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "MyGameInstance.generated.h"
 
UCLASS()
class YOURPROJECTNAME_API UMyGameInstance : public UGameInstance
{
    GENERATED_BODY()
 
public:
    virtual void Init() override;
};
  1. Next, open the .cpp file of your GameInstance class (e.g. MyGameInstance.cpp).
  2. We are gonna check Init function is called after game launch. Implement the Init function as follows:
MyGameInstance.cpp
#include "MyGameInstance.h"
#include "Engine/Engine.h"
 
void UMyGameInstance::Init()
{
    Super::Init();
    GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, TEXT("Hello, World!"));
}

Recompiling Project

To ensure that the changes made to your project, including the integration of the Chafkit plugin, take effect, you need to recompile your Unreal Engine project. Follow the steps below:

  1. In the Unreal Engine Editor, locate the Recompile button.
  2. The Recompile button is typically located at the bottom-right corner of the Unreal Engine Editor.

Recompile Button

  1. Click on the Recompile button to initiate the recompilation process.

The recompilation process will rebuild your project, applying any changes or updates made to the code, dependencies, or plugins. This ensures that your project is up to date and ready to utilize the features provided by Chafkit.

Once the recompilation is complete, you can proceed with utilizing the Chafkit features within your Unreal Engine project.

If recompilation fails, exit the Unreal Engine Editor and restart it. Then, try recompiling your project again.

Check Debug Message

To verify if the debug message from your GameInstance is working correctly, follow these steps:

  1. Make sure you have set your custom GameInstance properly as described earlier in the project settings.

  2. Click the Play button in the Unreal Editor to run the game.

  3. During gameplay, keep an eye on the left top corner of the screen.

  4. If your GameInstance is set up correctly and the Init() function is overridden as mentioned, you should see the "Hello, World!" message displayed as a debug message in the left top corner of the screen.

Call ChafKitSDK::Initialize

If your GameInstance is ready, you can now import ChafKit and call Initialize function. Edit the Init function of the GameInstance as follows:

MyGameInstance.cpp
#include "MyGameInstance.h"
#include "Engine/Engine.h"
#include "ChafKit/Public/ChafKitSDK.h"
 
void UMyGameInstance::Init()
{
    Super::Init();
    auto CallbackHandle = ChafKitSDK::Initialize(TEXT("YOUR-GAME-ID"));
 
    CallbackHandle->BindLambda(
        [](ChafKitCore::ChafKitInitialized result)
        {
            if (result.error == nullptr) {
                GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, TEXT("Successfully Initialized!"));
            }
            else {
                GEngine->AddOnScreenDebugMessage(-1, 30.0f, FColor::Yellow, TEXT("Initialization Failed."));
            }
        }
    );
}

Make sure to replace YOUR-GAME-ID with your actual game ID.

You can access your game ID from the Manage Game page of the Chafgames Developer Plaform.

Recompile your project and run the game. If the initialization is successful, you should see the "Successfully Initialized!" message displayed as a debug message in the left top corner of the screen.