UMG Setting Image

Hey guys,

I’m trying to set an an image at runtime in a widget.



UImage* UManager::getImage()
{
	// tried both
	// FString Path = FString("/Game/Asset/Textures/texture.texture");
	FString Path = FString("/Game/Asset/Textures/texture");

	UTexture2D* Texture = Cast<UTexture2D>(StaticLoadObject(UTexture2D::StaticClass(), NULL, *Path));

	UImage* Image = ConstructObject<UImage>(UImage::StaticClass());
	Image->SetVisibility(ESlateVisibility::Visible);
	Image->SetBrushFromTexture(Texture);
	Image->SetOpacity(50.5f);	

	UE_LOG(LogTemp, Warning, TEXT("Log")); // will be logged in the console

	return Image;
}


The Widget’s graph looks like this:


“Get Own Pokemon Sprite” = “getImage()”

Unfortunately it has absolutely no effect and it doesn’t change anything. If I change the image manually in the widget to the texture it will be displayed properly. The image is set to variable. I have no clue what else to do.

I appreciate every hint,
thanks!

ImagePokeEnemy is a widget, you don’t Set it. That won’t update the widget hierarchy, you’re just stomping the member pointer, which is probably a bad idea, I should consider finding a way to prevent that…

Anyway, you need to set the texture/brush of the widget. There are functions on the ImagePokeEnemy widget to accomplish this, SetBrush…, so don’t construct a new UImage widget, just set the texture on the brush.

Thank you very much, worked like a charm!