GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Getting Started Getting Started
Dylan Dumesnil edited this page
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 We'll add an We'll also override 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 Clone this wiki locally |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |