Render/Cache a Control to Canvas or Texture2D

Godot Version

Godot 4.2.2

Question

Hi! For background, I’m new to Godot but I have extensive working experience with Unity. Please direct me if there’s a better way of doing what I’m trying to do.

I’m making a Sudoku app as my first Godot project to come to grips with the Godot way of doing things. The 9x9 sudoku grid is 81 tiles, each of which is an instanced Scene including a label, and their own 3x3 grid of “pencil” numbers. In Sudoku games, it’s common to have each little square have a 3x3 grid of which numbers you think could go in that square. So we’re looking at 9x9 = 81 “Square” nodes, and 9x9x9 = 729 “Guess” Label nodes total, at the very least.

To that end, I figured I’d use the custom Canvas drawing to re-draw them when changed, rather than actually maintain all the Control nodes simultaneously.

I’d like to be able to take a Scene of Label nodes, a 3x3 grid of Label laid out with the GridContainer, and render them to a Texture2D.

My question is: How do I go about doing this? I spent today going through the documentation and trying (and failing) to get a ViewportTexture to show up. I can confirm that a SubViewport drew correctly to my ViewportTexture inside a TextureRect inside that scene. However, getting it to draw/copy to an ImageTexture doesn’t seem to be happening. Maybe I’m doing something wrong. If so, please spell out the steps that I should be taking from here, assuming that the ViewportTexture is as I expect it to be.

Alternatively, is there a better way of doing this, by overriding the _draw method of CanvasItem (on perhaps a Control with a script on it?)

I think you’re optimizing prematurely here.

I have a bit of a hard time visualizing the result you’re looking for but it doesn’t sound that bad. You can just try rendering a thousand labels and see how it affects performance, then proceed from there.

Viewports are generally hard to work with. I avoid them when possible.

If you don’t want your app to re-render every frame there should be a toggle in the settings to only do so on input. Not sure what it was called, maybe low processor mode.

Thank you. I think you’re right, I shouldn’t be trying to optimize this right now. I think I’m tryharding this whole project because I really want to push Godot to its limits (despite the simplistic nature of a Sudoku app as it is…) and see if it’ll hold water for a larger project.

With that in mind I ended up figuring out the problem, for now. The texture was invalid when calling ImageTexture.update() for the first time.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.