Called Function is nullptr c++

So the error is ‘Exception thrown: read access violation. this was nullptr. unreal engine’ - Card.cpp, Line 23.
I know I have to initialize the Actor first, but I just don’t know how to do it without Traces. Maybe I am getting all wrong, but yeah after days of trying, this post is my last hope.
What I am trying to do is, When clicking on the Card, then the UMG Menu I created pops up so you can choose what Action the Card should do (Place it for example), but now when I try to call a function from the My Actor ‘ACard’ to the ‘UButton’, its a nullptr.

Card.h

#pragma once
include “CoreMinimal.h”
include “Card.generated.h”

UCLASS()

class GAME_API ACard : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor’s properties
ACard();

void SummonCard();

// Mesh
UPROPERTY(EditAnywhere, Category = "Mesh")
    class UStaticMeshComponent* CardMesh;

UPROPERTY()
    FVector Location;

protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;

public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};

Card.cpp

#include "Card.h"

// Sets default values
ACard::ACard()
{
    PrimaryActorTick.bCanEverTick = true;
    CardMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
    Location = GetActorLocation();
}

// Called when the game starts or when spawned
void ACard::BeginPlay()
{
    Super::BeginPlay();
}

void ACard::SummonCard()
{
    Location.Y -= 50.f;
}

// Called every frame
void ACard::Tick(float DeltaTime)
{
    Super::Tick(DeltaTime);
}

CardMenu.h

#pragma once

#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "GameFramework/Actor.h"
#include "game/Duel/Card.h"
#include "CardMenu.generated.h"

UCLASS()
class GAME_API UCardMenu : public UUserWidget
{

    GENERATED_BODY()

public:
    UFUNCTION()
        void OnSummonClick();

    UPROPERTY()
        ACard* Card;

protected:
    UPROPERTY(BlueprintReadOnly, meta = (BindWidget))
        class UButton* SummonBtn;

public:
    virtual void NativeConstruct() override;
};

CardMenu.cpp

#include "CardMenu.h"
#include "Components/TextBlock.h"
#include "Components/Button.h"
void UCardMenu::NativeConstruct()
{

    Super::NativeConstruct();

    SummonBtn->OnClicked.AddDynamic(this, &UCardMenu::OnSummonClick);

}
void UCardMenu::OnSummonAtkClick()
{
    RemoveFromViewport(); // Is added to viewport from PlayerController
    Card = Cast<ACard>(???); // Do i have to do a cast?, if so, how?
    if (Card)
    {
        Card->SummonCard();
    }
    GEngine->AddOnScreenDebugMessage(-1, 2.f, FColor::Green, FString("removed from viewport!"));
}

So when you click the card, you’re gonna want to store the clicked card somewhere. In CardMenu perhaps?

Or I would have a CardsContainer, or maybe call it DrawnCards, or Hand? This could be responsible for spawning/drawing new cards, and then whenever a new card is drawn, it can subscribe the click event on the card, and store the card that was clicked. Then CardMenu could get that card

Right now from what you’ve shown it doesn’t look like CardMenu has a way to know what card is clicked/selected

1 Like