I am coming from an Angular/RxJs perspective and attempting to learn C++. I am getting to the point in my development journey where I need to understand and manage game and player state.
I found a library called RxCpp developed for reactive programming in C++, but the library seems inactive. There is even a wrapper for RxCpp and unreal engine, but that library is also inactive.
Is there anyone using reactive programming in Unreal Engine?
I am beginning to understand how UE handles event-driven design. The code below is probably bloated, but the goal is to create an NgRx-like environment.
Store = Player State
Action = public IncreaseCoinsAction()
Reducer = private IncreaseCoinsReducer()
Anytime a player collides with a coin, it dispatches the increaseCoinAction.
CoinGamePlayerState.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerState.h"
#include "CoinGamePlayerState.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE(FIncreaseCoinsDelegate);
UCLASS()
class COINCOLLECTOR_API ACoinGamePlayerState : public APlayerState
{
GENERATED_BODY()
// Create Coins property
UPROPERTY(EditAnywhere)
int32 Coins;
// Create delegate for increase coins
UPROPERTY(BlueprintAssignable)
FIncreaseCoinsDelegate IncreaseCoinsDelegate;
// Create function for broadcast increase coins
UFUNCTION(BlueprintCallable)
void IncreaseCoinsAction();
// Create begin play function
virtual void BeginPlay() override;
private:
// Create reducer function for increase coins
UFUNCTION()
void IncreaseCoinsReducer();
};
CoinGamePlayerState.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "CoinGamePlayerState.h"
// BeginPlay
void ACoinGamePlayerState::BeginPlay()
{
Super::BeginPlay();
// Set Coins to 0
Coins = 0;
// Bind IncreaseCoins function to delegate add dynamic
IncreaseCoinsDelegate.AddDynamic(this, &ACoinGamePlayerState::IncreaseCoinsReducer);
}
// Create function for broadcast increase coins
void ACoinGamePlayerState::IncreaseCoinsAction()
{
// Broadcast increase coins
IncreaseCoinsDelegate.Broadcast();
}
// Bind increase coins action to increase coins
void ACoinGamePlayerState::IncreaseCoinsReducer()
{
// Increase coins
Coins++;
// Print coins
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("Coins: %d"), Coins));
}
I have experience on RxJS and Angular 12.x too, however, RxCPP looks really awful to me, I believe not many people would use that on local machine gaming logic, unless for some network traffic applications. But C++ has lots of mature network libraries that can outperform everything else, whereas l believe lots of MMOs with heavy network traffic use their own C++ developed low-level protocols.
I think C++ is fast for linear tasks, and things in C++ tend to happen with low-level optimizations. ParallelFor() can do simple multi-threading tasks, and OpenMP can make things better.
I wish other people could write some insights into this since I’m also a newcomer to UE development.
As I’m learning, just had another thought about this.
I coded a fairly complex gaming logic for a casual game.
Then I dropped into the trap of ‘Timeline cannot be inside a loop’.
Took me 1 week to figure out that the UE C++ compiler would do something unknown for the timeline animation inside any loop, it would make multiple timelines just run once and it would be after the loop !
Then I missed the reactive programming.
In the end, I solved this by recursive calling, the UE wouldn’t optimize that.