[SOLVED] GetActorLocation() on Server RPC Crash

Edit: My solution to this was to pass the Card Placement reference as a parameter to the Server RPC

Hi guys!

I’m trying to get an actor location from a Server RPC, but this crashes my listen server…
Let me please explain what I’m doing:

  1. My GameMode spawns an actor (Card Placement aka GridCell) at the start of the match
  2. From my PlayerController (client-side) I “Tick” the actor under the mouse and check for a valid card placement
  3. When I release the left mouse button, if the card placement is valid, I run a Server RPC to play the card

The problem here is that while in the PlayerTick, the Card Placement Reference is not null and I get no error If I do a simple GetActorLocation() on that, but I can’t get why the reference is null when I use it in the Server RPC.

Here is the actual code:

// .h file
UPROPERTY( Replicated )
class AGridCell* TargetGridCell;

/** Functions */
void PlayCardOnMouseRelease();

bool ValidateCardPlacement( class AActor* HitActor );

UFUNCTION( Server, Reliable )
void Server_PlayCard();

void HandlePlayCard();

class ACard* CreatePlaceableCardOnServer( FVector InSpawnLocation );

void SetCardReplicateionAndData( class ACard* InCard );

void AddCardToCellPlacement( class ACard* InCard, class AGridCell* InCell );
// I run this to check if I can spawn the card from my player controller
// This function is called in PlayerTick and while here, TargetGridCell
// reference is != nullptr
bool ADiacroniaPlayerController::ValidateCardPlacement( AActor* HitActor )
{
    if( HitActor == nullptr ) return false;

    // If a grid cell has been detected under cursor
    if( AGridCell* const CellRef = Cast< AGridCell >( HitActor ) )
    {

        // If the cell is not free, we can't positon another card onto it
        if( CellRef->GetCellState() != ECellState::Free )
        {
            SetValidCardDrop( false );
            return false;
        }

        SetValidCardDrop( true );
        TargetGridCell = CellRef;
        return true;
    }

    SetValidCardDrop( false );
    return false;
}

Then, when I release the Left Mouse Button, this function is called:

void ADiacroniaPlayerController::PlayCardOnMouseRelease()
{
    if( IsValidCardDrop() )
    {
        Server_PlayCard();
        CardWidgetRef->RemoveFromViewport();
        return;
    }

    Client_DropCard();
    Client_DestroyCard();
    CardWidgetRef->ShowCardView();
}

void ADiacroniaPlayerController::Server_PlayCard_Implementation()
{    
    HandlePlayCard();
}

void ADiacroniaPlayerController::HandlePlayCard()
{
    if( GetLocalRole() == ROLE_Authority )
    {        
        ServerCardPlayedRef = CreatePlaceableCardOnServer( FVector( 0.0f, 0.0f, 200.0f ) );
        SetCardReplicationAndData( ServerCardPlayedRef );
        AddCardToCellPlacement( ServerCardPlayedRef, TargetGridCell );
        Client_DropCard();
        Client_DestroyCard();
    }
}

ACard* ADiacroniaPlayerController::CreatePlaceableCardOnServer( FVector InSpawnLocation )
{
    if( GetLocalRole() == ROLE_Authority )
    {
        FActorSpawnParameters SpawnParams;
            SpawnParams.Owner = this;        

        return GetWorld()->SpawnActor< ACard >( TestCardClass, InSpawnLocation, FRotator( 0.0f, 45.0f, 0.0f ), SpawnParams );
    }
    return nullptr;
}

void ADiacroniaPlayerController::SetCardReplicationAndData( ACard* InCard )
{
    InCard->SetReplicates( true );
    InCard->SetReplicateMovement( true );
}

void ADiacroniaPlayerController::AddCardToCellPlacement( ACard* InCard, AGridCell* InCell )
{
    if( GetLocalRole() == ROLE_Authority && InCard )
    {
        InCard->SetCardCellOwner( InCell->GetActorLocation() );
        InCard->GoToState( ECardState::PlayedFromHand );
    }
}

Also, I’m copy-pasting the crash report:

Fatal error!

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000148

