And then I try to call a Server function when something happens in the Tick() method. The server function only prints something in the log, and looks like this:
// In the .h:
UFUNCTION(Server, Reliable, WithValidation)
void ServerDoSomething();
// In the .cpp
void AMyActor::ServerDoSomething_Implementation()
{
UE_LOG(LogTemp, Warning, TEXT("I made it"));
}
bool AMyActor::ServerDoSomething_Validate()
{
return true;
}
By setting the owner in the BeginPlay() the error about “owning connections” goes away, but the actual method is NEVER called in the server. It just does nothing. So, how can I call a server function from an Actor placed in a level?
Unless the server also has your player set as owner, it will ignore the call.
Do you need to be able to use this from multiple players? Then I don’t think there’s a way other than adding a proxy function to the player controller (with a ptr to the actor) and have that call the actual function.
If you do go that way, make sure the actor is actually valid on the server side, otherwise malicious client could crash the server by sending a nullptr
You will need a start and end function and your server functions you made.
Example code, but it should work
/*
MoveYourPiece() CLIENT MOVES YOUR PIECE Start functon from button or what ever trigers it
*/
void AYourClass::MoveYourPiece()
{
ENetMode NetMode = GetNetMode();
SetThePiece();
if (NetMode == NM_Client)
{
ServerDoSomething();
}
}
// In the .cpp
void AMyActor::ServerDoSomething_Implementation()
{
UE_LOG(LogTemp, Warning, TEXT("I made it"));
SetThePiece();
}
bool AMyActor::ServerDoSomething_Validate()
{
return true;
}
/*
SetThePiece(); end function to actually move the piece
*/
void AYourClass::SetThePiece()
{
bMovePiece = !bMovePiece;//you will want to replicate this to everyone but yourself, if you need this
//rest of your code to actually move your piece
}