Page 1 of 1

Registering View Models in Unreal Engine C++

Posted: 18 Mar 2024, 18:26
by KCoppinsIventis
Hi there,

I was looking at this layered UI example as I have some HUD and I wanted to open a pause menu. I have a master user control `MainView` with my `HUDView` user control and a `PauseMenuView`. I also have ViewModels defined for each view which is the name of the view with the Model suffix. I have the following definition inside the `MainView`:
<UserControl.Resources>
    <DataTemplate x:Key="PauseMenuViewModelDataTemplate" DataType="{x:Type PauseMenu:PauseMenuViewModel}">
        <PauseMenu:PauseMenuView/>
    </DataTemplate>
    <DataTemplate x:Key="HudViewModelDataTemplate" DataType="{x:Type HUD:HUDViewModel}">
        <HUD:HUDView/>
    </DataTemplate>
</UserControl.Resources>
I have set the data context to my `MainViewModel` which handles toggling the `PauseMenuView`, exactly the same as the sample. It works perfectly within Blend. When loading into Unreal Engine, I do the same and manually set the MainViewModel to my c++ code-behind implementation. However I get the following errors on rendering:
Cannot set value for 'DataTemplate.DataType', unknown type 'PauseMenu:PauseMenuViewModel'
Cannot set value for 'DataTemplate.DataType', unknown type 'HUD:HUDViewModel'
I assume this is because I havent registered my view models. However, as per the Sample I have made my view models `UObjects`, how can I register them as UObjects? Or is there something else I'm missing?

Re: Registering View Models in Unreal Engine C++

Posted: 18 Mar 2024, 20:19
by hcpizzi
Hi,

The factories for such components are registered automatically. The name of a native C++ UClass is of the form /Script/ModuleName.ClassName. We register such classes with the name ModuleName.ClassName. So, most likely reason is that the namespaces are wrong.

Hope this helps!

Re: Registering View Models in Unreal Engine C++

Posted: 18 Mar 2024, 20:28
by KCoppinsIventis
That was exactly it, I have renamed my namespaces in my blend project to match those of my unreal module. Thanks for your help!