Communication System Software Design - wwestlake/Steamforge GitHub Wiki

Communication System Software Design

Overview

This document outlines the software design for the Communication System intended for use within the Steamforge project. It describes the approach, key components, interactions, and their integration into the Unreal Engine Blueprint workflow.


Goals

  • Enable actors to communicate seamlessly.
  • Ensure communication capabilities are Blueprint accessible.
  • Support both intra-map (local) and inter-map (cross-level/global) messaging.
  • Leverage Unreal Engine's component-based and subsystem architecture.

Architecture

Primary Components

  • UCommunicationComponent: Actor Component that actors attach to participate in communication.
  • UCommunicationCenter: Singleton Subsystem (UGameInstanceSubsystem) managing message dispatch and listener registrations.

UCommunicationComponent

Each actor uses a UCommunicationComponent to send and receive messages.

Responsibilities

  • Register and unregister with UCommunicationCenter on lifecycle events.
  • Expose Blueprint callable functions for sending messages.
  • Implement Blueprint events to handle incoming messages.

Component Definition

UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class STEAMFORGE_API UCommunicationComponent : public UActorComponent
{
    GENERATED_BODY()

public:
    UCommunicationComponent();

protected:
    virtual void BeginPlay() override;
    virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;

public:
    UFUNCTION(BlueprintCallable, Category="Communication")
    void SendMessage(FName Channel, const FString& MessageType, const TMap<FString, FString>& Payload);

    UFUNCTION(BlueprintImplementableEvent, Category="Communication")
    void OnMessageReceived(FName Channel, const FString& MessageType, const TMap<FString, FString>& Payload);

    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Communication")
    TArray<FName> ListeningChannels;
};

UCommunicationCenter

A centralized subsystem managing global communication across actors and maps.

Responsibilities

  • Manage listener registrations for channels.
  • Dispatch messages to registered listeners.
  • Maintain persistence across level transitions for inter-map communication.

Subsystem Definition

UCLASS()
class STEAMFORGE_API UCommunicationCenter : public UGameInstanceSubsystem
{
    GENERATED_BODY()

private:
    TMap<FName, TSet<TWeakObjectPtr<UCommunicationComponent>>> ChannelListeners;

public:
    static UCommunicationCenter* Get(const UObject* WorldContext);

    void RegisterListener(FName Channel, UCommunicationComponent* Listener);
    void UnregisterListener(FName Channel, UCommunicationComponent* Listener);

    void DispatchMessage(FName Channel, const FString& MessageType, const TMap<FString, FString>& Payload);
};

Workflow and Interactions

Component Lifecycle

  • BeginPlay: Component registers itself as a listener for configured channels.
  • EndPlay: Component unregisters itself to prevent invalid references.

Message Sending

Actors send messages through their attached UCommunicationComponent:

void UCommunicationComponent::SendMessage(FName Channel, const FString& MessageType, const TMap<FString, FString>& Payload)
{
    UCommunicationCenter* CommCenter = UCommunicationCenter::Get(this);
    if (CommCenter)
    {
        CommCenter->DispatchMessage(Channel, MessageType, Payload);
    }
}

Message Receiving

Actors receive messages via Blueprint-implemented events:

// Blueprint Event
void OnMessageReceived(FName Channel, const FString& MessageType, const TMap<FString, FString>& Payload);

Blueprint Integration

  • Components are fully configurable via the Blueprint details panel.
  • Sending messages uses Blueprint-callable methods (SendMessage).
  • Receiving messages utilizes Blueprint implementable events (OnMessageReceived).

Communication Scope

Intra-Map Communication

  • Communication between actors within the same map is managed directly through local subsystem registrations.

Inter-Map Communication

  • The UCommunicationCenter subsystem persists across level transitions using UGameInstanceSubsystem, inherently supporting cross-level communications.

Performance and Scalability

  • Using TWeakObjectPtr prevents unintended object lifetimes and memory overhead.
  • Lightweight message dispatch ensures minimal performance impact.

Example Scenario

Sending from Blueprint:

  • Use SendMessage node, specify Channel, MessageType, and Payload.

Receiving in Blueprint:

  • Implement OnMessageReceived event graph node to handle received messages.

Next Steps

  1. Implement and test the UCommunicationComponent and UCommunicationCenter.
  2. Validate intra-map and inter-map messaging in test environments.
  3. Integrate and refine based on practical game scenarios.

This document outlines a clear software design ready for immediate implementation within the Steamforge game project.

⚠️ **GitHub.com Fallback** ⚠️