+1 vote

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.


in Engine by (298 points)

1 Answer

+1 vote
Best answer

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.

by (5,278 points)
selected by

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?

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.

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!

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):

Thanks buddy massive help all running perfectly!

Quite welcome. ^_^

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.