| @ -0,0 +1,74 @@ | ||||
| #include "CSReplicatedObject.h" | ||||
| #include "Engine/BlueprintGeneratedClass.h" | ||||
| #include "Runtime/Launch/Resources/Version.h" | ||||
| #include "UObject/Package.h" | ||||
| #include "Engine/NetDriver.h" | ||||
| #include "Engine/Engine.h" | ||||
|  | ||||
| UWorld* UCSReplicatedObject::GetWorld() const | ||||
| { | ||||
| 	if (GetOuter() == nullptr) | ||||
| 	{ | ||||
| 		return nullptr; | ||||
| 	} | ||||
| 		 | ||||
| 	if (Cast<UPackage>(GetOuter()) != nullptr) | ||||
| 	{ | ||||
| 		return Cast<UWorld>(GetOuter()->GetOuter()); | ||||
| 	} | ||||
| 		 | ||||
| 	return GetOwningActor()->GetWorld(); | ||||
| } | ||||
|  | ||||
| void UCSReplicatedObject::GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const | ||||
| { | ||||
| 	Super::GetLifetimeReplicatedProps(OutLifetimeProps); | ||||
| 	 | ||||
| 	if (UBlueprintGeneratedClass* BPCClass = Cast<UBlueprintGeneratedClass>(GetClass())) | ||||
| 	{ | ||||
| 		BPCClass->GetLifetimeBlueprintReplicationList(OutLifetimeProps); | ||||
| 	} | ||||
| } | ||||
|  | ||||
| bool UCSReplicatedObject::IsSupportedForNetworking() const | ||||
| { | ||||
| 	return ReplicationState == ECSReplicationState::Replicates; | ||||
| } | ||||
|  | ||||
| int32 UCSReplicatedObject::GetFunctionCallspace(UFunction* Function, FFrame* Stack) | ||||
| { | ||||
| 	if (HasAnyFlags(RF_ClassDefaultObject) || !IsSupportedForNetworking()) | ||||
| 	{ | ||||
| 		return GEngine->GetGlobalFunctionCallspace(Function, this, Stack); | ||||
| 	} | ||||
| 	 | ||||
| 	return GetOuter()->GetFunctionCallspace(Function, Stack); | ||||
| } | ||||
|  | ||||
| bool UCSReplicatedObject::CallRemoteFunction(UFunction* Function, void* Parms, FOutParmRec* OutParms, FFrame* Stack) | ||||
| { | ||||
| 	AActor* Owner = GetOwningActor(); | ||||
| 	UNetDriver* NetDriver = Owner->GetNetDriver(); | ||||
| 	if (!IsValid(NetDriver)) | ||||
| 	{ | ||||
| 		return false; | ||||
| 	} | ||||
| 	 | ||||
| 	NetDriver->ProcessRemoteFunction(Owner, Function, Parms, OutParms, Stack, this); | ||||
| 	return true; | ||||
| } | ||||
|  | ||||
| AActor* UCSReplicatedObject::GetOwningActor() const | ||||
| { | ||||
| 	return GetTypedOuter<AActor>(); | ||||
| } | ||||
|  | ||||
| void UCSReplicatedObject::DestroyObject() | ||||
| { | ||||
| 	if (!IsValid(this)) | ||||
| 	{ | ||||
| 		return; | ||||
| 	} | ||||
|  | ||||
| 	MarkAsGarbage(); | ||||
| } | ||||
| @ -0,0 +1,48 @@ | ||||
| #pragma once | ||||
|  | ||||
| #include "CoreMinimal.h" | ||||
| #include "UObject/Object.h" | ||||
| #include "CSReplicatedObject.generated.h" | ||||
|  | ||||
| UENUM(BlueprintType) | ||||
| enum ECSReplicationState | ||||
| { | ||||
| 	// This UObject is considered for replication. | ||||
| 	Replicates, | ||||
| 	// This UObject is not considered for replication. | ||||
| 	DoNotReplicate, | ||||
| }; | ||||
|  | ||||
| /**  | ||||
|  * This class provides support for replicated UObjects in C# | ||||
|  */ | ||||
| UCLASS(Blueprintable, BlueprintType, DisplayName = "Replicated UObject", Abstract) | ||||
| class UCSReplicatedObject : public UObject | ||||
| { | ||||
| 	GENERATED_BODY() | ||||
|  | ||||
| public: | ||||
|  | ||||
| 	// UObject interface implementation | ||||
| 	virtual UWorld* GetWorld() const override; | ||||
| 	virtual void GetLifetimeReplicatedProps(TArray<FLifetimeProperty>& OutLifetimeProps) const override; | ||||
| 	virtual bool IsSupportedForNetworking() const override; | ||||
| 	virtual int32 GetFunctionCallspace(UFunction* Function, FFrame* Stack) override; | ||||
| 	virtual bool CallRemoteFunction(UFunction* Function, void* Parms, struct FOutParmRec* OutParms, FFrame* Stack) override; | ||||
| 	// End of implementation | ||||
|  | ||||
| 	// Will mark this UObject as garbage and will eventually get cleaned by the garbage collector. | ||||
| 	// Should only execute this on the server. | ||||
| 	UFUNCTION(meta = (ScriptMethod)) | ||||
| 	void DestroyObject(); | ||||
|  | ||||
| 	// Gets the Actor that "owns" this Replicated UObject. | ||||
| 	UFUNCTION(meta = (ScriptMethod)) | ||||
| 	AActor* GetOwningActor() const; | ||||
|  | ||||
| public: | ||||
|  | ||||
| 	// Is this UObject replicated? | ||||
| 	UPROPERTY(EditAnywhere) | ||||
| 	TEnumAsByte<ECSReplicationState> ReplicationState = ECSReplicationState::Replicates; | ||||
| }; | ||||
		Reference in New Issue
	
	Block a user