I'm trying to replicate GameInstance subSystem, Before i tried it i achived that for…
- User
I'm trying to replicate GameInstance subSystem, Before i tried it i achived that for worldSubsystem. But i can't achive that with this error: [2024.10.10-13.33.24:340][429]LogWindows: Error: Object ScenarioSubsystem None created in SV_ScenarioSystemHandler instead of GameInstance Code: UCLASS() class ASSYSTEMRUNTIME_API ASV_ScenarioSystemHandler : public AActor { GENERATED_BODY()
public: // Sets default values for this actor's properties ASV_ScenarioSystemHandler();
protected: // Called when the game starts or when spawned virtual void BeginPlay() override;
void ReplicateSubSystem(); void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override;public:
UPROPERTY(Replicated) TWeakObjectPtr<UScenarioSubsystem> SubSystem;}; // Sets default values ASV_ScenarioSystemHandler::ASV_ScenarioSystemHandler() { // Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = false; bAlwaysRelevant = true; bReplicates = true; bReplicateUsingRegisteredSubObjectList = true; }
// Called when the game starts or when spawned void ASV_ScenarioSystemHandler::BeginPlay() { Super::BeginPlay(); ReplicateSubSystem(); }
void ASV_ScenarioSystemHandler::ReplicateSubSystem() { if (!HasAuthority()) return;
SubSystem = GetGameInstance()->GetSubsystem<UScenarioSubsystem>(); AddReplicatedSubObject(SubSystem.Get()); SubSystem->SetOuter(this);}
void ASV_ScenarioSystemHandler::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(ASV_ScenarioSystemHandler, SubSystem);} SubSystem replication part:
virtual bool CallRemoteFunction(UFunction* Function, void* Parameters, struct FOutParmRec* OutParams, FFrame* Stack) override; virtual int32 GetFunctionCallspace(UFunction* Function, FFrame* Stack) override; virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override; virtual bool IsSupportedForNetworking() const override { return true; }private: UPROPERTY(ReplicatedUsing = ONRep) TObjectPtr OuterActor; public: void SetOuter(AActor* Outer); UFUNCTION() void ONRep();
void UScenarioSubsystem::SetOuter(AActor* Outer) { this->OuterActor = Outer;
}
void UScenarioSubsystem::ONRep() { //todo return; }
bool UScenarioSubsystem::CallRemoteFunction(UFunction* Function, void* Parameters, FOutParmRec* OutParams, FFrame* Stack) { if (OuterActor) { if (UNetDriver* NetDriver = OuterActor->GetNetDriver()) { NetDriver->ProcessRemoteFunction(OuterActor, Function, Parameters, OutParams, Stack); return true; } } return false; }
int32 UScenarioSubsystem::GetFunctionCallspace(UFunction* Function, FFrame* Stack) { if (OuterActor) { return OuterActor->GetFunctionCallspace(Function, Stack); } return FunctionCallspace::Local; }
void UScenarioSubsystem::GetLifetimeReplicatedProps(TArray& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps);
DOREPLIFETIME(UScenarioSubsystem, OuterActor);}
- Assistant
The error you're encountering:
[2024.10.10-13.33.24:340][429]LogWindows: Error: Object ScenarioSubsystem None created in SV_ScenarioSystemHandler instead of GameInstanceindicates that Unreal Engine is expecting the
UScenarioSubsystemto be owned by theGameInstance, but your code is attempting to set its outer to anAActor(ASV_ScenarioSystemHandler). Subsystems in Unreal Engine have specific ownership requirements, and modifying their outer can lead to unexpected behavior and errors like the one you're seeing.Understanding the Issue
-
Subsystem Ownership:
- GameInstance Subsystems: These are designed to be globally accessible and are owned by the
GameInstance. Altering their ownership disrupts the engine's internal handling. - World Subsystems: These are tied to the
UWorldand their replication might differ, which explains why you were able to replicate aWorldSubsystembut not aGameInstanceone.
- GameInstance Subsystems: These are designed to be globally accessible and are owned by the
-
Replication Mechanics:
- Replicated Properties: Using
TWeakObjectPtrfor replicated properties can be problematic because weak pointers are non-owning and may becomenullptr, leading to replication issues. - AddReplicatedSubObject: This function expects objects to maintain their intended ownership hierarchy. Changing the outer of a subsystem breaks this expectation.
- Replicated Properties: Using
Recommended Solutions
1. Avoid Changing the Subsystem's Outer
Instead of trying to set the outer of the subsystem to an actor, maintain its ownership with the
GameInstance. This ensures that Unreal Engine's internal systems handle it correctly.Modified
ReplicateSubSystemFunction:void ASV_ScenarioSystemHandler::ReplicateSubSystem() { if (!HasAuthority()) return; SubSystem = GetGameInstance()->GetSubsystem<UScenarioSubsystem>(); if (SubSystem.IsValid()) { // No need to change the outer AddReplicatedSubObject(SubSystem.Get()); } }2. Use the Actor as a Mediator for Replication
Instead of making the subsystem itself replicated, use your
ASV_ScenarioSystemHandleractor to replicate relevant data from the subsystem. This respects the subsystem's ownership while ensuring necessary data is synchronized across the network.Example:
-
Expose Relevant Data in the Subsystem:
// UScenarioSubsystem.h UCLASS() class ASSYSTEMRUNTIME_API UScenarioSubsystem : public UGameInstanceSubsystem { GENERATED_BODY() public: // Example data you want to replicate UPROPERTY(BlueprintReadOnly, Category = "Scenario") int32 ScenarioValue; // Function to update ScenarioValue void UpdateScenarioValue(int32 NewValue); };// UScenarioSubsystem.cpp void UScenarioSubsystem::UpdateScenarioValue(int32 NewValue) { ScenarioValue = NewValue; // Notify the handler actor if needed } -
Replicate Data via the Handler Actor:
// ASV_ScenarioSystemHandler.h UCLASS() class ASSYSTEMRUNTIME_API ASV_ScenarioSystemHandler : public AActor { GENERATED_BODY() public: ASV_ScenarioSystemHandler(); protected: virtual void BeginPlay() override; void ReplicateSubSystem(); virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override; public: UPROPERTY(ReplicatedUsing = OnRep_ScenarioValue) int32 ReplicatedScenarioValue; UFUNCTION() void OnRep_ScenarioValue(); UFUNCTION(Server, Reliable, WithValidation) void ServerUpdateScenarioValue(int32 NewValue); };// ASV_ScenarioSystemHandler.cpp void ASV_ScenarioSystemHandler::BeginPlay() { Super::BeginPlay(); ReplicateSubSystem(); } void ASV_ScenarioSystemHandler::ReplicateSubSystem() { if (!HasAuthority()) return; UScenarioSubsystem* Subsystem = GetGameInstance()->GetSubsystem<UScenarioSubsystem>(); if (Subsystem) { ReplicatedScenarioValue = Subsystem->ScenarioValue; } } void ASV_ScenarioSystemHandler::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps); DOREPLIFETIME(ASV_ScenarioSystemHandler, ReplicatedScenarioValue); } void ASV_ScenarioSystemHandler::OnRep_ScenarioValue() { // Handle the replicated value on clients } bool ASV_ScenarioSystemHandler::ServerUpdateScenarioValue_Validate(int32 NewValue) { return true; } void ASV_ScenarioSystemHandler::ServerUpdateScenarioValue_Implementation(int32 NewValue) { UScenarioSubsystem* Subsystem = GetGameInstance()->GetSubsystem<UScenarioSubsystem>(); if (Subsystem) { Subsystem->UpdateScenarioValue(NewValue); ReplicatedScenarioValue = NewValue; } }- Explanation:
- The
ASV_ScenarioSystemHandleractor now holds a replicated variableReplicatedScenarioValuethat mirrors a value from theUScenarioSubsystem. - When the subsystem updates its value, the actor can update the replicated variable accordingly.
- Clients receive updates through
OnRep_ScenarioValue, where you can handle the new value as needed.
- The
- Explanation:
3. Create a Dedicated Replicated UObject (Alternative Approach)
If you need more complex replication logic, consider creating a separate
UObjectspecifically for replication purposes, rather than using a subsystem directly.Example:
-
Create a Replicated UObject:
// UScenarioReplicatedObject.h UCLASS() class ASSYSTEMRUNTIME_API UScenarioReplicatedObject : public UObject { GENERATED_BODY() public: UPROPERTY(ReplicatedUsing=OnRep_ScenarioData) FString ScenarioData; UFUNCTION() void OnRep_ScenarioData(); virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override; };// UScenarioReplicatedObject.cpp void UScenarioReplicatedObject::OnRep_ScenarioData() { // Handle replicated data } void UScenarioReplicatedObject::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps); DOREPLIFETIME(UScenarioReplicatedObject, ScenarioData); } -
Manage the Replicated UObject via the Actor:
// ASV_ScenarioSystemHandler.h UCLASS() class ASSYSTEMRUNTIME_API ASV_ScenarioSystemHandler : public AActor { GENERATED_BODY() public: ASV_ScenarioSystemHandler(); protected: virtual void BeginPlay() override; virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override; public: UPROPERTY(VisibleAnywhere, Replicated) UScenarioReplicatedObject* ReplicatedObject; };// ASV_ScenarioSystemHandler.cpp ASV_ScenarioSystemHandler::ASV_ScenarioSystemHandler() { bReplicates = true; // Initialize the replicated object ReplicatedObject = CreateDefaultSubobject<UScenarioReplicatedObject>(TEXT("ReplicatedObject")); if (ReplicatedObject) { ReplicatedObject->SetIsReplicated(true); } } void ASV_ScenarioSystemHandler::BeginPlay() { Super::BeginPlay(); if (HasAuthority() && ReplicatedObject) { // Initialize or update the replicated object based on the subsystem UScenarioSubsystem* Subsystem = GetGameInstance()->GetSubsystem<UScenarioSubsystem>(); if (Subsystem) { ReplicatedObject->ScenarioData = "Initial Data"; // Example } } } void ASV_ScenarioSystemHandler::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const { Super::GetLifetimeReplicatedProps(OutLifetimeProps); DOREPLIFETIME(ASV_ScenarioSystemHandler, ReplicatedObject); }- Explanation:
- A separate
UScenarioReplicatedObjecthandles the replication of data. - The
ASV_ScenarioSystemHandleractor manages this object, ensuring it stays in sync with theUScenarioSubsystem.
- A separate
- Explanation:
Key Takeaways
-
Maintain Proper Ownership: Subsystems are designed to be owned by specific engine classes (
GameInstance,UWorld, etc.). Altering their ownership can lead to engine errors and undefined behavior. -
Use Actors as Mediators: To replicate data from subsystems, use actors to hold replicated properties that reflect the subsystem's state, rather than trying to replicate the subsystem itself.
-
Separate Concerns: Consider creating dedicated
UObjectclasses for replication if the data is complex or requires specialized handling, ensuring that subsystem logic and replication logic remain decoupled. -
Replication Best Practices:
- Prefer using
TObjectPtror raw pointers for replicated objects where appropriate. - Ensure that any replicated
UObjectis properly initialized and has replication enabled. - Use
ReplicatedUsingto handle client-side updates when replicated properties change.
- Prefer using
By adhering to these practices, you can effectively replicate the necessary data from your subsystems without encountering ownership-related errors.
-