Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

I'm trying to replicate GameInstance subSystem, Before i tried it i achived that for…

  1. 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);
    

    }

  2. 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 GameInstance
    

    indicates that Unreal Engine is expecting the UScenarioSubsystem to be owned by the GameInstance, but your code is attempting to set its outer to an AActor (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

    1. 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 UWorld and their replication might differ, which explains why you were able to replicate a WorldSubsystem but not a GameInstance one.
    2. Replication Mechanics:

      • Replicated Properties: Using TWeakObjectPtr for replicated properties can be problematic because weak pointers are non-owning and may become nullptr, leading to replication issues.
      • AddReplicatedSubObject: This function expects objects to maintain their intended ownership hierarchy. Changing the outer of a subsystem breaks this expectation.

    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 ReplicateSubSystem Function:

    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_ScenarioSystemHandler actor to replicate relevant data from the subsystem. This respects the subsystem's ownership while ensuring necessary data is synchronized across the network.

    Example:

    1. 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
      }
      
    2. 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_ScenarioSystemHandler actor now holds a replicated variable ReplicatedScenarioValue that mirrors a value from the UScenarioSubsystem.
        • 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.

    3. Create a Dedicated Replicated UObject (Alternative Approach)

    If you need more complex replication logic, consider creating a separate UObject specifically for replication purposes, rather than using a subsystem directly.

    Example:

    1. 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);
      }
      
    2. 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 UScenarioReplicatedObject handles the replication of data.
        • The ASV_ScenarioSystemHandler actor manages this object, ensuring it stays in sync with the UScenarioSubsystem.

    Key Takeaways

    1. 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.

    2. 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.

    3. Separate Concerns: Consider creating dedicated UObject classes for replication if the data is complex or requires specialized handling, ensuring that subsystem logic and replication logic remain decoupled.

    4. Replication Best Practices:

      • Prefer using TObjectPtr or raw pointers for replicated objects where appropriate.
      • Ensure that any replicated UObject is properly initialized and has replication enabled.
      • Use ReplicatedUsing to handle client-side updates when replicated properties change.

    By adhering to these practices, you can effectively replicate the necessary data from your subsystems without encountering ownership-related errors.