OnConstruction()

OnConstruction is similar of Blueprint Construction Script. OnConstruction() will fire every time there is a change happening inside Actor inside Editor. Example, if we want to create a tower that can shoot different things (Fire, Ice, Acid). we can use the OnConstruction() function to do the changes. We could set some variable that will tell what kind of tower that specific instance should be and when we change that variable, OnConstruction() function is called instantly and do necessary changes to actor. This comes very handy, because we can see all the changes happening, before the game is even running!


Example

We create an Actor that will change it’s static mesh and size by checking the enum value. We start by creating a class based on actor. Go ahead and create a static mesh component called MainMesh and give some mesh for it. You can look example here. After the class is created and mesh is set, we create the enum class. Remember, this comes just top of the UCLASS() macro.

UENUM(BlueprintType) //"BlueprintType" is essential to include
enum class Enum_MeshChanger : uint8
{
Chair UMETA(DisplayName = "Chair"),
Rock UMETA(DisplayName = "Rock"),
Decorator UMETA(DisplayName = "Decorator")
};

In private section, we create enum and  OnConstruction() function.  Enum will have UPROPERTY of EditInstanceOnly, because we want to set our enum inside the editor view.

private:
UPROPERTY(EditInstanceOnly)
Enum_MeshChanger enum_MeshChanger;
virtual void OnConstruction(const FTransform& Transform) override;

In .CPP, we create our declaration for the function and set mesh properties depending the enum value.  What you can see here is, we can set multiple things to happen inside our class by changing one single enum value inside editor window. We can see the all changes immediadely.  Rememeber, function is ran everytime you make a slighest change to your actor.

void AC_DynamicMaterialActor::OnConstruction(const FTransform& Transform)
{
Super::OnConstruction(Transform);
// Placeholder mesh.
UStaticMesh* NewMesh;
// Change settings depenging the enum value. 
if (enum_MeshChanger == Enum_MeshChanger::Chair)
{
NewMesh = Cast(StaticLoadObject(UStaticMesh::StaticClass(), NULL, TEXT("/Game/StarterContent/Props/SM_Chair.SM_Chair")));
MainMesh->SetWorldScale3D(FVector(2, 2, 2));
MainMesh->SetStaticMesh(NewMesh);
}
if (enum_MeshChanger == Enum_MeshChanger::Rock)
{
NewMesh = Cast(StaticLoadObject(UStaticMesh::StaticClass(), NULL, TEXT("/Game/StarterContent/Props/SM_Rock.SM_Rock")));
MainMesh->SetWorldScale3D(FVector(1, 1, 1));
MainMesh->SetStaticMesh(NewMesh);
}
if (enum_MeshChanger == Enum_MeshChanger::Decorator)
{
NewMesh = Cast(StaticLoadObject(UStaticMesh::StaticClass(), NULL, TEXT("/Game/StarterContent/Props/SM_MatPreviewMesh_02.SM_MatPreviewMesh_02")));
MainMesh->SetWorldScale3D(FVector(3, 3, 3));
MainMesh->SetStaticMesh(NewMesh);
}
}

a1

That’s it! Now you know how to use OnConstruction() function to make changes to your actor before the game is even started!

Vastaa

Sähköpostiosoitettasi ei julkaista. Pakolliset kentät on merkitty *