How can I texture a shape with a camera view of another place in the same (2d) world?

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

I am trying to create portals in my 2d game, and I want each portal to show a view of where the portal leads to. Each portal has a Polygon2D shape that I want to texture with the view into the other place in the world. I assume the way to do the is to set the texture to “New ViewportTexture”, so I added a viewport with a camera to my Portal scene. The problem is, the viewport will be inside of the world I am trying to render it to, and it seems like this is a problem. Also, I saved Portal as a separate scene so that I can create instances of it whenever the character in my game creates a new portal. This means that the viewport isn’t even in the world that the viewport needs to render to until an instance of Portal is created in the world. So I’m guessing I need to write code to set the target world of the viewport every time an instance of Portal is created, but I don’t know how to do this. Any help is appreciated!