Set camera2d to only appear in part of the screen?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By stubbsy345
:warning: Old Version Published before Godot 3 was released.

Don’t know if i’ve phrased that very well. Basically, I have my character with its own camera2d attached to moving around on a tilemap. what i want to do is instance this on another scene (which is a tabcontainer) so that the actual game only appears in the central part of that particular tab, as if it were a window of the game, rather than it filling the whole screen. Just like you would have a map in the corner of a game. I’m new to godot, and game coding in general. I presume it has something to do with viewport but I can’t figure out how to do it.

Any help would be greatly appreciated.

Thanks!

:bust_in_silhouette: Reply From: avencherus

Correct, you’ll want to do this with a viewport node. The basic layout for this in 2D is to have a Control node as a parent, then make a viewport node as a child. It should then acquire it’s rect size from the control. After that, place a node as a child of the viewport that will act as the world or scene. Then add your character to that node.

Great that has worked. However, my issue now is that the window is only showing that small section of that scene, rather than resizing the scene to fit that smaller viewport. How do I set it so that the smaller viewport shows the whole of the instances scene?

stubbsy345 | 2017-04-02 08:34

For scaling, I think you’ll have to use the Viewport Sprite. Applying a Viewport to a Control I believe just gives you exactly that size looking in. It’s useful for split screen games. I could be mistaken, but I’m not familiar with any options for forcing full resolution to this method.

For a Viewport Sprite, you add your Viewport to a Node2D instead of a Control. Set it’s Viewport/Rect size to your desired full resolution. Then set Render Target/Enabled to true.

Add all the usual world items as children to it.

Then add a Viewport Sprite somewhere in the tree outside of the Viewport node. Assign it’s Viewport to the one you created.

From there you can scale and transform the Viewport Sprite as you like.

avencherus | 2017-04-02 10:56

Thanks that is really useful. The window looks great now. One final problem, and if you could help me I would massively appreciate it. In this format it appears that the viewport does not pick up input event that are attached to the child of the viewport. However, it was before so I think it may be something to do with the rendering. Do you know of a solution.

Thanks again for your help!

stubbsy345 | 2017-04-02 12:03

I work around that by creating a forwarding function in the child node. Inside the higher node that detects input, I pass the event along.

Make sure you check your values with prints, because if you use transforms, you’ll want to apply that to things like screen coordinates, so they remain accurate.

func _input(event):
  mynode.forwarded_input(event)

avencherus | 2017-04-02 12:07

Thanks buddy massive help all running perfectly!

stubbsy345 | 2017-04-02 12:23

Quite welcome. :slight_smile:

avencherus | 2017-04-02 12:23

Hi, one last question. In my subscene within the ViewportSprite I have added a button that you need to be able to press. I understand how to connect for keyboard input but I can’t figure out how to pass the signal for pressing the button. Is there anyway of doing this?

Sorry for berating you with another question.

stubbsy345 | 2017-04-03 18:34