How can I use SpawnDecalAtLocation in C++

337592-

I would like to know how to set the DecalMaterial, one of the parameters of the SpawnDecalAtLocation function.

SpawnDecalAtLocation is called from mouse events, not from constructor.

In Blueprint, it looks like the following picture.

337593-

Hello! Where are you calling this code? In widget? Or some actor? Or another class?

PlayerController. I want to create a decal at the mouse pick point

Ok, but mouse click point is just point on 2D screen while decals are created in 3D world. So, you can deproject 2D screen position to 3D world coords or throw ray cast and get hit location in 3D world. Both methods are available in C++:

You’ll need to move the UMaterialInterface* DecalMaterial part into the class declaration in your header (.h) file.

Next, add to the source (.cpp) file:
#include "UObject/ConstructorHelpers.h"

Then, add to the Constructor:

static ConstructorHelpers::FObjectFinder DecalMaterialAsset(TEXT("/Game/Materials/MyDecalMaterial.MyDecalMaterial")); DecalMaterial = DecalMaterial.Object;


Note: This will load the MaterialInterface “MyDecalMaterial” located a directory called “Materials” in your Content folder. You can change the path however you like, just make sure you repeat the file name in the path after the period (like OtherDecal.OtherDecal or T_Chair.T_Chair.

Hope this helps!

Thanks for the answer! +1