Unreal Engine 5.7.3 - C++ API Improvements
TLDR;
Can we have the same style API in Unreal as Unity does?
P0: Functions that expose the VM and State Machine
P1: Documentation
P2: Functions to create instances on the widget itself, not the Rive file
P3: Widget events for Rive initialization
Context
I’m testing out the new Rive rewrite for Unreal Engine 5.7.3 and I’m hitting a wall with a relatively tame use-case. The documentation isn’t as comprehensive as other platforms (i.e. Unity) which I can attribute to the API being newer; however, I’m using other docs to get the ‘spirit’ of how things are generally done in Rive which I will draw comparisons to.
Setup
I have a single Artboard called HUD, backed by a HUDVM. The HUDVM has two fields: `title` and a nested View Model `reticleVM`. The ReticleVM has a single field `isHovered`. The structure resembles the following:
HUDVM
title: string
reticleVM: ReticleVM
isHovered: bool
I believe I have all the bindings setup properly in the Rive editor as it works in the Animation view. When the `isHovered` field is set to `true`, the Reticle component, backed by the `reticleVM` property on HUDVM, changes as expected.
Blueprint Editor
In Unreal Engine, I have a RiveWidget using the imported HUD Rive File. The Artboards, State Machine, View Models, and Instances show up under Rive details as expected. As I understand it, I have two options to instantiate the HUDVM: use auto bind default view model (checkbox) or call the Make View Model node in Blueprint (K2Node_MakeViewModel).
If I auto-bind, I don’t see a way to get a reference to the VM in either BP or C++ to update any field on the VM. Admittedly, I’m not sure what the use case for this would be if I can’t get a reference to the VM.
If I use the BP node, I can store a reference to the VM after creation; however, it’s not clear where/when I should create the child `reticleVM` (or if I should create one at all). I assume I would make the child VM, then the parent, then set the `reticleVM` field on the parent.
C++ API
In C++, the API is a little confusing.
Unity has a relatively straightforward API:
if (riveWidget.Status == WidgetStatus.Loaded)
{
ViewModel vm = riveWidget.Artboard.DefaultViewModel;
ViewModelInstance vmi = vm.CreateDefaultInstance();
// Applying to a state machine will automatically bind to its artboard
riveWidget.StateMachine.BindViewModelInstance(vmi);
}In Unreal:
I don’t see a Rive status to subscribe to on the widget, file, file descriptor, or artboard. There’s a `FRiveReadyDelegate` defined in RiveWidget.h that seems to not be used and/or exposed.
I’m using `NativeConstruct` with some guards instead.
The only default VM methods I can find either give me back strings or enums, no VM object.
The only ways to create a VM seem to be static functions like `URiveFile::CreateViewModelByName(RiveFile, ViewModelName, ViewModelInstanceName)`
Am I meant to create all child VMs manually? Why not use the instances defined in Rive Editor? Same question in BP.
The docs say to bind the VM to the State Machine (not Artboard); however, the only functions that deal with them return strings.
Auto-bind seems to apply the VM to the state machine, I should be able to get a reference to a State Machine and do this manually.
If I only bind the HUDVM to the Artboard and attempt to set a value on the child `reticleVM`, I get an error from `RiveViewModel.cpp`: “Failed to unsettle view models artboard state machine”. This is despite ensuring the child instance is valid.
A janky workaround is to iterate through all fields on the parent VM and use reflection to check for nested VMs, then call `ChildVM->SetOwningArtboard(Artboard)`. This seemingly fixes everything but I don’t believe this is realistic.
No matter what combination of what I do, I always get one warning “ArtboardInstance is invalid”. I assume it’s a timing issue.
If I add the `isHovered` field to the HUDVM, everything works as expected. It’s the child VM lifecycle, docs, and C++ API that I’m struggling with.