Second viewport not working as expected

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

I am trying to set up a minimap in a 2D game. To do this, I have a Map scene which has the following structure:

enter image description here

My MapViewport size is set to 64x64 and my Camera2D is set to current. The Sprite node is empty.

What I intend to do was to draw the Viewport's texture to the Sprite.

enter image description here

This is the technique recommended in Chapter 21 of Godot Game Engine Development in 24 Hours.

When I add the Map as a child of my Level, it works as expected. I see a 64x64 section of the GridContainer. But when I place the map in my HUD, which is a CanvasLayer, I see the entire GridContainer.

Why is this?

I think you should be using a ViewportContainer for your Viewport. (ed: late, I know)

duke_meister | 2018-07-11 05:04

Btw, in the future, please paste the actual code instead of a screenshot, because it’ll make it easier for some of us to, if necessary, recreate your project. Also, there’s a “Godot Game Engine Development in 24 Hours” book? Dang. :open_mouth:

Ertain | 2019-02-11 23:45

:bust_in_silhouette: Reply From: mokalux

Hi, even later reply but who knows?

I believe you are missing the call to the yield function (needs to be called twice) before you can get the viewport texture. As per the 3D in 2D sample project:

func _ready():
	viewport = get_node("Viewport")
	sprite = get_node("Sprite")
	viewport_sprite = get_node("Viewport_Sprite")

	# Assign the sprite's texture to the viewport texture
	viewport.set_clear_mode(Viewport.CLEAR_MODE_ONLY_NEXT_FRAME)
	
	# Let two frames pass to make sure the screen was captured
	yield(get_tree(), "idle_frame")
	yield(get_tree(), "idle_frame")
	viewport_sprite.texture = viewport.get_texture()