-
Notifications
You must be signed in to change notification settings - Fork 8
Getting Started
- Clone this repo into your Plugins folder
- Launch the editor and enable the plugin
-
This step is only necessary for creating View Models in C++
In your project's*.build.cs
file, you'll need to addMDViewModel
as a dependency:PublicDependencyModuleNames.Add("MDViewModel");
- Next step is to set up a view model to use in your blueprints
A view model is an intermediate object between a View (your UMG widget or Actor Blueprint) and the Model (the system/object you want data from).
Start by creating a new class that extends from UMDViewModelBase
.
For this example, we're setting up a basic Character view model that we'll use to display the player's current character's name.
We'll add an FText CharacterName;
property to our view model, making sure to mark it with BlueprintReadOnly
so we can read it in our blueprints, Transient
since we won't be serializing data in the property, and FieldNotify
so that our blueprints can bind to it.
We'll also override void InitializeViewModel()
so that we can fetch the name from our character when the view model is created. We use GetContextObjectEnsure()
to get the character. The view model's context object will be explained in the Binding to the View Model in the Editor section of this page.
ExampleCharacterViewModel.h
#pragma once
#include "ViewModel/MDViewModelBase.h"
#include "ExampleCharacterViewModel.generated.h"
// A view model that represents an Example Character actor
UCLASS(DisplayName = "Example Character VM")
class UExampleCharacterViewModel : public UMDViewModelBase
{
GENERATED_BODY()
protected:
virtual void InitializeViewModel() override;
// The name given to the represented character
UPROPERTY(BlueprintReadOnly, Transient, FieldNotify)
FText CharacterName;
};
ExampleCharacterViewModel.cpp
#include "ExampleCharacterViewModel.h"
#include "ExampleCharacter.h"
void UExampleCharacterViewModel::InitializeViewModel()
{
Super::InitializeViewModel();
// GetContextObjectEnsure will Cast our context object for us and ensure if the context object is not the type we expected
if (AExampleCharacter* Character = GetContextObjectEnsure<AExampleCharacter>())
{
// If the value (2nd param) passed to MDVM_SET_FIELD is different than the field's current value (1st param)
// then the field will be updated and any blueprints that are bound to that field will be notified of the change
MDVM_SET_FIELD(CharacterName, Character->GetCharacterName());
// We could also bind to any delegates on ExampleCharacter here. For example if the character's name can be changed
// then we would listen for that here and call MDVM_SET_FIELD so that our bound blueprints are notified.
// If necessary, we can override ShutdownViewModel() to unregister any delegates we've bound to
}
}
With our simple view model setup, we can compile and launch the editor where we'll bind a UMG widget to our new view model.
This is just a small portion of what you could do in your view model, for more advanced examples check out [TODO - Link to advanced page on Writing View Models]
BP View Model Class documention Coming soon...
To display the character name from the Example Character View Model we setup above, create a simple UMG Widget with a TextBlock that we'll bind to our view model and add the widget to your HUD.
Open up your UMG Widget Blueprint and switch to Graph Mode. Open the View Model editor, either from the drawer button at the bottom or from the menu bar Window -> View Models.
Click the + Add View Model button at the bottom left of the view model editor tab.
This will open a new window where you'll select the view model you want to bind your View to and set how that view model instance will be retrieved or set on your View.
WIP