How-To: Build an MVVM Screen
Goal: a screen whose visuals bind to a ViewModel through UE's ModelViewViewModel (MVVM) plugin, using the Crimson bases so every ViewModel can reach its ULocalPlayer.
1. Pick the right base
| Base | Use for | Adds |
|---|---|---|
UCrimsonViewModelBase | Any ViewModel | Initialize(LocalPlayer) (BlueprintCallable), GetLocalPlayer() (BlueprintPure), NativeOnInitialized (BlueprintNativeEvent) - a hook that fires once the local player is set |
UCrimsonScreenViewModelBase | One ViewModel per screen/page | Nothing - semantic marker so screen VMs are distinguishable from entry VMs |
UCrimsonEntryViewModelBase | One ViewModel per list/tile row | EntryIndex (FieldNotify) so row widgets can bind to their own position |
2. Author the ViewModel
Create a Blueprint Class with parent Crimson Screen View Model Base (e.g. BP_InventoryVM). Add variables (e.g. GoldText) and tick Field Notify on each one the view should react to. Override the Native On Initialized event to pull initial state - Get Local Player is valid from that point on.
3. Bind the view and initialize
In the screen widget open Window -> Viewmodels, add your ViewModel class (creation mode 'Create Instance' is simplest), and add View Bindings from its FieldNotify variables to widget properties. Then initialize the VM when the screen activates:
4. Entry ViewModels for list rows
Give each row a UCrimsonEntryViewModelBase subclass. The owning code sets EntryIndex (C++ SetEntryIndex); the row widget binds to it (FieldNotify) for striping, numbering, or selection logic. This pairs with How-To: Lists with Mixed Item Types - the entry widget grabs its VM in OnListItemObjectSet.
See also
- How-To: Lists with Mixed Item Types - the view side of entry VMs.
- How-To: Push & Pop Screens - activation lifecycle the VM init rides on.