Download

Some problems while implement a function that allow player switch between multiple UWorld.

Hello, I’m now trying to make some function to allow the player to switch between multiple UWorld just like Editor and PIE.
I almost finish it, but there still have some problem. I can hack the Engine code but I want to find a better solution(I really don’t want to modify the Engine code.)
In my implementation, there only have one window, and I can make the window display any UWorld by call SetViewportClient function in FSceneViewport. It does works but I found a big problem immediately.
So, the first problem was about the ViewportOverlayWidget. In the default UGameViewportClient, there have no ViewportOverlayWidget, so I can’t call AddToViewport() in the new world or I will get the Engine crash. I tried to use the existing one. but I found I cannot get the ViewportOverlayWidget from the FSceneViewport because it’s private and has no getter. It’s really annoyed me. I can’t find any way to get the variable so I modified the Engine code, add a getter and it works. But as I say I don’t want to modify the Engine code, so I want to know if there have any better way to do it?
The other problem is about the FAudioDeivce. As the comment says “every UGameViewportClient should have an AudioDevice”, I think it will work fine with UnrealEditor but it causes a problem in Standalone Game.
In Standalone Game, the FAudioDeviceManager::CreateAudioDevice()(Engine\Source\Runtime\Engine\Private\AudioDeviceManager.cpp line.323) will refuse to create more AudioDevice. There have a comment says “// If we are running without the editor, we only need one audio device.”. But I have many worlds and each worlds have its own GameViewportClient. When the UGameViewportClient destroyed, It will still shutdown an AudioDevice. It will cause the only AudioDevice been destroyed. The worst thing is once I continue to create a new world, it will try to create a new AudioDevice, but because it’s not in the AudioThread, the Engine will crash.
I think if there can only have 1 AudioDevice, so the UGameViewportClient shouldn’t shut it down. So I tried to provide an invalid AudioDeviceHandle and prevent the AudioDevice to be destroyed. But I found the AudioDeviceHandle in UGameViewportClient is private and have no setter. So I add a setter to UGameViewportClient and it works fine. But I want to know if there have a better solution?
Sorry for my poor English.