0x00007ffd9d0fbd73 UE4Editor-DiacroniaCards-4868.dll!ADiacroniaPlayerController::AddCardToCellPlacement() [E:\GitHub\DiacroniaCards\Source\DiacroniaCards\Private\Player\DiacroniaPlayerController.cpp:350]
0x00007ffd9d0fd9f6 UE4Editor-DiacroniaCards-4868.dll!ADiacroniaPlayerController::HandlePlayCard() [E:\GitHub\DiacroniaCards\Source\DiacroniaCards\Private\Player\DiacroniaPlayerController.cpp:316]
0x00007ffd9d0fe46b UE4Editor-DiacroniaCards-4868.dll!ADiacroniaPlayerController::Server_PlayCard_Implementation() [E:\GitHub\DiacroniaCards\Source\DiacroniaCards\Private\Player\DiacroniaPlayerController.cpp:231]
0x00007ffddbcd26a4 UE4Editor-CoreUObject.dll!UFunction::Invoke() [D:\Build\++UE4\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\Class.cpp:5588]
0x00007ffddbf60ef3 UE4Editor-CoreUObject.dll!UObject::ProcessEvent() [D:\Build\++UE4\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\ScriptCore.cpp:1992]
0x00007ffdce82c125 UE4Editor-Engine.dll!AActor::ProcessEvent() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\Actor.cpp:864]
0x00007ffdcee28c65 UE4Editor-Engine.dll!FObjectReplicator::ReceivedRPC() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\DataReplication.cpp:1269]
0x00007ffdcee26fac UE4Editor-Engine.dll!FObjectReplicator::ReceivedBunch() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\DataReplication.cpp:1081]
0x00007ffdceda73a6 UE4Editor-Engine.dll!UActorChannel::ProcessBunch() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\DataChannel.cpp:2862]
0x00007ffdcedaf6d9 UE4Editor-Engine.dll!UActorChannel::ReceivedBunch() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\DataChannel.cpp:2703]
0x00007ffdcedb32bc UE4Editor-Engine.dll!UChannel::ReceivedSequencedBunch() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\DataChannel.cpp:412]
0x00007ffdcedb27bc UE4Editor-Engine.dll!UChannel::ReceivedNextBunch() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\DataChannel.cpp:826]
0x00007ffdcedb2e20 UE4Editor-Engine.dll!UChannel::ReceivedRawBunch() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\DataChannel.cpp:510]
0x00007ffdcf330367 UE4Editor-Engine.dll!UNetConnection::ReceivedPacket() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\NetConnection.cpp:2783]
0x00007ffdcf33200d UE4Editor-Engine.dll!UNetConnection::ReceivedRawPacket() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\NetConnection.cpp:1316]
0x00007ffdc073ac60 UE4Editor-OnlineSubsystemUtils.dll!UIpNetDriver::TickDispatch() [D:\Build\++UE4\Sync\Engine\Plugins\Online\OnlineSubsystemUtils\Source\OnlineSubsystemUtils\Private\IpNetDriver.cpp:1370]
0x00007ffdcf36b7a1 UE4Editor-Engine.dll!TBaseUObjectMethodDelegateInstance<0,UNetDriver,void __cdecl(float),FDefaultDelegateUserPolicy>::ExecuteIfSafe() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Core\Public\Delegates\DelegateInstancesImpl.h:611]
0x00007ffdce774260 UE4Editor-Engine.dll!TMulticastDelegate<void __cdecl(float),FDefaultDelegateUserPolicy>::Broadcast() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Core\Public\Delegates\DelegateSignatureImpl.inl:955]
0x00007ffdcf141a64 UE4Editor-Engine.dll!UWorld::Tick() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\LevelTick.cpp:1331]
0x00007ffdcef16a4f UE4Editor-Engine.dll!UGameEngine::Tick() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Engine\Private\GameEngine.cpp:1794]
0x00007ff714c39bda UE4Editor.exe!FEngineLoop::Tick() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Launch\Private\LaunchEngineLoop.cpp:4836]
0x00007ff714c511ac UE4Editor.exe!GuardedMain() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Launch\Private\Launch.cpp:169]
0x00007ff714c5128a UE4Editor.exe!GuardedMainWrapper() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:137]
0x00007ff714c652ed UE4Editor.exe!WinMain() [D:\Build\++UE4\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:268]
0x00007ff714c6801a UE4Editor.exe!__scrt_common_main_seh() [d:\agent\_work\5\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288]
0x00007ffe0b627c24 KERNEL32.DLL!UnknownFunction []
0x00007ffe0b9ed721 ntdll.dll!UnknownFunction []
// 0x00007ffd9d0fbd73 UE4Editor-DiacroniaCards-4868.dll!ADiacroniaPlayerController::AddCardToCellPlacement() [E:\GitHub\DiacroniaCards\Source\DiacroniaCards\Private\Player\DiacroniaPlayerController.cpp:350]
// refers to this line of code in ADiacroniaPlayerController::AddCardToCellPlacement(...)
InCard->SetCardCellOwner( InCell->GetActorLocation() );

And I’m 100% sure that the error is given by just that “GetActorLocation()”… Any advice on how to do this or ideas on what I’m doing wrong? Thank you guys so much for your time and help!