# Intercepting and Handling Messages from NPCForge Plugin

### This guide provides steps to set up message interception from the NPCForge plugin to enable custom handling, such as displaying messages in the game console or on-screen.

#### **Prerequisites**

1. Unreal Engine project with NPCForge plugin integrated.
2. Basic knowledge of C++ and Unreal Engine's subsystem management.

#### **Step 1: Add NPCForge Dependency**

To access NPCForge's functionalities, add it as a dependency in your project's `Build.cs` file.

1. Open `YourProject.Build.cs`.
2. Add `"NPCForge"` to the list in `PublicDependencyModuleNames` as shown below:

   ```csharp
   PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore", "EnhancedInput", "NPCForge" });
   ```
3. Save and rebuild your project to ensure NPCForge is properly included.

#### **Step 2: Include the Message Manager in Your Class**

In the class responsible for handling messages, include `MessageManager.h`:

```cpp
#include "MessageManager.h"
```

This gives your class access to the `UMessageManager` subsystem, which will be used to intercept and handle incoming messages.

#### **Step 3: Setting Up Message Interception**

To listen for messages from NPCForge, connect to the `NewMessageReceivedEvent` event from the `UMessageManager`. This event triggers whenever a new message arrives. Here’s how to set it up:

1. Check if the `UMessageManager` subsystem exists in the game world.
2. Bind a custom handler function (e.g., `OnMessageReceived`) to `NewMessageReceivedEvent`.

Here’s a code snippet for setting up the event binding:

```cpp
if (UMessageManager* MessageManager = GetWorld()->GetSubsystem<UMessageManager>())
{
    MessageManager->NewMessageReceivedEvent.AddDynamic(this, &AMessageDisplayActor::OnMessageReceived);
}
```

In this example:

* `AMessageDisplayActor` is the class that will handle the intercepted messages.
* `OnMessageReceived` is a custom function that processes each incoming message.

#### **Step 4: Implement the Message Handler**

Define the handler function `OnMessageReceived` in your actor or class. The function receives an `FMessage` parameter, which contains the message details such as sender, receiver, and content.

Example implementation:

```cpp
AMessageDisplayActor::OnMessageReceived(FMessage Message)
{
    // Display the message in the game's console
    UE_LOG(LogTemp, Display, TEXT("FROM GAME: Intercepted Message: Sender=%s, Receiver=%s, Content=%s"),
           *Message.SenderID, *Message.ReceiverID, *Message.Content);
}
```

**Explanation:**

* **`SenderID`**: Identifies the message sender.
* **`ReceiverID`**: Identifies the intended receiver of the message.
* **`Content`**: Contains the actual content of the message.

This function logs the message details to the console using `UE_LOG`, but you can modify it to display the message on-screen or trigger other in-game actions.
