Setting viewport world2D to root viewport world2D not working?

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

I have a simple 2d game in which i want to make a minimap.

I use a viewport with a script that tells it in _onready:

world_2d = get_tree().get_root().world_2d;

When i use this, my game is not rendered inside the minimap viewport , however the minimap can work if i put my game elements in a custom(as in, not the root viewport) viewport, and tell my minimap viewport to set its world to the custom viewport’s one.

export(NodePath) var targetViewport;
func _ready():
world_2d = get_node(targetViewport).world_2d;

i am really wondering why setting the minimap viewport’s world2d to that of an added viewport works but setting it to that of the root viewport doesn’t.

:bust_in_silhouette: Reply From: cgx

It seems that it is not possible to get the “world” of a parent’s viewport, because it will try to render the child’s viewport, which needs the parent’s viewport, which will try to render the child’s viewport, and so on.

In this case, any viewport you put on the scene tree will be a child of the root viewport by default, so you cannot access the root’s world2d from within a child viewport.

You are my saver TT

pengyou | 2021-09-04 08:20

Glad that it helped you!

cgx | 2021-09-04 13:31