Access ViewModel collection from ViewModelResolve->Create Instance method

I am using MVVMViewModelContextResolver to initialize my ViewModels in the UI
However in the blueprint CreateInstance method, you are not allowed to SET/Store variables nor access the ViewModelSubsystem->ViewModelCollection so you are only left with the option of instancing a new VM every time it gets called (flooding the memory everytime you visit a screen using that VM).

In code it says " Shared data to find or create a ViewModel at runtime" but how can I find an existing VM then?

I would like access to the ViewModelCollection and if none is set with that context, I want to create one and store it in the collection. If already exists, just return the existing object ref.

Am I missin something?

Erik

Late to the game here, but if you want to find an existing Viewmodel at runtime from the Global Viewmodel Repository you can access the subsystem that holds it in the CreateInstance function.

UObject* MyResolverClass::CreateInstance(const UClass* ExpectedType, const UUserWidget* UserWidget, const UMVVMView* View) {
/**
* 1. Get the World from the UserWidget using GetWorld(UserWidget)
* 2. Get the Game Instance from the World if it’s valid (it will be lol)
* 3. Get the Viewmodel Subsystem from the Game Instance, and return that
* 4. If it’s not there, create it instead and optionally register it with the global subsystem
*/
}

This is pseudocode, but the idea is solid. Any Widget passed in to this function is associated with SOME player controller (usually) which means it’s also probably world-aware, so calling GetWorld()from your widget will be your best friend here.

In Blueprint it’s largely the same with nodes, I think the code is written the same as the blueprint would be.