Download

Sending USTRUCT via RPC in AActor component - values not carried over

This is the struct that I’m sending in my actor component.



 
#pragma once
 
#include "PlayerInputState.generated.h"
 
USTRUCT()
struct FPlayerInputState
{
        GENERATED_USTRUCT_BODY()
 
        FPlayerInputState()
                : xAxis(0.0f), yAxis(0.0f), zAxis(0.0f), pitch(0.0f), roll(0.0f), yaw(0.0f), timestamp(0.0f) {}
 
        FPlayerInputState(float a_xAxis, float a_yAxis, float a_zAxis, float a_pitch, float a_roll, float a_yaw, float a_timestamp)
                : xAxis(a_xAxis), yAxis(a_yAxis), zAxis(a_zAxis), pitch(a_pitch), roll(a_roll), yaw(a_yaw), timestamp(a_timestamp) {}
       
        UPROPERTY(Replicated)
        float xAxis;
        UPROPERTY(Replicated)
        float yAxis;
        UPROPERTY(Replicated)
        float zAxis;
 
        UPROPERTY(Replicated)
        float pitch;
        UPROPERTY(Replicated)
        float roll;
        UPROPERTY(Replicated)
        float yaw;
 
        UPROPERTY(Replicated)
        float timestamp;
};


This is the actual function setup.



// HEADER
UFUNCTION(Server, WithValidation, Unreliable)
void ServerSendInput(FPlayerInputState a_inputState);

// CPP
void UNetworkedMovementComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

        // Testing shows LocalPlayerInput (an FPlayerInputState struct member of this component) has non-zero values
	if (GetOwner()->Role < ROLE_Authority)
	{
		ServerSendInput(LocalPlayerInput);
	}
}

void UNetworkedMovementComponent::ServerSendInput_Implementation(FPlayerInputState a_inputState)
{
	// in here, a_inputState's variables/members are zero, no matter what is originally sent.
}

bool UNetworkedMovementComponent::ServerSendInput_Validate(FPlayerInputState a_inputState)
{
	return true;
}



Anyone have any ideas why the struct’s member variables aren’t coming across in the RPC? I also tried with const FPlayerInputState& arguments, but same result.

I don’t know what it changes, but I’m not sure if you need to make all of the variables in the USTRUCT to be Replicated. I think you just need to list them as UPROPERTY() for them to be sent.

Also, make sure to check the variables on your client before sending, if they’re not modified they’ll be zero too.

Yeah, tried without the ‘Replicated’ in them. And yepp, they’re definitely non-zero before sending.

Tried it again without ‘Replicated’ and it works. Who knows!

Probably something related to the serialisation of replicated variables? Either way, good to hear that it worked, haha.