UE4 C++ cant access a declared variable in a function

I have a function inside a UWidget class and that function adds slots to a UUniformGridPanel, it works perfectly, I want to change the slot image on a key press. When I press it, it crashes with “EXCEPTION ACCESS VIOLATION” error.
Every variable and function is in the public section, the action goes like this:

  • Call UWidget class function from AActor class when E key pressed
  • The called function crashes the editor when trying to access ANY variable from the same class

AActor class where the UWidget class is called:

void ACPP_InteractBase::Pickup(){
    GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Orange,FString::Printf(TEXT("+ %s"),(*ItemInfo.Name.ToString())));
	Destroy();
	TestInvBar->UpdateInventoryBarSlot(ItemInfo.Image);
}

UWidget class where the function (UpdateInventoryBarSlot) is written:

void UCPP_InventoryBar::AddInventoryBarSlots(){
    for(int i = 0; i <= 9; i++)
    {
        UPROPERTY()
	    UCPP_InventoryBarSlot* InventoryBarSlot = CreateWidget<UCPP_InventoryBarSlot>(GetWorld(),InventoryBarSlotClass);
        InventoryBarSlots.Add(InventoryBarSlot);
        InventoryBarSlotPanel->AddChildToUniformGrid(InventoryBarSlots[i],0,i);
    }
}

void UCPP_InventoryBar::UpdateInventoryBarSlot(class UTexture2D* NormalImage){
    GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Orange,FString::Printf(TEXT("Works %s"), NormalImage));

    if(InventoryBarSlotPanel != nullptr){
        InventoryBarSlotPanel->GetPaletteCategory();
    }
    //InventoryBarSlots[0]->UpdateButtonNormalImage(NormalImage);
}

AddInventoryBarSlots function adds the slots without errors, and its variables are declared in the .h file, yet its not accessible in THAT function.

The DebugMessage executes if no variable is called in the function.

Have you marked your InventoryBarSlots array with UPROPERTY() ?

Yes

public:
	void NativeConstruct() override;
	void AddInventoryBarSlots();
	void UpdateInventoryBarSlot(class UTexture2D* NormalImage);
	UPROPERTY( meta = (BindWidget))
	UImage* InventoryBarFrame;
	UPROPERTY( meta = (BindWidget))
	UUniformGridPanel* InventoryBarSlotPanel;


	UPROPERTY(EditAnywhere)
	TSubclassOf<UCPP_InventoryBarSlot> InventoryBarSlotClass;
	UPROPERTY()
	TArray<UCPP_InventoryBarSlot*> InventoryBarSlots;

};

Maybe it needs “EditAnywhere”? Or the functions need UFUNCTION()?

It looks like that the problem is not in the UWidget class, I called the function with the “null” variable inside Native Construct and it ran without issues (so its not null). So its probably a problem within the AActor class where I call the function, read something that the variable from the new class is probably null so you cant call functions from it.

So how would I call it properly?

Declaring the variable where I call the function from (.h):

	UPROPERTY()
	UCPP_InventoryBar* TestInvBar;

Calling the function inside .cpp

	TestInvBar->UpdateInventoryBarSlot(ItemInfo.Image);

(The parameter probably doesnt matter, tried it without parameter and it still crashed).
This is how it looks like. What could be the problem?

I have fixed the class instance error with the following command:

TestInvBar = NewObject<UCPP_InventoryBar>(GetWorld(),InventoryBarClass);

So now when I call a function it runs, but any variable declared in that class (where the function comes from) is null.

AActor class where I call the function:

if(TestInvBar != nullptr){
		TestInvBar->UpdateInventoryBarSlot(ItemInfo.Image);
    	GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Orange,FString::Printf(TEXT("----------------------")));
    	//GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Orange,FString::Printf(TEXT("testFloat, %f"), TestInvBar->test));
	}
	else{
    	GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Orange,FString::Printf(TEXT("TestInvBar is nullptr")));
	}

The UWidget Class where the function is declared:

void UCPP_InventoryBar::NativeConstruct(){
    Super::NativeConstruct();

    test = 666.0f;
    AddInventoryBarSlots();
}
    
void UCPP_InventoryBar::AddInventoryBarSlots(){
    for(int i = 0; i <= 9; i++)
    { 
        InventoryBarSlot = CreateWidget<UCPP_InventoryBarSlot>(GetWorld(),InventoryBarSlotClass);
        InventoryBarSlots.Add(InventoryBarSlot);                                                                                                     
        InventoryBarSlotPanel->AddChildToUniformGrid(InventoryBarSlots[i],0,i);
    }
}

void UCPP_InventoryBar::UpdateInventoryBarSlot(UTexture2D* NormalImage){
    GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Orange,FString::Printf(TEXT("testFloat, %f"), test));
    if(InventoryBarSlot != nullptr){
        GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Orange,FString::Printf(TEXT("Not Null")));
    }
    else{
        GEngine->AddOnScreenDebugMessage(-1,5.f,FColor::Orange,FString::Printf(TEXT("Null")));
    }
    //InventoryBarSlots[0]->UpdateButtonNormalImage(NormalImage);
}

And the output:
kép

The test (float variable) has a value of 666.f yet it returns 0. It only works if I declare its value inside the .h file. Which is not possible for most of the variables.
Any idea what I’m doing wrong?

This topic has been moved from International to Programming & Scripting.

When posting, please review the categories to ensure your topic is posted in the most relevant space. Hopefully, this new category will help you (or other devs!) find an answer.

In the meantime, good luck and happy developing :slight_smile:

Thank You! I couldnt post in other categories except “International”.