I have previously successfully made a RPC method with validation in my C++ template project which you can see just below. The Unreal Tools will make a class.generated.h file with the RPC validation implementation.
Now I want to make a new method ‘OnInteract’ which is pretty much the same but more simplified (just to play a sound). The project will compile successfully and I can register the ‘Interact’ event in blueprint/in-game. The sound doesn’t play however and I believe this may have to have to do with the class.generated.h file not having the rcp implementation? Maybe I should attack this problem from a different angle but I’m still curious why the class.generated.h file doesn’t contain references to my new RPC method.
First is the working RPC method (OnFire, ServerFire_Validate, ServerFire_Implementation).
Second you will see my new RPC method (OnInteract, ServerInteract_Validate, ServerInteract_Implementation).
AMyProjectCharacter.h
*/** Fires a projectile. */
void OnFire();
UFUNCTION(reliable, server, WithValidation)
void ServerFire();*
AMyProjectCharacter.cpp
void AMyProjectCharacter::OnFire()
{
ServerFire();
}
bool AMyProjectCharacter::ServerFire_Validate()
{
return true;
}
void AMyProjectCharacter::ServerFire_Implementation()
{
// try and fire a projectile
if (ProjectileClass != NULL)
{
const FRotator SpawnRotation = GetControlRotation();
// MuzzleOffset is in camera space, so transform it to world space before offsetting from the character location to find the final muzzle position
const FVector SpawnLocation = GetActorLocation() + SpawnRotation.RotateVector(GunOffset);
UWorld* const World = GetWorld();
if (World != NULL)
{
// spawn the projectile at the muzzle
World->SpawnActor<AMyProjectProjectile>(ProjectileClass, SpawnLocation, SpawnRotation);
}
}
// try and play the sound if specified
if (FireSound != NULL)
{
UGameplayStatics::PlaySoundAtLocation(this, FireSound, GetActorLocation());
}
// try and play a firing animation if specified
if(FireAnimation != NULL)
{
// Get the animation object for the arms mesh
UAnimInstance* AnimInstance = Mesh1P->GetAnimInstance();
if(AnimInstance != NULL)
{
AnimInstance->Montage_Play(FireAnimation, 1.f);
}
}
}
The Unreal Tools then generates a MyProjectCharacter.generated.h file with the following:
extern MYPROJECT_API FName MYPROJECT_ServerFire;
#define AMYPROJECTCharacter_EVENTPARMS
#define AMYPROJECTCharacter_RPC_WRAPPERS \
bool ServerFire_Validate(); \
virtual void ServerFire_Implementation(); \
\
DECLARE_FUNCTION(execServerFire) \
{ \
P_FINISH; \
if (!this->ServerFire_Validate()) \
{ \
RPC_ValidateFailed(TEXT("ServerFire_Validate")); \
return; \
} \
this->ServerFire_Implementation(); \
}
Having succeeded at this I wanted to create a new function. AMyProjectCharacter.h:
/** Triggers an action */
void OnInteract();
UFUNCTION(reliable, server, WithValidation)
void ServerInteract();
AMyProjectCharacter.cpp
void AMYPROJECTCharacter::OnInteract()
{
ServerInteract();
}
bool AMYPROJECTCharacter::ServerInteract_Validate()
{
return true;
}
void AMYPROJECTCharacter::ServerInteract_Implementation()
{
if (InteractSound != NULL)
{
UGameplayStatics::PlaySoundAtLocation(this, InteractSound, GetActorLocation());
}
}
For some reason this doesn’t create an entry in the MyProject.generated.h file. I have tried re-compiling and cleaning the project but no luck. I simply wanted to play a sound with this function.