What is the preferred way to acces buttons from viewport_2d_in_3d?

Godot Version

4.3

Question

Hello godotneers,

I’m developing a small VR app at the moment, basically learning from almost the beginning.

I’m having trouble accessing the buttons from a control/UI scene that is assigned to a (instantiated or inherited) “viewport_2d_in_3d.tscn” scene.

RIght now I make my UI scenes, with a control node as root with a script, and connect the button’s signals to that script. Then from that script I write variables of boolean type to a global script. Sometimes this works, sometimes this doesn’t. I’m still learning about proper project organisation.

What is the prefered way to work with buttons/UI elements inside a viewport_2d_in_3d scene?

You could have a look at the Gui in 3D demo project. It showcases, how to design the interface between the 3D world and the inner gui scene.

Usually what I do is have the root script of the 2D scene send out it’s own signals so you can connect to those from outside of the viewport.

If you add the scene just from the property, you’ll need to connect these signals from code. You can call get_scene_instance() on the Viewport2Din3D node to get access to the 2D scene instance.

Alternatively a lot of users have simply enabled editable children on the Viewport2Din3D node and add the 2D scene directly under the viewport child node. That is what happens behind the scenes when you set the subscene through the property. Now you can use the normal IDE functions to hook up the signals.