Problem with Viewports and stretch modes

woopdeedoowoopdeedoo Posts: 109Member
edited August 2017 in 2D

I'm making a top-down shmup game, and since the game view is narrow and doesn't cover the entire screen, I'm trying to make it so it gets centered, leaving two empty areas to the sides where I might place UI elements and such.

I made some tests a while ago and all went well, but then I was using large sprites and a large resolution. Now I'm using small sprites to achieve a pixelated look, and a resolution of 320x200 that needs to be stretched up. And with that I'm running into problems.

Here's my project if anyone wants to take a look.

To lay it out properly, I made a main scene with just a root node, and a separate Game scene (this is because I will have more than one game modes, and this one is an isolated test for now), and added a node2D with a sprite for the ship. I instantiated the Game scene in the main, tested it, and it worked fine. The ship was at 0,0 relative to the root, so when I changed the root's position to 70,0 the ship was rendered at that position. So far so good.

Then I added some parallax backgrounds in the Game scene, and stuff got problematic. They are rendered at 0,0 no matter what position I set the root node to. One way I solved this in my previous tests, was with a viewport (which I know would also later be useful to hide enemies that might fly out the sides of the game view, so it seems like a worthy approach to me).

I added a Control node under the main, and then a viewport under it, and placed the Game scene as child of the viewport. (I also changed the Game scene's root to a CanvasLayer. I don't remember what this was for, I just did it now because that's how I did it in my previous tests. But if I set it to node2D it still works, and I don't see any difference in behavior).

The Control node's position is now set to 70,0, and my hope is that everything gets rendered there. But, depending on the stretch mode I use it gives me a different unwanted result:

If I use 2d stretch mode it renders things at 70,0 but doesn't stretch anything.
If I use viewport stretch mode it stretches things properly, but renders everything at 0,0, apparently ignoring the Control's position...

Does anyone have any idea why this happens or how to solve it?


  • woopdeedoowoopdeedoo Posts: 109Member
    Answer ✓

    It seems I managed to solve this issue by ditching the Control and Viewport on the main scene (Game scene is now a Node2D directly under the Main), and by creating, in the Game scene, a new Viewport, set it as render target, and adding a ViewportSprite with that Viewport assigned to it. Then I just placed everything under the Viewport. Seems to be working perfectly this way (regardless of the stretch mode I choose, too, it seems).

Sign In or Register to comment.