Cant get to render Subview to texture in Godot 4

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By kbkmn

Im building small strategy game. Whenever i click on unit its own camera must project texture to UI element. So i’ve created SubViewport Node set it’s resolution to 256x256, added Camera2D to it and in my TextureRect im setting its texture like that:

set_texture(_camera.get_viewport().get_texture())

But my texture is blank. If i remove SubViewport, camera renders main viewport and projects it to texture just fine, except its rendered in window resolution and with main camera, not with separate one. I’ve tried to enable/disable Own World property for viewport, but there is no difference in results. It seem that Viewport and SubViewport changed from Godot 3 to Godot 4 drastically. Can you help me?

:bust_in_silhouette: Reply From: kbkmn

I’ve found an answer. I need to set Transparent BG to true in SubViewport and in code i need to get Viewport i need to render in SubViewport and set SubViewport world_2d to Viewports world_2d

_subviewport.set_world_2d(_viewport.get_world_2d())

And that’s it