Skip to content
Snippets Groups Projects
Select Git revision
  • c7f73d16e03477d53bc7d8bb0a2c972df28be87c
  • 5.4 default protected
  • 5.5
  • dev/5.5
  • dev/5.4
  • dev/5.3_downgrade
  • feature/experimenttime_hack
  • 5.3 protected
  • _IntenSelect5.3
  • IntenSelect5.3
  • 4.27 protected
  • 4.26 protected
  • 5.0 protected
  • 4.22 protected
  • 4.21 protected
  • UE5.4-2024.1
  • UE5.4-2024.1-rc1
  • UE5.3-2023.1-rc3
  • UE5.3-2023.1-rc2
  • UE5.3-2023.1-rc
20 results

DisplayClusterPawnCAVE.cpp

Blame
  • ReplicatedMotionControllerComponent.cpp 2.79 KiB
    // Fill out your copyright notice in the Description page of Project Settings.
    
    
    #include "Pawn/ReplicatedMotionControllerComponent.h"
    
    #include "Net/UnrealNetwork.h"
    
    UReplicatedMotionControllerComponent::UReplicatedMotionControllerComponent()
    {
    	PrimaryComponentTick.bCanEverTick = true;
    	PrimaryComponentTick.SetTickFunctionEnable(true);
    	SetIsReplicatedByDefault(true);
    
    	// Direct transform replication
    	ControllerNetUpdateRate = 100.0f; // 100 htz is default
    	ControllerNetUpdateCount = 0.0f;
    }
    
    // Naive direct transform replication (replace with input rep?)
    
    void UReplicatedMotionControllerComponent::UpdateState(float DeltaTime)
    {
    	if (GetOwner()->HasLocalNetOwner())
    	{
    		if (GetIsReplicated())
    		{
    			const FVector Loc = GetRelativeLocation();
    			const FRotator Rot = GetRelativeRotation();
    
    			if (!Loc.Equals(ReplicatedTransform.Position) || !Rot.Equals(ReplicatedTransform.Rotation))
    			{
    				ControllerNetUpdateCount += DeltaTime;
    				if (ControllerNetUpdateCount >= (1.0f / ControllerNetUpdateRate)) // todo save inverse?
    				{
    					ControllerNetUpdateCount = 0.0f;
    
    					ReplicatedTransform.Position = Loc;
    					ReplicatedTransform.Rotation = Rot;
    					if (GetNetMode() == NM_Client) // why do we differentiate here between netmode and authority?
    					{
    						SendControllerTransform_ServerRpc(ReplicatedTransform);
    					}
    				}
    			}
    		}
    	}
    }
    
    void UReplicatedMotionControllerComponent::TickComponent(float DeltaTime, ELevelTick TickType,
                                                             FActorComponentTickFunction* ThisTickFunction)
    {
    	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
    	UpdateState(DeltaTime);
    }
    
    void UReplicatedMotionControllerComponent::GetLifetimeReplicatedProps(
    	TArray<class FLifetimeProperty>& OutLifetimeProps) const
    {
    	Super::GetLifetimeReplicatedProps(OutLifetimeProps);
    
    	DISABLE_REPLICATED_PRIVATE_PROPERTY(USceneComponent, RelativeLocation);
    	DISABLE_REPLICATED_PRIVATE_PROPERTY(USceneComponent, RelativeRotation);
    	DISABLE_REPLICATED_PRIVATE_PROPERTY(USceneComponent, RelativeScale3D);
    
    	// Skipping the owner with this as the owner will use the controllers location directly
    	DOREPLIFETIME_CONDITION(UReplicatedMotionControllerComponent, ReplicatedTransform, COND_SkipOwner);
    	DOREPLIFETIME(UReplicatedMotionControllerComponent, ControllerNetUpdateRate);
    }
    
    void UReplicatedMotionControllerComponent::SendControllerTransform_ServerRpc_Implementation(
    	FVRTransformRep NewTransform)
    {
    	// Store new transform and trigger OnRep_Function
    	ReplicatedTransform = NewTransform;
    
    	if (!GetOwner()->HasLocalNetOwner())
    		OnRep_ReplicatedTransform();
    }
    
    bool UReplicatedMotionControllerComponent::SendControllerTransform_ServerRpc_Validate(FVRTransformRep NewTransform)
    {
    	return true;
    	// Optionally check to make sure that player is inside of their bounds and deny it if they aren't?
    }