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!")); }