0 votes

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?

in Engine by (1,544 points)
edited by

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

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. :-O

1 Answer

+1 vote

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()
by (167 points)
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 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 webmaster@godotengine.org with your username.