Dynamic TextBlock text

So am I missing something or do textBlocks not work when declaring the text as a variable rather than a static string? I’m trying to make a simple overlay to show some debug information that changes in real time but I can’t seem to figure out how to declare the value for the textBlock as a variable. Anyone have any ideas on how this can be done? Some of the examples seem to use arrays of strings to change values but I find it hard to believe that’s the only way it can be done.

1 Like

As far as I know, you need to recreate the texture for this.
like this pg:
https://playground.babylonjs.com/#3E8YWK#2

2 Likes

As far as I understand you just need to get this variable from the render loop:

    scene.registerBeforeRender(function () {
        text1.text = "current frame time (GPU): " + (instrumentation.gpuFrameTimeCounter.current * 0.000001).toFixed(2) + "ms";
     });

Example - https://playground.babylonjs.com/#HH8T00#1

4 Likes

Ok I did some more testing. And I completely forgot about template literals.

  createDebugOverlay(fps) {
    this.fps = new GUI.TextBlock()
    this.fps.text = `${fps}`
    this.fps.color = 'white'
    this.fps.fontSize = 24
    this.advancedTexture.addControl(this.fps)
  }

  updateOverlay(fps) {
    this.graphics.scene.registerBeforeRender(() => {
      this.fps.text = `${fps}`
    })
  }

This snippet works fine, but this one also works. You don’t have to register it before the render. But that probably is good practice.

  updateOverlay(fps) {
    this.fps.text = `${fps}`
  }

I’m gonna leave the first one, it seems like good practice with the engine.

Thanks guys. This one was bugging me, though, I probably should code during the day when my brain is fully functional because I generally use template literals all the time but for some reason didn’t think to here.

And thank you @musk, that solution though is probably more for creating actual dynamic 3D mesh text blocks but it is still pretty awesome. I’ll be using that in the future as I build out the UI, for now I’m just doing some testing and I’m tired of logging everything out to the console which slows things down often.

Appreciate yall.

4 Likes