Using Mutable from Code - anticto/Mutable-Documentation GitHub Wiki

Creating a Customizable Object Instance

As seen in Basic Concepts, a Customizable Object Instance references a Customizable Object plus a set of parameter values:

UCustomizableObject* Object = ...

if (Object)
{
    Instance = NewObject<UCustomizableObjectInstance>();

    // Link to an existing Customizable Object
    Instance->SetObject(Object);
}

Connecting an Instance with an existing Actor

The best way to link an Instance to an existing Actor is to use the Customizable Object Instance Usage. It lets you link an existing Actor's Skeletal Mesh Components to a Skeletal Mesh Component node.

Usage = NewObject<UCustomizableObjectInstanceUsage>();

// Link to the previously created Instance
Usage->SetCustomizableObjectInstance(Instance);

// Select Skeletal Mesh Component node to link to
Usage->SetComponentName("Body");

// Attach to existing Actor's Skeletal Mesh Component
Usage->AttachTo(Component);

Changing Parameters

At any time, you can change the parameters of a Customizable Object Instance. This change will not take effect until an Update is requested.

// Sets the bool value of the parameter "Freckles"
Instance->SetBoolParameterSelectedOption("Freckles", true);

// Sets the int value of the parameter "Shirt" (Enum Type)
Instance->SetEnumParameterSelectedOption("Shirt", "BasicShirt");

// Sets the float value of the parameter "Fatness", range from 0 to 1
Instance->SetFloatParameterSelectedOption("Fatness", 0.5f);

// Sets the color value of the parameter "EyeColor"
Instance->SetColorParameterSelectedOption("EyeColor", FColor::Blue);

Changing State

In addition to changing parameters, you can also change the State.

Instance->SetCurrentState("Locker");

Updating an Instance

Once all parameters are set, to apply them you need to request an Update. Updates are always asynchronous, so expect some delay before seeing the resulting customizations.

// Updates all components linked to this Instance
Instance->UpdateSkeletalMeshAsync();

[!NOTE] Calling UpdateSkeletalMeshAsync on the Customizable Object Instance or the Customizable Object Instance Usage is equivalent. All Components linked to the Instance will be updated.

Updated Delegate

Since the Updates are asynchronous, Mutable provides two ways to know when an update has finished.

By calling specialized Update with a delegate:

FInstanceUpdateDelegate Delegate;
Delegate.AddObject(this, ...);

Instance->UpdateSkeletalMeshAsyncResult(Delegate);

Or by using existing delegates in the Instance or Usage:

Instance->UpdatedDelegate.BindUObject(this, ...);