All Questions

Community

D
jaspior

2d viewport world sync problem

I'm workig on a 2d game and I was trying to make a minimap.

while there are different ways to do it, I've tried using a second viewport. I was able to make it work only when I put the game scene in a viewport inside a viewport container and the minimap viewport in a canvas layer. 

I've tried to make the game scene and only add an viewport container and viewport and in the ready function I do something like this:

$viewportContainer/viewport.world_2d = get_tree().get_root().get_viewport().world_2D 

or 

$viewportContainer/viewport.world_2d = get_node("main").world_2D

In both ways, godot is unable to find the world_2D and the minimap viewport don't render anything.

I was wondering if it's possible to show multiple viewports in a fully 2D game without the need to use multiple canvaslayers and viewports. 

I can send a demo code if its necessary. Thank you!

  • Nathan Lovato replied

    You don't necessarily need a viewport to create a minimap, but you do need one viewport per separate game world to render to a texture.

    I don't think you can do what you're trying to achieve.

    The simplest setup to render some nodes to a separate view and display the output on screen is ViewportContainer -> Viewport -> the nodes you want to render. Viewport renders its children through the current camera among its children, and that's pretty much it.

    For a minimap, you do need unique nodes that represent characters and other entities from your game world. I don't know a good way around creating new nodes. You can sync those nodes in the minimap world with the ones from the game world using RemoteTransform2D nodes, like I think we show in the viewport guide.

    An alternative is to keep a reference to every game entity in the minimap code and update their position manually in _process.

    I'd say those are your main options.

    I was wondering if it's possible to show multiple viewports in a fully 2D game without the need to use multiple canvaslayers and viewports. 

    You don't *need* canvas layers, although there's no reason not to use one if you want to ensure your minimap displays above everything else. You'll avoid Z ordering issues instantly using a canvas layer.

    You don't need a viewport at all either if you don't need any post-processing or clipping on your minimap. You can just use a background texture and spawn individual sprite nodes for icons over it - but then you do need to manually clamp and manage the position of the icons to ensure they don't get out of the minimap's background.