Error: Viewport Texture must be set to use it

Godot Version

v4.2.1.stable.mono.official [b09f793f5]


I have a character scene, and I want to add an healthbar to it. This is the scene:
Immagine 2024-04-02 151903

I set the Sprite3D’s texture to the SubViewport, and in the character scene, it seems to work fine. However once I go to my main scene I get the following errors:
Immagine 2024-04-02 152123

And at runtime I get these:
Immagine 2024-04-02 152207

The errors are doubled since I have two characters in the main scene. It appears that I can fix them by manually setting the texture for each instance of the character scene, but that could quickly get tedious. Is there any other solution?

Hi! Can I ask why you are using a SubViewport for this? Most times I would use a canvas layer instead to put the health bar onto.

I want it to move with the character. Not sure if you can do that with a canvas layer too.

Hmm, in that case there are two ways I would do it.

The faster way would be to just add the progress bar to the scene, no canvas layer or SubViewport or anything. I’ve done it with Godot 3 in the past and it’s worked fine, unsure for Godot 4. This progress bar should work fine, it will follow the player and zoom in, but it cannot interact with other UI the way you may want it. Video here -

The other way you could do it would be to add a canvas layer for your UI, and put a node2D where you want the healthbar to appear. Then use some code to get the screen position of that node, and set the position of your healthbar to be that position.

Personally, I would go for the latter, it feels easier to expand on for me, but if you want to keep things simpler then the first option should work.